Professional Documents
Culture Documents
C Programming Case1
C Programming Case1
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:
Process the student data and assign grades according to the following criteria:
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
Total score
Assigned grade
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.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>
int numStudents = 0;
numStudents++;
return numStudents;
printf("Student ID\tMid-term\tFinal\tHomework\tTotal\tGrade\n");
char grade;
grade = 'A';
grade = 'B';
} else if (scores[i][3] >= 65) {
grade = 'C';
grade = 'D';
} else {
grade = 'F';
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%c\n",
int main() {
int numStudents = 0;
if (file == NULL) {
return 1;
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.
3. Function Prototypes:
Function prototypes are declared for the three functions: `input_data()`, `print_data()`, and the
`main()` function.
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;
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.
printf("Student ID\tMid-term\tFinal\tHomework\tTotal\tGrade\n");
char grade;
grade = 'A';
grade = 'B';
grade = 'D';
} else {
grade = 'F';
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%c\n",
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 numStudents = 0;
if (file == NULL) {
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.