0

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;
}
7
  • 1
    This may help: stackoverflow.com/questions/11280523/… Commented Oct 10, 2022 at 11:53
  • 1
    Be aware that nobody will write your code for you here. You need to come up with some cod Commented Oct 10, 2022 at 11:54
  • Yeah, I understand this. Commented Oct 10, 2022 at 12:24
  • 1
    Have a look at fgets() and sscanf() that should give you a pretty good starting point. Commented Oct 10, 2022 at 12:32
  • 1
    A working program could look something like this: 1. initialize data 2. open file 3. read file line by line (and process the line) 5. examine data and calculate average Commented Oct 10, 2022 at 12:33

1 Answer 1

2

You need to read the file line by line and process each line independently:

#include <stdlib.h> // EXIT_FAILURE
#include <stdio.h> // fopen()
#include <stdbool.h> // bool

/**
 * Struct that holds evaluation of a student.
 */
struct StudentEvaluation {
    int student_id;
    int course_id;
    float grade;
};

/**
 * Function responsible for parsing a single line.
 */
bool processLineBuffer(
    const char *line,
    struct StudentEvaluation *eval
) {
    // sscanf returns the number of items it
    // successfully parsed
    int ret = sscanf(
         line,
        "%d %d %f",
        &eval->student_id, &eval->course_id, &eval->grade
    );

    // that's why we check if the return value is 3
    return ret == 3;
}

int main(void) {
    FILE *fp = fopen("students.txt", "r");

    if (!fp) {
        fprintf(stderr, "Failed to open file.\n");
        return EXIT_FAILURE;
    }

    char line_buffer[256];

    while (fgets(line_buffer, sizeof(line_buffer), fp)) {
        struct StudentEvaluation eval;

        // only examine StudentEvaluation if parsing was successful
        if (processLineBuffer(line_buffer, &eval)) {
            printf("successfully parsed: \n");

            // print the parsed data:
            printf("    student_id is %d\n", eval.student_id);
            printf("    course_id is %d\n", eval.course_id);
            printf("    grade is %f\n", eval.grade);
        }
    }

    fclose(fp);
}

This is of course by no means the complete program, but it should give you a framework to (hopefully) complete your program.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.