"Write a program to take Knumbers and scores from an input file (score.dat), calculate the sum and average of the scores for each row
and each column, and display the scores, the sum, and the average in a form of table like the figure below. Use a string to handle Knumbers and an array to calculate the sum and average of each score.
Output should look like this (the spacing is kind of off when I post it on the forum)
Knumber Score1 Score2 Score3 Score4 Score5 Sum Average
--------- ------- ------- -------- -------- -------- ----- ----------
K001 80 90 75 87 85 417 83.00
K002 100 99 89 95 70 453 90.00
K003 70 76 87 95 100 428 85.00
K004 50 60 79 75 99 363 72.00
K005 70 80 90 100 60 400 80.00
K006 50 60 78 45 62 295 59.00
K007 89 98 90 95 97 469 93.00
--------- -------- ------- -------- -------- ------- ----- ----------
Sum 509 563 588 592 573
Average 72 80 84 84 81
My input file (score.dat) contains the following information on it
K001 80 90 75 87 85
K002 100 99 89 95 70
K003 70 76 87 95 100
K004 50 60 79 75 99
K005 70 80 90 100 60
K006 50 60 78 45 62
K007 89 98 90 95 97
and the following is my code
--I know its horribly wrong but I am stuck and have been sitting here sifting through notes trying to figure it out for about 2 hours now--
#include <stdio.h>
#include <string.h>
main(void)
{
FILE *imp; /* File input pointer */
int scoreArray[5]; /* array to handle scores */
int score; /* Integer values */
int i=0,a=0;
int input_status;
char knumber[10]; /* Character string to handle K numbers */
double average=0, sum=0; /* double data types to determine sum and average */
imp = fopen("score.dat", "r"); /* Opens the .dat file (scores.dat) */
input_status = fscanf(imp, "%d", &knumber); /* returned status value */
for(a=0; a<8; a++){ /* Loop to sum up individual columns */
for(i=0; i<6; i++){ /* Loop to sum up individual rows */
scoreArray[i] += score;
}}
fclose(imp);
system("pause");
}
right now it compiles with no error but executes with no data at all
My questions are how to associate the strings and arrays (my loops use a and i integers cuz thats what we have been doing in class but im not sure the program knows to associate the data from the input file to those integer types)
Also we never really learned how to make the execution of a program look like a table as in the example above.
any help would be greatly appreciated, thank you

New Topic/Question
Reply




MultiQuote




|