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");
}