0

I am tring to get this program together and can't seem to make it work. Ok. . this program is to take in a file with a matrix of zeros and numbers 1-9. The zeros are shaded and the numbers are part of a block. All numbers and the ones directly beside them make a block. the program is to count the squares in each block and tell me how many block and squares there are.
0 0 0 0 0 0 0 0
0 0 4 0 0 0 1 0
0 0 2 2 0 0 2 0
0 0 3 0 0 3 3 0
0 0 0 0 4 4 0 0
0 5 0 0 5 0 0 0
0 6 0 0 6 6 0 0
0 0 0 0 0 0 0 0
The borders of the matrix must be shaded. The out put of this program with this file should be:
block 1 contains 4 squares
block 2 contains 9 squares
block 3 contains 2 squares

Ok so I keep getting the wrong out put from the program and don't know where I'm going wrong
can anyone help me?

#include <stdio. h>
#include <stdlib. h>
#define  MAXLINELEN 18


FILE  *getOpen();
void   push(int *, int *);
void   popShow();


struct  Stack
{
     int     row;
     int     col;
     struct  Stack *priorAddr;
}; 
struct Stack *tosp;
 
int main()
{  
   FILE        *inFile;       
   char        **matrix = 0;       
   int          nrows;             
   int          ncols;             
   int          i;
   int          row;
   int          col;
   char         singleLine[MAXLINELEN];
   int          size;
   int          location;
   int          rowElement;
   int          colElement;
   int          countSquares;
   int          block;
       
   
   
   // Open the matrix text file. 
   inFile = getOpen();
   tosp = NULL;
   
   // Reads in the file line by line to determine the number of rows and columns. 
   nrows = 0;
   ncols = 0;
   while (fgets(singleLine, MAXLINELEN - 1, inFile) != NULL)
   {
         nrows++;
         ncols++;
         printf("#%d: %s\n", nrows, singleLine);
   }
   size = nrows * ncols;
   printf("The size of the Matrix is: %d \n", size);
   
   // Dynamically allocates the matrix. 
   matrix = malloc(nrows * sizeof(char *));
   if(matrix == NULL)
   {
        printf("Out of memory. \n");
        exit(1);
   }
   for(i = 0; i < nrows; i++)
   {
        matrix = malloc(ncols * sizeof(char *));
        if(matrix == NULL)
        {
             printf("Out of memory. \n");
             exit(1);
        }
   }
   printf("Memory allocated successfully!\n");
   
   // Populate the matrix with the data from the file. 
   rewind(inFile);
   for (row = 0; row < nrows; row++)
    {
        for (col = 0; col < ncols; col++)
        {
           if (fscanf(inFile, "%c ", &matrix[row][col]) == 1)
           {
                printf("%c ", matrix[row][col]);
           }
           
        }
        putchar('\n');
    }
   
    // Search for every non-zero character in the matrix. 
    for (rowElement = 0; rowElement < row; rowElement++)
    {
        for (colElement = 0; colElement < col; colElement++)
        {
            if (matrix[rowElement][colElement] > '0')
            {
                 printf("Non-Zero Character is at Row: %d  Col: %d\n", rowElement, colElement);
                 push(&rowElement, &colElement);
            }
        }
    }
   
   popShow();
  
   
   printf("Number of squares: %d\n", countSquares);
   printf("Number of blocks: %d\n", block);
   
   
   
   
   
   
   
   
   system("pause");
   return 0;
   
}
   
/* =============================================
                   getOpen
   =============================================
   This function opens the file for reading, and 
   returns the file name back to the program for 
   use. 
*/
FILE *getOpen()
{
     FILE *fname;
     char name[21];
     
     printf("\nEnter a file name: ");
     gets(name);
     fname = fopen(name, "r");
     if (fname == NULL)
     {
          printf("Can not open the file %s. \n", name);
          exit(1);
     }
     printf("File was successfully opened!\n");
     
     return(fname);
}

/* ==============================================
                       push                      
   ==============================================
   This function pushes the element onto the 
   stack. 
*/

void push(int *row, int *col)
{
     struct Stack *newPtr;
     
     newPtr = (struct Stack *) malloc(sizeof (struct Stack));
     if (newPtr == (struct Stack *) NULL)
     {
          printf("\nFailed to allocate memory \n");
          exit(1);
     }
          
     newPtr->row = *row; 
     newPtr->col = *col;
     newPtr->priorAddr = tosp;
     tosp = newPtr;
}

/* ===========================================
                    popShow
   ===========================================
   This function pops the elements off of the
   stack and displays them. 
*/

void popShow()
{
     int  row;
     int  col;
     void pop(int *, int *);
     
     printf("\nPopped from the stack are:\n");
     
     while (tosp != NULL)
     {
        pop(&row, &col);
         printf("Row: %d   Col: %d\n", row, col);
     }
}

/* ===========================================
                         pop
   ===========================================
   This function pops the data off the stack. 
*/

void pop(int *row, int *col)
{
     struct Stack *tempAddr;
     
     tosp->row = *row;
     tosp->col = *col;
     tempAddr = tosp->priorAddr;
     free(tosp);
     tosp = tempAddr; 
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Adak
0

The newptr should be sized for a pointer, so add an asterisk to this line:

newPtr = (struct Stack *) malloc(sizeof (struct Stack));

and make it:
newPtr = (struct Stack *) malloc(sizeof (struct Stack*));


//don't you want to connect the rows, with it's columns?

for(i = 0; i < nrows; i++)
   {
        matrix = malloc(ncols * sizeof(char *));  [B]//where's the row number?[/B]
        if(matrix == NULL)
        {
             printf("Out of memory. \n");
             exit(1);
        }
   }

Edited by Adak: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.