Term Results
Welcome to the last lesson of the C course! In it, you'll put together everything you've learned and build one useful program step by step: the math results of a class for the whole term. The class wrote three tests this term, and for the report cards, the teacher needs every student's average and grade. When you finish, your program will print results like these:
Max is not in the class.
MATH RESULTS
Adam 42 38 45 41.7 B
Emma 36 33 38 35.7 C
Finn 50 50 50 50.0 A
Jack 17 25 20 20.7 D
Lucy 35 40 41 38.7 B
Best: Finn (average 50.0)
Class average: 37.3The program prints a table with each student's points, average and grade, finds the best student, adds bonus points and fixes a mistake in one student's points. You'll use variables, conditions, loops, arrays, strings, functions, pointers and structures: everything from the course. Each exercise continues where the previous one ended (the editor starts with its finished code) and adds one piece.
The data. Each student is a structure of type Student with a name and an array of points from the three tests (each test gave 50 points at most). The students are in the array students at the start of main(). Each student in it has two pairs of braces: the outer ones belong to the whole student, the inner ones to the array points. The comma after the last student isn't a mistake: C allows it, and it makes adding another student easier. So students[0].name is "Adam", and students[0].points[2] is his score in the third test, 45. The variable count holds the number of students. It's computed with sizeof, so it stays right even when you add a student. In a real school, the data would come from the school's online system. The programs here get their data from variables.
The plan. The program is split into functions, and each has one job. So far, there's one: print_report(), which gets the array of students and their number. For now, it prints only the heading. It's above main(), because a function must be declared before you call it. That's the usual shape of a C program: the structure at the top, the functions under it, and main(), which uses them, at the bottom.
The parameter const Student students[] is the address of the array from main() (it means the same as const Student *students), so nothing is copied. Thanks to const, the function only reads the students, which is all printing needs.
You'll print each student on one line, piece by piece: the name, then the points with an inner loop, and finally \n. Widths line up the columns: %-10s pads the name with spaces to 10 characters (the - puts the name on the left), and %4d prints a number right-aligned in 4 characters. For example, football players and their goals in three matches:
#include <stdio.h>
typedef struct {
char name[20];
int goals[3];
} Player;
int main(void) {
Player team[] = {
{"Tom", {2, 0, 1}},
{"Sara", {1, 3, 0}},
};
for (int i = 0; i < 2; i++) {
printf("%-6s", team[i].name);
for (int j = 0; j < 3; j++) {
printf("%3d", team[i].goals[j]);
}
printf("\n");
}
return 0;
}Tom 2 0 1
Sara 1 3 0The condition is i < count, not <=. The last index is count - 1, so with <=, the loop would reach for a student who isn't in the array, and the program would stop with RuntimeError: unreachable.
Instructions
In print_report(), under the line that prints the heading, write a for loop with a variable i that starts at 0 and runs while i is less than count. In the loop, print each student's name with printf("%-10s", students[i].name);, and under it, end the line with printf("\n");. The names appear each on a line of its own.
Between those two printf calls, add an inner for loop with a variable j that runs while j is less than 3 (three tests). In it, print the student's points students[i].points[j] with %4d, without \n. The program prints:
MATH RESULTS
Adam 42 38 45
Emma 36 33 38
Finn 48 50 47
Jack 14 22 17
Lucy 27 40 41Start programming for free
1/7