codegurukool.com · Free coding tutorials & online compiler

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

Steps

  1. Read average: it adds all marks and divides by the count.
  2. Read grade: it returns A, B, C or F.
  3. Press Run and read the report.
  4. 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

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;
}

Run this code in the online editor

← C Big O (DSA)