Array Average in C

Level 3119 keystrokes

Calculates and prints the average of an integer array.

C code

// Four values whose average is wanted
1int arr[] = {10, 20, 30, 40};
// Integer total, starting at 0
2int sum = 0;
// The loop body is the single indented line below
3for (int i = 0; i < 4; i++)
// After the loop, sum is 100
4 sum += arr[i];
// Dividing by 4.0 (a double) avoids integer division; %.1f shows one decimal: 25.0
5printf("Avg: %.1f\n", sum / 4.0);

Output

Avg: 25.0
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • ;× 6
  • ,× 4
  • =× 4
  • +× 3
  • .× 2
  • "× 2
  • (× 2
  • )× 2
  • [× 2
  • ]× 2

More C examples at level 3

← C typing practice