Hey all this code should read in info and store it to a file. Then retrieve the file and calculate the sum and average of the row and column. But for reason I keep getting rubbish for my sum and average output. can someone look at it and tell what the problem is? Thank you.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream>

#define ROW 2
#define COL 2

void main()
{
   FILE *CanCal;
   int TD[ROW][COL];
   long int i, j, z, col, row;
   float sum=0, avg;

   CanCal = fopen("can.txt", "w");
   if( CanCal == NULL )
   	{
     		puts("Error opening file can.txt");
         exit(0);
      }

   	//}
   //fprintf( HiScores, "%d %d\n", ROW, COL);

   for(i = 0 ; i < ROW; i++)
     {
         for(j = 0; j < COL; j++)
         	{
             	printf("Enter speed(mi/h): ");
             	scanf("%d", &TD[i][j]);
             	fprintf(CanCal, "%d ", &TD[i][j]);
            }
         printf("\n");
		}

   getch();

  /*++++++++++++++++++++++++++++++File read++++++++++++++++++++++++++++++++++++*/

   CanCal = fopen("can.txt", "r+");
   if( CanCal == NULL )
   	{
     		puts("Error opening file can.txt");
      	exit(0);
   	}
   fscanf( CanCal, "%d %d", &row, &col);
   printf("%d %d\n", row, col);
   printf("\n\n");


   for(i = 0 ; i < ROW; i++)
     {
         for(j = 0; j < COL; j++)
         	{
            	fscanf( CanCal, "%d ", &z);
            	TD[i][j] = z;
               printf("%d\t", &TD[i][j]);

            }
            //sum += TD[i][j];
         	printf("\n");
      }

   for(i = 0 ; i < ROW; i++)
   	{
       	for(j = 0; j < COL; j++)
            {
               //sum = 0;

            }
         sum += TD[i][j];
         avg = sum / COL;
         printf("\nThe sum of ROW %d is %f", i, sum);
   		printf("\nThe average of ROW %d is %.2f", i, avg);
         printf("\n");
         //sum = 0;
      }
  /*
   printf("\n================================\n");
   for(j = 0 ; j < COL; j++)
   	{
       	for(i = 0; i < ROW; i++)
            {
               sum += TD[i][j];
            }
         avg = (float)sum / ROW;
         printf("\nThe sum of COLUMN %d is %d", &j, ∑);
   		printf("\nThe average of COLUMN %d is %.2f", &j, &avg);
         printf("\n");
         sum = 0;

      } */

     getch();
     system("pause");
}

CODING ERRORS:

CanCal = fopen("can.txt", "r+");

you havent closed the previously opened pointer to teh same file.

fprintf(CanCal, "%d ", &TD[i][j]);

you're printing the address not the value

printf("%d\t", &TD[i][j]);

ditto

fscanf( CanCal, "%d %d", &row, &col);

well, now that you've done this, your file pointer is halfway through your file. you go to read it again in your ROW and COL index loops, it picks up where you left off.

OTHER PROBLEMS:

include <conio.h>

deprecated, unportable crap library. you don't need getch() or clrscr(). get out of the habit of using this now.

include <iostream>

pointless C++ header unused in C code

void main()

is wrong. main returns a type "int"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.