Home › C Tutorial › Live project: Student report card
Live project: Student report card
Calculate averages and grades with arrays and functions.
Use arrays, loops, functions and if statements to print a report card.
What you will build
- A two-dimensional array of marks
- An
averagefunction and agradefunction - A formatted report for every student
Steps
- Read
average: it adds all marks and divides by the count. - Read
grade: it returns A, B, C or F. - Press Run and read the report.
- Change the marks and run again.
Run it
Press Run to see the project work. Then change the data in the editor and run it again.
Challenge
- Add a fourth student.
- Print the class topper.
Example code
#include <stdio.h>
double average(int m[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) sum += m[i];
return (double) sum / n;
}
char grade(double a) {
if (a >= 90) return 'A';
if (a >= 75) return 'B';
if (a >= 50) return 'C';
return 'F';
}
int main(void) {
const char *names[] = {"Asha", "Ravi", "Meena"};
int marks[3][3] = {{92, 88, 95}, {70, 65, 80}, {45, 50, 40}};
for (int s = 0; s < 3; s++) {
double a = average(marks[s], 3);
printf("%-6s average %.1f grade %c\n", names[s], a, grade(a));
}
return 0;
}