Why isn't it working.

This is a discussion on Why isn't it working. within the C Programming forums, part of the General Programming Boards category; I have been trying to figure out why it will not get past print the chart. I get past the ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    Why isn't it working.

    I have been trying to figure out why it will not get past print the chart. I get past the chart and get no data but zeros. unable to debug. Please help.
    Code:
    # include <stdio.h>
    # include <stdlib.h>
    #include <limits.h>
    
    #define ROWS 12
    #define COLUMNS 8
    #define _CRT_SECURE_NO_DEPRECATE
    
    //Function Declarations
    int** buildTable    (void);
    void  fillTable     (int** table);
    void  processTable  (int** table);
    int   smaller       (int first, int second);
    int   larger        (int first, int second);
    int   rowMinimum    (int* rowPtr);
    int   rowMaximum    (int* rowPtr);
    float rowAverage    (int* rowPtr);
    
    int main (void)
    {
    	//Local Declarations
    	  int** table;
    
       //Statements
    	  table = buildTable();
    	  fillTable    (table);
    	  processTable (table);
    	  
    	  system ("PAUSE");
    	  return 0;
    }// end of main
    /*==============================buildTable=====================================
    Creates backbone of the table by creating an array of pointers, each pointing
    to an array of intergers. 
         Pre:  Nothing
    	 Post: Returns pointer to the table.
    */
    int** buildTable (void)
    {
    	//Local Declarations
    	  //int   rowNum;
    	  //int   colNum;
    	  int** table;
    	  int   row;
    	  
       //Statements
    	 // printf("\nEnter the nuber of rows in the table:");
    	 // scanf ("%d", &rowNum);
    	  table = (int**) calloc (ROWS + 1, sizeof(int*));
    	  for (row = 0; row < ROWS; row++)
    	    {
    			//printf("Enter number of intergers in row %d: ", row + 1);
    			//scanf ("%d", &colNum);
    			table[row] = (int*)calloc(COLUMNS + 1, sizeof (int));
    			table[row][0] = COLUMNS;
    	    }//end for
    	  table[row] = NULL;
    
    	  return table;
    }//buildTable
    /*=============================fillTable=======================================
        This function fills the array rows with data.
    	     Pre:   array of pointers
    		 Post:  array filled
    */
    void fillTable (int** table)
    {
    	//Local Declarations
    	  FILE* spTable;
    //      int i;
    	  int row = 0;
    	  int column = 0;
          
       //Statements
    	  printf("\n============================================================");
    	  printf("\n\t\tNow we fill the table.\n");
    	  spTable = fopen ("c:\\12-8data.txt", "r");
    	  printf("\n============================================================\n");
    
    	  if (!spTable)
    	    {
    		  printf ("Could nopt open 12-8data.txt file.\a\n");
    		  exit (101);
    	    }// if open fails
    
    	  for (int i = 0; i = ROWS * COLUMNS; i++)
    		{
    		 fscanf(spTable, "%d", &table[row][column]);
    		 printf ("%d\t", table[row][column]);
    	     column++;
    		if (column==COLUMNS)
    		{
    		 printf("\n");
    		 row++;
    		 column = 0;
    		}
    	   }
    	  return;
    }//fillTable end
    /*==============================processTable===================================
      Process the table to create the statistics.
        Pre:   Table
    	Post:  Column statistics (min., max., and average)
    */
    void processTable (int** table)
    {
    	//Local Declarations
    	  int   row = 0;
    	  int   rowMin;
    	  int   rowMax;
    	  float rowAve;
    
        //Statements
          while ( table[row] != NULL)
    	    {
              rowMin = rowMinimum (table[row]);
    		  rowMax = rowMaximum (table[row]);
    		  rowAve = rowAverage (table[row]);
    		  printf("\n The statistics for Row %d", row + 1);
    		  printf("\n The minimum: %5d", rowMin);
    		  printf("\n The maximum: %5d", rowMax);
    		  printf("\n The average: %5d", rowAve);
    		  row++;
    	   }//while 
    
    	  return;
    }//processTable
    /*============================rowMinimum=======================================
    Determine the minimum of the data in a column.
       Pre:   given pointer to the column
       Post:  returns the minimum for that column
    */
    int rowMinimum (int* rowPtr)
    {
    	//Local Declarations
    	  int rowMin = INT_MAX;
          
       //Statements
    	  for (int column = 0; column <= *rowPtr; column++)
    		  rowMin = smaller (rowMin, *(rowPtr + column));
    	  return rowMin;
    } //colMinimum
    /*===========================rowMaximum========================================
    Calculate the maximum of the data in a column. 
       Pre:  given pointer to the column
       Post: returns the maximum for that column
    */
    int rowMaximum (int* rowPtr)
    {
    	//Local Declaration
    	  int rowMax = INT_MIN;
    	 
        //Statements
    	  for(int column = 0; column <= *rowPtr; column++)
    		  rowMax = larger (rowMax, *(rowPtr + column));
    
    	  return rowMax;
    }// colMaximum
    /*============================colAverage=======================================
    This function calculates the average of data in a column.
       Pre:   pointer to the column
       Post:  returns the average for that column
    */
    float rowAverage (int* rowPtr)
    {
    	//Local Declaration
    	  float total = 0;
    	  float rowAve;
    
       //Statements
    	  for (int column = 0; column <= *rowPtr; column++)
    		  total += (float)*(rowPtr + column);
    	  rowAve = total / *rowPtr;
    
    	  return rowAve;
    }//colAverage
    /*================================smaller======================================
    This function returns the smaller of two numbers.
      Pre:   two numbers
      Post:  returns the smaller
    */
    int smaller (int first, int second)
    {
     //Statements
       return (first < second ? first :second);
    } //smaller
    /*=================================larger======================================
    This function returns the larger of two numbers.
       Pre: two numbers
       Post: returns the larger
    */
    int larger (int first, int second)
    {
    	//Statements
    	return ( first > second ? first : second);
    }//larger
    I am trying to got to column calulations but just want it to work with rows first.

    Code:
    838 758 113 515  51 627  10 419
    
    212  86 749 767  84  60 225 543
    
     89 183 137 566 966 978 495 311
    
    367  54  31 145 882 736 524 505
    
    394 102 851  67 754 653 561  96
    
    628 188  85 143 967 406 165 403
    
    562 834 353 920 444 803 962 318
    
    422 327 457 945 479 983 751 894
    
    670 259 248 757 629 306 606 990
    
    739 516 414 262 116 825 181 134
    
    343  22 233 536 760 979  71 201
    
    336  61 160   5 729 644 475 993
    the data text 12-8 data text file. Could some one please help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should look into debugging, it will serve you well. Anyway, here's a line to start with:
    Code:
    for (int column = 0; column <= *rowPtr; column++)
    Why do you think this statement works?

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Don't think it's the problem but do you close spTable?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    An advice for you: mixing tabs and spaces is not a good idea. Choose one and you'll find that the code in the thread will look much better indented and easier to read.
    Oh and as esbo points out, you never close the file you've opened. Bad practice. Always close files you open.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Don't like the look of this:=
    Code:
    for (int i = 0; i = ROWS * COLUMNS; i++)
    Did you mean this?
    Code:
    for (int i = 0; i <= ROWS * COLUMNS; i++)
    Seems to work a little better like that!

    Well it gets futher but the results don't look too good!!
    Last edited by esbo; 01-27-2008 at 07:51 PM.

  6. #6
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    OK left a couple of bugs in for you to do with printing formats, and a few debug statements


    Code:
    
    
    # include <stdio.h>
    # include <stdlib.h>
    #include <limits.h>
    
    #define ROWS 12
    #define COLUMNS 8
    #define _CRT_SECURE_NO_DEPRECATE
    
    //Function Declarations
    int** buildTable    (void);
    void  fillTable     (int** table);
    void  processTable  (int** table);
    int   smaller       (int first, int second);
    int   larger        (int first, int second);
    int   rowMinimum    (int* rowPtr);
    int   rowMaximum    (int* rowPtr);
    float rowAverage    (int* rowPtr);
    
    int main (void)
    {
    	//Local Declarations
    	  int** table;
    
       //Statements
    	  table = buildTable();
    printf("\nbuilt");
    	  fillTable    (table);
    printf("\nbuilt");
    	  processTable (table);
    	  printf("\nbuilt2");
    	  system ("PAUSE");
    	  return 0;
    }// end of main
    /*==============================buildTable=====================================
    Creates backbone of the table by creating an array of pointers, each pointing
    to an array of intergers. 
         Pre:  Nothing
    	 Post: Returns pointer to the table.
    */
    int** buildTable (void)
    {
    	//Local Declarations
    	  //int   rowNum;
    	  //int   colNum;
    	  int** table;
    	  int   row;
    	  
       //Statements
    	 // printf("\nEnter the nuber of rows in the table:");
    	 // scanf ("%d", &rowNum);
    	  table = (int**) calloc (ROWS + 1, sizeof(int*));
    	  for (row = 0; row < ROWS; row++)
    	    {
    			//printf("Enter number of intergers in row %d: ", row + 1);
    			//scanf ("%d", &colNum);
    			table[row] = (int*)calloc(COLUMNS + 1, sizeof (int));
    			table[row][0] = COLUMNS;
    	    }//end for
    	  table[row] = NULL;
    
    	  return table;
    }//buildTable
    /*=============================fillTable=======================================
        This function fills the array rows with data.
    	     Pre:   array of pointers
    		 Post:  array filled
    */
    void fillTable (int** table)
    {
    	//Local Declarations
    	  FILE* spTable;
    //      int i;
    	  int row = 0;
    	  int column = 0;
          int i;
       //Statements
    	  printf("\n============================================================");
    	  printf("\n\t\tNow we fill the table.\n");
    	  spTable = fopen ("12-8data.txt", "r");
    	  printf("\n============================================================\n");
    
    	  if (!spTable)
    	    {
    		  printf ("Could nopt open 12-8data.txt file.\a\n");
    		  exit (101);
    	    }// if open fails
    
    	  for ( i = 0; i <= ROWS * COLUMNS; i++)
    		{
    		 fscanf(spTable, "%d", &table[row][column]);
    		 printf ("%d\t", table[row][column]);
    	     column++;
    		if (column==COLUMNS)
    		{
    		 printf("\n");
    		 row++;
    		 column = 0;
    		}
    printf("\npre ret");
    
    	   }
    printf("\nvvvvvvvvvvvvvv");
    		fclose(spTable);
    	  return;
    }//fillTable end
    /*==============================processTable===================================
      Process the table to create the statistics.
        Pre:   Table
    	Post:  Column statistics (min., max., and average)
    */
    void processTable (int** table)
    {
    	//Local Declarations
    	  int   row = 0;
    	  int   rowMin;
    	  int   rowMax;
    	  float rowAve;
    
        //Statements
          while ( table[row] != NULL)
    	    {
              rowMin = rowMinimum (table[row]);
    		  rowMax = rowMaximum (table[row]);
    		  rowAve = rowAverage (table[row]);
    		  printf("\n The statistics for Row %d", row + 1);
    		  printf("\n The minimum: %5d", rowMin);
    		  printf("\n The maximum: %5d", rowMax);
    		  printf("\n The average: %5d", rowAve);
    		  row++;
    	   }//while 
    
    	  return;
    }//processTable
    /*============================rowMinimum=======================================
    Determine the minimum of the data in a column.
       Pre:   given pointer to the column
       Post:  returns the minimum for that column
    */
    int rowMinimum (int* rowPtr)
    {
    	//Local Declarations
    	  int rowMin = INT_MAX;
          int column;
       //Statements
    	  for ( column = 0; column <= COLUMNS; column++)
    		  rowMin = smaller (rowMin, *(rowPtr + column));
    	  return rowMin;
    } //colMinimum
    /*===========================rowMaximum========================================
    Calculate the maximum of the data in a column. 
       Pre:  given pointer to the column
       Post: returns the maximum for that column
    */
    int rowMaximum (int* rowPtr)
    {
    	//Local Declaration
    	  int rowMax = INT_MIN;
    	 int column;
        //Statements
    	  for(column = 0; column <= COLUMNS; column++)
    		  rowMax = larger (rowMax, *(rowPtr + column));
    
    	  return rowMax;
    }// colMaximum
    /*============================colAverage=======================================
    This function calculates the average of data in a column.
       Pre:   pointer to the column
       Post:  returns the average for that column
    */
    float rowAverage (int* rowPtr)
    {
    	//Local Declaration
    	  float total = 0;
    	  float rowAve=0;
    int column;
       //Statements
    	  for ( column = 0; column <= COLUMNS; column++)
    		  total += (float)*(rowPtr + column);
    printf("\ntotal %f", total);
    	  rowAve = total / COLUMNS;
    printf("\nAverage %f", rowAve);
    	  return rowAve;
    }//colAverage
    /*================================smaller======================================
    This function returns the smaller of two numbers.
      Pre:   two numbers
      Post:  returns the smaller
    */
    int smaller (int first, int second)
    {
     //Statements
       return (first < second ? first :second);
    } //smaller
    /*=================================larger======================================
    This function returns the larger of two numbers.
       Pre: two numbers
       Post: returns the larger
    */
    int larger (int first, int second)
    {
    	//Statements
    	return ( first > second ? first : second);
    }//larger

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    Could someone please help me!

    I'm trying hard to understand could someone walk throught my code with me and point out what exactly I'm doing wrong because apparently I keep doing it and I don't see it. PLEASE HElP.

  8. #8
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    OK severl time you wrote

    Code:
      for(int column = 0; column <= *rowPtr; column++)
    When you should have wrote
    Code:
     for ( column = 0; column <= COLUMNS; column++)
    You know the number of columns because you defined it.
    So what is with this *rowPtr business?

    You repeated that error several times.

    Then when you print out the results you do

    Code:
    printf("\n The average: %5d", rowAve);
    Which is wrong because you are printing a float not an int.
    Code:
    printf("\n The average: %5f", rowAve);

    You also seem to have mixed up rows and colums, or at least I did!!

  9. #9
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    OK

    Code

    Code:
    
    
    # include <stdio.h>
    # include <stdlib.h>
    #include <limits.h>
    
    #define ROWS 12
    #define COLUMNS 8
    #define _CRT_SECURE_NO_DEPRECATE
    
    //Function Declarations
    int** buildTable    (void);
    void  fillTable     (int** table);
    void  processTable  (int** table);
    int   smaller       (int first, int second);
    int   larger        (int first, int second);
    int   rowMinimum    (int* rowPtr);
    int   rowMaximum    (int* rowPtr);
    float rowAverage    (int* rowPtr);
    
    int main (void)
    {
    	//Local Declarations
    	  int** table;
    
       //Statements
    	  table = buildTable();
    	  fillTable    (table);
    	  processTable (table);
    
    	  system ("PAUSE");
    	  return 0;
    }// end of main
    /*==============================buildTable=====================================
    Creates backbone of the table by creating an array of pointers, each pointing
    to an array of intergers. 
         Pre:  Nothing
    	 Post: Returns pointer to the table.
    */
    int** buildTable (void)
    {
    	//Local Declarations
    	  //int   rowNum;
    	  //int   colNum;
    	  int** table;
    	  int   row;
    	  
       //Statements
    	 // printf("\nEnter the nuber of rows in the table:");
    	 // scanf ("&#37;d", &rowNum);
    	  table = (int**) calloc (ROWS + 1, sizeof(int*));
    	  for (row = 0; row < ROWS; row++)
    	    {
    			//printf("Enter number of intergers in row %d: ", row + 1);
    			//scanf ("%d", &colNum);
    			table[row] = (int*)calloc(COLUMNS + 1, sizeof (int));
    			table[row][0] = COLUMNS;
    	    }//end for
    	  table[row] = NULL;
    
    	  return table;
    }//buildTable
    /*=============================fillTable=======================================
        This function fills the array rows with data.
    	     Pre:   array of pointers
    		 Post:  array filled
    */
    void fillTable (int** table)
    {
    	//Local Declarations
    	  FILE* spTable;
    //      int i;
    	  int row = 0;
    	  int column = 0;
          int i;
       //Statements
    	  printf("\n============================================================");
    	  printf("\n\t\tNow we fill the table.\n");
    	  spTable = fopen ("12-8data.txt", "r");
    	  printf("\n============================================================\n");
    
    	  if (!spTable)
    	    {
    		  printf ("Could nopt open 12-8data.txt file.\a\n");
    		  exit (101);
    	    }// if open fails
    
    	  for ( i = 0; i <= ROWS * COLUMNS; i++)
    		{
    		 fscanf(spTable, "%d", &table[row][column]);
    		 printf ("%d\t", table[row][column]);
    	     column++;
    		if (column==COLUMNS)
    		{
    		 printf("\n");
    		 row++;
    		 column = 0;
    		}
    
    	   }
    		fclose(spTable);
    	  return;
    }//fillTable end
    /*==============================processTable===================================
      Process the table to create the statistics.
        Pre:   Table
    	Post:  Column statistics (min., max., and average)
    */
    void processTable (int** table)
    {
    	//Local Declarations
    	  int   row = 0;
    	  int   rowMin;
    	  int   rowMax;
    	  float rowAve;
    
        //Statements
          while ( row <ROWS )
    	    {
              rowMin = rowMinimum (table[row]);
    		  rowMax = rowMaximum (table[row]);
    		  rowAve = rowAverage (table[row]);
    		  printf("\n The statistics for Row %d", row + 1);
    		  printf("\n The minimum: %5d", rowMin);
    		  printf("\n The maximum: %5d", rowMax);
    		  printf("\n The average: %5f", rowAve);
    		  row++;
    	   }//while 
    
    	  return;
    }//processTable
    /*============================rowMinimum=======================================
    Determine the minimum of the data in a column.
       Pre:   given pointer to the column
       Post:  returns the minimum for that column
    */
    int rowMinimum (int* rowPtr)
    {
    	//Local Declarations
    	  int rowMin = 10000;
          int column;
       //Statements
    	  for ( column = 0; column < COLUMNS; column++){
    		  rowMin = smaller (rowMin, *(rowPtr + column));
    		}
    	  return rowMin;
    } //colMinimum
    /*===========================rowMaximum========================================
    Calculate the maximum of the data in a column. 
       Pre:  given pointer to the column
       Post: returns the maximum for that column
    */
    int rowMaximum (int* rowPtr)
    {
    	//Local Declaration
    	  int rowMax = INT_MIN;
    	 int column;
        //Statements
    	  for(column = 0; column < COLUMNS; column++)
    		  rowMax = larger (rowMax, *(rowPtr + column));
    	  return rowMax;
    }// colMaximum
    /*============================colAverage=======================================
    This function calculates the average of data in a column.
       Pre:   pointer to the column
       Post:  returns the average for that column
    */
    float rowAverage (int* rowPtr)
    {
    	//Local Declaration
    	  float total = 0;
    	  float rowAve=0;
    int column;
       //Statements
    	  for ( column = 0; column < COLUMNS; column++)
    		  total += (float)*(rowPtr + column);
    	  rowAve = total / COLUMNS;
    	  return rowAve;
    }//colAverage
    /*================================smaller======================================
    This function returns the smaller of two numbers.
      Pre:   two numbers
      Post:  returns the smaller
    */
    int smaller (int first, int second)
    {
     //Statements
       return (first < second ? first :second);
    } //smaller
    /*=================================larger======================================
    This function returns the larger of two numbers.
       Pre: two numbers
       Post: returns the larger
    */
    int larger (int first, int second)
    {
    	//Statements
    	return ( first > second ? first : second);
    }//larger


    Results


    Code:
    
    
    Press any key to continue . . .
    
    
    ============================================================
    		Now we fill the table.
    
    ============================================================
    838	758	113	515	51	627	10	419	
    212	86	749	767	84	60	225	543	
    89	183	137	566	966	978	495	311	
    367	54	31	145	882	736	524	505	
    394	102	851	67	754	653	561	96	
    628	188	85	143	967	406	165	403	
    562	834	353	920	444	803	962	318	
    422	327	457	945	479	983	751	894	
    670	259	248	757	629	306	606	990	
    739	516	414	262	116	825	181	134	
    343	22	233	536	760	979	71	201	
    336	61	160	5	729	644	475	993	
    0	
     The statistics for Row 1
     The minimum:    10
     The maximum:   838
     The average: 416.375000
     The statistics for Row 2
     The minimum:    60
     The maximum:   767
     The average: 340.750000
     The statistics for Row 3
     The minimum:    89
     The maximum:   978
     The average: 465.625000
     The statistics for Row 4
     The minimum:    31
     The maximum:   882
     The average: 405.500000
     The statistics for Row 5
     The minimum:    67
     The maximum:   851
     The average: 434.750000
     The statistics for Row 6
     The minimum:    85
     The maximum:   967
     The average: 373.125000
     The statistics for Row 7
     The minimum:   318
     The maximum:   962
     The average: 649.500000
     The statistics for Row 8
     The minimum:   327
     The maximum:   983
     The average: 657.250000
     The statistics for Row 9
     The minimum:   248
     The maximum:   990
     The average: 558.125000
     The statistics for Row 10
     The minimum:   116
     The maximum:   825
     The average: 398.375000
     The statistics for Row 11
     The minimum:    22
     The maximum:   979
     The average: 393.125000
     The statistics for Row 12
     The minimum:     5
     The maximum:   993
     The average: 425.375000

    You will just have to compare the two sets of code side by side and then work out what
    was changed and why.
    Last edited by esbo; 01-27-2008 at 10:52 PM.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    It typically doesn't work that way. That's why we do it step-by-step, you know.
    Or at the very least, point out where the bugs or errors are.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 07:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 07:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21