Programming and Webmasters forum
HomeSearchRecent PostsLoginRegister Contact Us

Username  
Password
Announcing 14th Weekly Contest - From 25 July To 01 August.

Win every week on this forum.

Chek out How To Win?
 

Pages: [1]   Go Down
 
  Email this topic  |  Print
0 Members and 1 Guest are viewing this topic.

Need help with C program

 
webmaster forum
Contest Points: 100
 
New Poster
Posts: 3
Topics: 1
March 03, 2010, 12:52:34 PM

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 ther 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);
   //check
   system("pause");
   // 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;
} :b :b :b
 
webmaster forum
Contest Points: 100
 
New Poster
Posts: 3
Topics: 1
March 03, 2010, 12:56:21 PM

The first system("pause");
Code:
system("pause");[
 
webmaster forum
Contest Points: 100
 
New Poster
Posts: 3
Topics: 1
March 03, 2010, 01:00:48 PM

The first system("pause"); should be taken out I just was tring to cheak the code and it does not need to be there.  sorry Just filled with frustration.
Thanks to anyone who can help
 
webmaster forum
polas  Offline
*
 
Hacker
Gender: Male
Posts: 1224
Topics: 78
WWW
March 14, 2010, 03:55:57 PM

Currently on an iPhone so can't do any detailed looking at moment however one thing that jumps out at me is a severe memory bug. You malloc matrix initially to be nrows bytes (by the c standard a char is 1 byte, so you don't need sizeof char.) then after this you loop through to nrows  reallocating matrix, this does not add extra memory like you want to do, the syntax for that would be matrix. As you have it you will have memory overruns and mem leak, would be surprised if it isn't seg faulting

also, I am not convinced , although cannot test on my phone, that 2d access like matrix[a] will work with malloc as the compiler doesn't explicity know the size of memory dimensions. I would either use a 2d array here or malloc nrows by ncols in one statement and access array like matrix[a*ncols+b]


An issue with your logic, you are building a linked list of none zero values, I done see and conditional to ensure that values touch other values to make a distinct block or where you update the blocks var - have you missed out some functions? If not you will need to write them.

Mesham Type Oriented Parallel Programming Language
Skydive in North East England
 
webmaster forum
polas  Offline
*
 
Hacker
Gender: Male
Posts: 1224
Topics: 78
WWW
March 14, 2010, 03:57:25 PM

Oh and you need to set row and col in popshow function

Mesham Type Oriented Parallel Programming Language
Skydive in North East England
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC


Google visited last this page July 22, 2010, 08:24:03 PM

Valid XHTML 1.0 Transitional     Valid XHTML 1.0 Transitional