You are on page 1of 7

Write a C program to grade the students for the course.

The program should:

Read student data from a file named scores.txt, where each entry consists of a student ID, mid-term
exam score, final exam score, and homework score. Store this information in arrays, with the total score
calculated based on the following weights:

Mid-term exam score: 40%

Final exam score: 40%

Homework score: 20%

Process the student data and assign grades according to the following criteria:

Score 80 and above: Grade A

Score 75 - 79 (excluding 80): Grade B

Score 65 - 74 (excluding 75): Grade C

Score 55 - 64 (excluding 65): Grade D

Score below 55: Grade F

Print a report of the grading process to an output file named Student_Grade.out. Each row of the output
file should include:

Student ID

Scores for all three parts

Total score

Assigned grade

Implement the following functions:

a. input_data(): A function with three parameters: a one-dimensional array to store student IDs,
a two-dimensional array to store scores for all three parts and total scores, and a file pointer. This
function should open the input file, read the data, and store student IDs and scores in the respective
arrays. Additionally, calculate the total score and return the count of entries read.

b. print_data(): A function with three parameters: a one-dimensional array for student IDs, a
two-dimensional array for scores, and the number of entries to print. This function should print the
student ID and all four scores for the specified number of entries.

c. In the main() function, declare two arrays: a one-dimensional array for student IDs and a two-
dimensional array of size 100x4 to store scores. Initialize these arrays with zeros. Then, call input_data()
to read data from the file and store it in arrays. Record the number of entries in the variable
numStudent. Finally, call print_data() to display the results on the screen.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

The whole code.

#include <stdio.h>

#define MAX_STUDENTS 100

int input_data(int studentIDs[], int scores[][4], FILE *file) {

int numStudents = 0;

while (fscanf(file, "%d %d %d %d", &studentIDs[numStudents], &scores[numStudents][0],


&scores[numStudents][1], &scores[numStudents][2]) == 4) {

scores[numStudents][3] = (int)(0.4 * scores[numStudents][0] + 0.4 * scores[numStudents][1] + 0.2 *


scores[numStudents][2]);

numStudents++;

return numStudents;

void print_data(int studentIDs[], int scores[][4], int numEntries) {

printf("Student ID\tMid-term\tFinal\tHomework\tTotal\tGrade\n");

for (int i = 0; i < numEntries; i++) {

char grade;

if (scores[i][3] >= 80) {

grade = 'A';

} else if (scores[i][3] >= 75) {

grade = 'B';
} else if (scores[i][3] >= 65) {

grade = 'C';

} else if (scores[i][3] >= 55) {

grade = 'D';

} else {

grade = 'F';

printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%c\n",

studentIDs[i], scores[i][0], scores[i][1], scores[i][2], scores[i][3], grade);

int main() {

int studentIDs[MAX_STUDENTS] = {0};

int scores[MAX_STUDENTS][4] = {{0}};

int numStudents = 0;

FILE *file = fopen("scores.txt", "r");

if (file == NULL) {

fprintf(stderr, "Error opening file.\n");

return 1;

numStudents = input_data(studentIDs, scores, file);

print_data(studentIDs, scores, numStudents);

fclose(file);

return 0;

}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1. File Inclusion:

The program includes the necessary header file `stdio.h`, which provides functions for input and
output operations.

#include <stdio.h>

2. Macro Definition:

A macro named `MAX_STUDENTS` is defined to represent the maximum number of students. This
value is set to 100.

#define MAX_STUDENTS 100

3. Function Prototypes:

Function prototypes are declared for the three functions: `input_data()`, `print_data()`, and the
`main()` function.

int input_data(int studentIDs[], int scores[][4], FILE *file);

void print_data(int studentIDs[], int scores[][4], int numEntries);

4. `input_data()` Function:

This function reads data from the input file (`scores.txt`) and populates the arrays for student IDs and
scores. The total score for each student is calculated based on the specified weights (40% for mid-term,
40% for final, and 20% for homework). The function returns the count of entries read.
int input_data(int studentIDs[], int scores[][4], FILE *file) {

int numStudents = 0;

while (fscanf(file, "%d %d %d %d", &studentIDs[numStudents], &scores[numStudents][0],


&scores[numStudents][1], &scores[numStudents][2]) == 4) {

// Calculate total score based on weights

scores[numStudents][3] = (int)(0.4 * scores[numStudents][0] + 0.4 * scores[numStudents][1] + 0.2 *


scores[numStudents][2]);

numStudents++;

return numStudents;

5. `print_data()` Function:

This function takes student IDs, scores, and the number of entries as parameters and prints a
formatted report on the screen. It calculates the corresponding grade for each student based on the
specified grading criteria.

void print_data(int studentIDs[], int scores[][4], int numEntries) {

printf("Student ID\tMid-term\tFinal\tHomework\tTotal\tGrade\n");

for (int i = 0; i < numEntries; i++) {

// Determine the grade based on the total score

char grade;

if (scores[i][3] >= 80) {

grade = 'A';

} else if (scores[i][3] >= 75) {

grade = 'B';

} else if (scores[i][3] >= 65) {


grade = 'C';

} else if (scores[i][3] >= 55) {

grade = 'D';

} else {

grade = 'F';

// Print the student data

printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%c\n",

studentIDs[i], scores[i][0], scores[i][1], scores[i][2], scores[i][3], grade);

6. `main()` Function:

In the `main()` function, arrays for student IDs and scores are declared and initialized with zeros. The
program then opens the input file (`scores.txt`), calls the `input_data()` function to read and process the
data, and prints the results using the `print_data()` function. Finally, the file is closed.

int main() {

int studentIDs[MAX_STUDENTS] = {0};

int scores[MAX_STUDENTS][4] = {{0}};

int numStudents = 0;

FILE *file = fopen("scores.txt", "r");

if (file == NULL) {

fprintf(stderr, "Error opening file.\n");


return 1;

// Read data from file and store in arrays

numStudents = input_data(studentIDs, scores, file);

// Print the student data

print_data(studentIDs, scores, numStudents);

// Close the file

fclose(file);

return 0;

Make sure to have a file named `scores.txt` with the correct format (student ID, mid-term score, final
score, homework score) for the program to read and process. The program calculates the total scores,
assigns grades, and prints the results on the screen.

You might also like