Mini-project Part 1: Score Statistics

This project brings together all of Part 1: input, branching, loops, functions, and arrays — with an input/output format like an online judge problem.

The program reads the list of exam scores of one class, then prints a summary of its statistics.

What to build

A program that reads from standard input:

  • first line: one integer n (1 ≤ n ≤ 100,000), the number of students,
  • second line: n integers 0..100, the score of each student.

Then prints five lines:

  1. the lowest score,
  2. the highest score,
  3. the average with two digits after the decimal point,
  4. the number of students whose score is above the average,
  5. five numbers separated by spaces: the number of A, B, C, D, and E grades.

The grade boundaries use the rule from Chapter 04: A ≥ 90, B ≥ 80, C ≥ 70, D ≥ 60, otherwise E.

Sample input:

8
95 72 88 60 45 79 91 83

Sample output:

45
95
76.62
5
2 2 2 1 1

Check it yourself: the average is 613 / 8 = 76.625 → rounded to 76.62 by setprecision. There are five scores above the average (95, 88, 79, 91, 83).

Work plan

Do not write all five output lines at once. Work in stages, and after each stage run the program — as soon as something is wrong, you know exactly which stage caused it.

Stage What to do Done when
1 read n and the n scores cerr << n << " " << a[0]; prints 8 95
2 lines 1 and 2 (lowest, highest) the output is 45 then 95
3 line 3 (average) the output is 76.62, not 76 or 76.625
4 line 4 (above average) the output is 5
5 the huruf ("letter") function and line 5 the output is 2 2 2 1 1

Stage 3 is where two mistakes from Chapter 03 meet at once: integer division, and setprecision without fixed. If your output is 76 or 7.66e+01, check both.

Edge cases you must test

Input Correct output
1 / 100 100, 100, 100.00, 0, 1 0 0 0 0
3 / 0 0 0 0, 0, 0.00, 0, 0 0 0 0 3
4 / 70 70 70 70 70, 70, 70.00, 0, 0 0 4 0 0

Each of the three tests something different. The first: n = 1, where the lowest and highest score are the same data item. The second: a score of 0 must be classified as E, and the initial value for "lowest" must not be 0. The third: "above the average" means greater than, not greater than or equal to — if all scores are the same, the answer is 0, not 4.

If you are still stuck

Read only one hint, try again, and only then read the next.

  1. Have you run the program with the sample input from a file, rather than retyping the numbers?
  2. If the numbers are right but the format is not: does your output contain exactly five lines, with no sentences, and no extra spaces at the end of a line?
  3. If the average comes out as a whole number: does the division use double, and is fixed written before setprecision(2)?
  4. If the grade counts are off: trace one score at a boundary, for example exactly 90 and exactly 89, through your huruf function by hand.
  5. If the sum is wrong for large data: what type is used for the sum accumulator? The value 100,000 × 100 still fits in an int, but getting used to long long here will save you a mistake later.

Files

The starter version and the finished version are in projects/01-statistik-nilai/:

  • starter.cpp — a skeleton with TODO comments.
  • finished.cpp — a working version. Look at it after trying your own.

Compile and run:

g++ -std=c++17 -O2 -o statistik projects/01-statistik-nilai/starter.cpp
./statistik < projects/01-statistik-nilai/contoh-masukan.txt

The < sign supplies the file's contents as input, exactly as an online judge does. That way you do not need to retype the numbers every time you test.

PowerShell does not recognize the < sign. There, use this form:

Get-Content projects/01-statistik-nilai/contoh-masukan.txt | ./statistik

Or run cmd first, and then the < form works as usual. The < form is used throughout the book because it is what is written in almost all contest problems and documentation.

Hints

  • Store the scores in a global array of size 100005.
  • The lowest, highest, and sum can be computed in one pass. The number of students above the average needs a second pass, because the average is only known after all the data has been read.
  • Compare with the average using double, not int.
  • Write one function char huruf(int nilai) that returns 'A' through 'E', then count the results with five counters or an array of size 5.
  • Do not print any lead-in sentence.

What you cannot use yet

  • map, set, or other data structures that have not been taught. An array is enough.
  • sort. It may be used, but it is not needed: all the answers can be obtained with ordinary passes.

Done?

Checklist before declaring it finished:

Optional extensions

If everything works and you want more practice:

  • Also print the median. This requires sort and different handling for even and odd n.
  • Print the mode, the score that appears most often. Because the scores are only 0..100, one array of size 101 is enough — and that is the "array as a marker" pattern from Chapter 07.
  • Measure your program's time with clock() for n = 100,000, then compare it with your own estimate of the number of steps.

Continue to Chapter 08 — What is computational thinking, where the focus shifts from writing code to designing the way to solve a problem.