Congratulations!
Congratulations, your program works, and you've reached the end of the C course! It prints a table with every student's points, average and grade, finds the best student, adds the bonus for helping at the competition and fixes points by name. And you wrote all of it yourself.
One last improvement. The teacher also wants to know how the whole class did: the class average. It's the sum pattern again: a variable that starts at 0, and a loop that adds each student's average() to it. Then you divide the sum by count. After that, you'll see that your program works for any class: a new student joins, and all you need to do is add her to the array.
What you've learned. Look at how each part of the course found its place in your program:
printfand its formats print the table:%sfor the names,%4dfor the points,%6.1ffor the averages and%cfor the grades. The widths line up the columns.- Variables and types hold the sums and the averages. The averages have decimals because you added up in a
doublevariable. - Conditions decide the grade (
else ifin the right order), keep every test at 50 points at most and check whether a student was found. - Loops go through the students and their points (a loop inside a loop), add up, find the best student and search for a student by name.
- Arrays hold the students and the points of each of them.
sizeofworks out how many students there are. - Strings are the students' names:
chararrays that end with'\0', which you print with%sand compare withstrcmp. - Functions split the program into parts with clear jobs:
average(),grade()andbest_student()compute and return a result,add_bonus()andfix_points()change the data,find_student()searches andprint_report()prints. - Pointers let
add_bonus()change the student right in the array (&students[2]), andfind_student()returns the address of the student it found, orNULL. Arrays go to functions as the address of their first element, without copying, andconstsays that the function only reads them. - Structures keep together what belongs together: a name and three scores. You reach the members with a dot, and through a pointer with the arrow
->. A structure parameter is a copy, and a function can return a structure too. - And when something didn't work, the compiler's error messages told you what was wrong and on which line. It always pays to fix the first one first.
C on your own computer. Here, your browser compiled and ran the programs. On your own computer, you have several options:
- An online compiler needs no installation. For example, on onlinegdb.com, choose C as the language, paste your program and run it with the Run button. Input from the keyboard works there too.
- Windows: install Code::Blocks, which is free. Download the installer with
mingwin its name: it also contains the GCC compiler. Then create a new project (File, New, Project, Console application), choose C as the language, replace the sample code with your program and run it withF9(Build and run). - macOS: in the Terminal, install the tools with the command
xcode-select --install. Save your program asmain.c, go to its folder in the Terminal withcd, and compile it:clang -Wall main.c -o main. This creates a program calledmain, which you run with./main. - Linux: install the GCC compiler, for example on Ubuntu with
sudo apt install gcc. Then compile your program withgcc -Wall main.c -o mainand run it with./main.
The -o main part says what the finished program should be called, and -Wall turns on warnings. Read them! Here, the website treated the most dangerous ones as errors, but on your computer, many of them are only warnings, and the program is created anyway. A program with such a mistake may then print nonsense or crash, for example with the message Segmentation fault. After every change to the code, compile it again, or you'll run the old version. Here on the website, the Run button did that for you.
What's next. You now know the basics that every C program is built from. Here are some ideas for what to learn next:
- Input from the user. On your computer,
scanfworks:scanf("%d", &points);waits until the user types a number and presses Enter, and stores it in the variable. Notice the&:scanfgets the address of the variable so that it can change it, just likeadd_bonus(). (Here in the browser, there's no one to type, so the programs got their data from variables.) The teacher could then type in the points herself. - Files. With
fopen,fprintfandfclosefromstdio.h, a program can save the results to a file and read them back next time, so they don't disappear when the program ends. - Memory. The size of the array
studentsis fixed when you write the program. Withmallocandfreefromstdlib.h, a program asks for memory while it runs, for example for as many students as there are in a file. That's where pointers are most useful. - C++. C++ is built on C: almost everything you know works there too, and it adds strings that grow by themselves (
std::string), vectors, classes and much more. There's a C++ course here on the website too. - Your own projects and contests. A number-guessing game, a quiz, tic-tac-toe in a 2D array. You learn the most by solving your own problems. C and C++ are also used to program small devices such as Arduino boards, and they're very popular in programming contests, such as the olympiads in informatics that many countries hold for secondary school students.
And if you'd like to try another language, the C++, Python and JavaScript courses are waiting for you.
Instructions
At the end of print_report(), under the line with the best student, print the class average: add up the averages of all the students in a loop, and divide the sum by count. Our class has:
Class average: 37.3A new student, Mia, has joined the class. Add her to the end of the array students: {"Mia", {40, 44, 47}},. You don't have to change anything else: count is computed with sizeof, and the functions work for any number of students. The results end like this:
Lucy 35 40 41 38.7 B
Mia 40 44 47 43.7 B
Best: Finn (average 50.0)
Class average: 38.4If the class average is 46.1, you're dividing by 5 written straight into the code instead of count. And add Mia at the end: at the start, she would shift the other students' indexes, and the bonus would go to Emma and Finn instead of Finn and Jack.
Start programming for free
7/7