Actually, I need to calculate course average of student, for example course 115: grades are 86.5 and 84.3, so average is 85.4.
Therefore I need to read data from file and store it in array but I don't understand how to do so without using pointers apart from FILE pointer.
There is following data in the file: studentid, courseid, grade
Input file "input.txt":
201456 115 86.5
201456 112 86.4
201456 122 85.4
202145 115 84.3
202145 112 65.3
202145 122 78.4
Code:
#include <stdio.h>
typedef struct{
int courseid;
double courseavg;
} Course;
typedef struct {
int studentid;
double grade[3];
} Student;
typedef struct {
Course course[3];
Student student[2];
} Major;
int main() {
Course course[3] = {
{1, 0.0},
{2, 0.0},
{3, 0.0}
};
Student student[2] = {
{0, 0.0, {0.0, 0.0, 0.0}},
{0, 0.0, {0.0, 0.0, 0.0}}
};
FILE *in = fopen("input.txt", "r");
fscanf(in,"%d %d %f %d %d %f %d %d %f %d %d %f %d %d %f %d %d %f",
&student[0].studentid, &course[0].courseid, &student[0].grade[0],
&student[0].studentid, &course[1].courseid, &student[0].grade[1],
&student[0].studentid, &course[2].courseid, &student[0].grade[2],
&student[1].studentid, &course[0].courseid, &student[1].grade[0],
&student[1].studentid, &course[1].courseid, &student[1].grade[1],
&student[1].studentid, &course[2].courseid, &student[1].grade[2]);
course[0].courseavg = (student[0].grade[0] + student[1].grade[0])/2;
printf("%f", &course[0].courseavg);
return 0;
}
fgets()andsscanf()that should give you a pretty good starting point.