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();
void pop(int * , int *);
//STACK *createStack (void);
//void push(int *);
//void pushCol(int *);
struct Stack
{
int row;
int col;
struct Stack *priorAddr;
};
struct Stack *tosp;
struct StackRow
{
int row;
//int col;
struct StackRow *priorAddr;
};
struct StackRow *tospRow;
/*struct Stack
{
int element;
struct Stack *priorAddr;
};*/
struct StackCol
{
int col;
struct StackCol *priorAddr;
};
struct StackCol *tospCol;
/*struct POSITION
{
int row;
int col;
};*/
//struct POSITION *pPos;
int main()
{
FILE *inFile;
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.
char matrix[nrows][ncols];
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;
}
I do know that in lines 115 through 126 there should be somthing that when a non-zero is found it stores that location in a temp. and then looks next to it if not below it and to the other side so on and so forth... in order to find the non-zeros that are directly adjacent to each other. Something like
Location counter
up(location, locRow -1, locColumn)
right(location, locRow, locColumn +1)
left(location, locRow, locColumn -1)
down(location, locRow +1, locColumn)
I just don't know how to implement it in C code.
This is not ment to be a program that takes weeks to write, but in my case it is. I don't know what I'm doing and am looking up everything.