Task 3: Class Gradebook
Difficulty: Moderate
Problem to Solve
Create a small gradebook that stores student records, calculates class statistics, and arranges the students by score.
Create a file named gradebook.c inside a folder named class-gradebook.
Each student record must contain a name and a score. The program must read the
records, calculate the class average, identify the highest-scoring student, and
sort the records from highest score to lowest score.
Requirements
- Define a structure named
Studentcontaining:- a character array named
name; and - an integer named
score.
- a character array named
- Create an array with space for at most 10 students.
- Ask how many students will be entered.
- Accept between 3 and 10 students.
- For every student:
- read a one-word name of at most 49 characters;
- read an integer score from 0 through 100; and
- re-prompt if the score is outside that range.
- Calculate and print the class average with exactly two digits after the decimal point.
- Identify and print the student with the highest score.
- Sort the complete student records from highest score to lowest score using selection sort.
- Print the sorted ranking with positions beginning at 1.
- Keep all program logic inside
main; do not create additional functions. - Use only standard C libraries.
If two students have the same score, either order is acceptable. If several students share the highest score, printing the first one entered as the top student is acceptable.
You may assume that values entered for the student count and scores are integers. Your program must still reject integers outside the required ranges.
Example
$ ./gradebook
Number of students: 4
Student 1 name: Farid
Farid's score: 74
Student 2 name: Madina
Madina's score: 95
Student 3 name: Rustam
Rustam's score: 81
Student 4 name: Zebo
Zebo's score: 86
Average: 84.00
Top student: Madina (95)
Ranking:
1. Madina - 95
2. Zebo - 86
3. Rustam - 81
4. Farid - 74
Starter Code
#include <stdio.h>
typedef struct
{
char name[50];
int score;
} Student;
int main(void)
{
Student students[10];
int student_count;
// Read a valid number of students.
// Read every student's name and score.
// Calculate the average and find the highest-scoring student.
// Sort the Student records by score.
// Print the results and ranking.
}
Reading the records
Use a loop from 0 through student_count - 1. A name can be read into
the current structure with a width-limited conversion:
scanf("%49s", students[i].name);
Read the score into students[i].score. A do-while loop is useful for
repeating the prompt until the score is between 0 and 100.
Average and highest score
While visiting the records, add every score to an integer total. After the
loop, convert one value to double before dividing:
double average = (double) total / student_count;
Store the index of the highest-scoring student. Begin with index 0, then
replace it whenever a later record has a greater score.
Sorting structures
Selection sort can compare students[j].score values. When a larger score
is found, remember the index of the complete record.
Swap entire Student values, not only their scores:
Student temporary = students[i];
students[i] = students[largest];
students[largest] = temporary;
Keeping the name and score in one structure ensures that they remain together during the sort.
Preserve the top student
Sorting changes the positions of records in the array. Either print the top student before sorting, or save the top student's name and score before the sort begins.
Compile and Test
From the class-gradebook folder, run:
cc gradebook.c -o gradebook
./gradebook
If your system provides gcc instead of cc, run
gcc gradebook.c -o gradebook.
Check at least these cases:
- exactly 3 students;
- exactly 10 students;
- students entered in increasing score order;
- students entered in decreasing score order;
- several students with the same score;
- every student with a score of
0; - every student with a score of
100; and - an invalid score such as
-1or101, followed by a valid score.
Return to the Problem Set 3 overview.