C programming Repetition loop

I have most of the code done i just need to be able to make a file read columns instead of lines, any opinions?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  I have to calculate the sum and average of the numbers for each column, and display them in a form of table like the figure below in an output file (socres.out) and on screen.    

[output]
if you run my code you get it nice and clean make sure you have an input file with the provided numbers then, what i am having difficulty with is to sum the numbers displayed horizontal instead of vertical. Have i explained my self correctly?
[/output]
 
Input file, scores.dat, must have the following data.

89 90 95 100 70
60 70 85 86 92
100 95 87 92 98
80 85 78 94 96
78 85 67 90 89
70 80 98 88 95
50 79 89 77 75
100 92 96 86 83
78 83 88 67 79
90 80 78 95 100



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 #include <stdio.h>

main()
 {
 	
 	
 	double sum; /*Sum of all numbers*/
 	double average; /*Average of all numbers*/
 	int first, second, third, fourth, fifth;
 	int line = 1;
 	
 	FILE *inp, /*input file*/
 	     *outp; /*output file*/
    
    /*Open Files*/
 	inp = fopen("scores.dat", "r");				
 	outp = fopen("outp.out", "w");			
 	
	printf(outp,"Assignment 7\n");
    printf(outp,"Rodolfo Esparza\n");
    printf(outp,"K00326334\n\n"); 
	 
	  fprintf(outp, "      Score1       Score2           Score3           Score4         Score5\t        Sum\t      Average\n");
	  fprintf(outp, "   ---------\t ---------\t ---------\t  ---------\t ---------\t  ---------\t    ---------\n");
	 
	 
	 
    printf("Assignment 7\n");
    printf("Rodolfo Esparza\n");
    printf("K00326334\n\n"); 
	 
	  printf("\tScore1\tScore2\tScore3\tScore4\tScore5\t   Sum\tAverage\n");
      printf("\t------\t------\t------\t------\t------\t------\t-------\n");
	 
	 
	line = 1;						
	while (line <= 10)				
	{
		fscanf (inp, "%d %d %d %d %d", &first, &second, &third, &fourth, &fifth);
		
		sum = first + second + third + fourth + fifth;    //Formula for sum
		
		average  = (first + second + third + fourth + fifth)/5; //Formula for average
		
	printf ("\t   %d\t    %d\t    %d\t   %d\t   %d\t   %2.0lf\t  %.2lf\n\n", first, second, third, fourth, fifth, sum, average);
    fprintf (outp, "\t  %d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%2.0lf\t\t%.2lf\n\n", first, second, third,fourth, fifth, sum, average);			
		line++;		
	}
    
    fprintf(outp, "   ---------\t ---------\t ---------\t  ---------\t ---------\t  ---------\t    ---------\n");
	printf("\t------\t------\t------\t------\t------\t------\t ------\n");
    
    
    
	 /*close Files*/
     fclose(inp);
     fclose(outp);
 
	system ("pause");
 }
i just need to be able to make a file read columns instead of lines, any opinions?


you can't do that, you should use a multidimensional array instead and compute the values using for loop
I'm not sure quite what the question means. The input file is what it is, so you have to read it more or less as at present. But what you do after reading it can be changed according to requirements.

If you want to find the sum of each column, then introduce some new variables for each total, and inside the while loop, at some point after reading the line of data from the file, put this:
1
2
3
4
5
        sum1 += first;
        sum2 += second;
        sum3 += third;
        sum4 += fourth;
        sum5 += fifth; 

Then at the end, after the loop has terminated, output the totals.

But if you are wanting to transpose rows and columns, then yes, a 2D array is the way to go, I think.
Topic archived. No new replies allowed.