The Lab Record

A C practical is assessed on three things per program: the algorithm written out in numbered steps, the program itself, and its output on a couple of sample runs. This chapter works through the twelve weeks of a standard first-semester C lab in that exact shape. Every program gets its algorithm first, in the plain step-by-step form a lab record expects, then the complete C89 program you can run and edit here, then a note on the trap the examiner is watching for. Each program also exists as a checked exercise below the lesson, so you can write it yourself against test cases before the lab test asks you to.

Week 1 is arithmetic: the five basic operators on two numbers, then two formulas that look trivial and catch students anyway, the volume of a sphere and Heron's formula for a triangle. Everything here uses chapters 1 to 3 only: scanf and printf, the arithmetic operators, and one function from the math library.

Program 1: Arithmetic on Two Numbers

Accept two numbers and print their sum, difference, product, quotient, and remainder. The remainder operator % only works on integers, so this program reads integers.

Algorithm:

  1. Start.
  2. Read two integers a and b.
  3. Compute a + b, a - b, and a * b, and print each.
  4. If b is 0, print that division is impossible and stop.
  5. Compute a / b and a % b, and print each.
  6. Stop.

Run it with 10 2, then 7 2, then -7 2. The second run shows what chapter 3 warned about: 7 / 2 is 3, because integer division throws the fraction away, and 7 % 2 is the 1 it threw away. The third run shows the sign rule: -7 / 2 is -3, truncated toward zero, and -7 % 2 is -1, taking the sign of the dividend. Examiners like asking for these two outputs by hand.

The zero check is the part students leave out. Dividing an integer by zero is undefined behaviour, so the program has to refuse before the operator runs, not report an error afterwards. Printing the first three results and then stopping is a reasonable contract, and it is the one the exercise checks.

Program 2: Volume of a Sphere

Volume = 4/3 π r³. Two decisions decide whether the answer is right: how π is written, and how 4/3 is written.

Algorithm:

  1. Start.
  2. Read the radius r.
  3. Compute volume = (4.0 / 3.0) × π × r × r × r.
  4. Print the volume to two decimal places.
  5. Stop.

A radius of 1 gives 4.19, and 2 gives 33.51. Now change 4.0 / 3.0 to 4 / 3 and run again: 1 gives 3.14, because 4 / 3 is the integer 1 and the formula silently became π r³. This is the single most common wrong answer in the whole lab, and it produces a plausible-looking number, so the only defence is knowing the rule: an operator with two integer operands does integer arithmetic, whatever it is later assigned to.

π is a symbolic constant, the chapter 2 habit, rather than a literal repeated in the formula. The cube is written as three multiplications rather than pow(radius, 3), which keeps <math.h> out of a program that does not otherwise need it. And note the pairing from chapter 4: %lf reads a double in scanf, but %f prints one in printf.

Program 3: Area of a Triangle from Three Sides

With sides a, b, c, Heron's formula is area = √(s(s−a)(s−b)(s−c)) where s = (a+b+c)/2. The formula assumes the three numbers can actually form a triangle, and the program has to check that itself.

Algorithm:

  1. Start.
  2. Read the sides a, b, and c.
  3. If a + b ≤ c, or b + c ≤ a, or a + c ≤ b, print that this is not a triangle and stop.
  4. Compute s = (a + b + c) / 2.
  5. Compute area = √(s × (s − a) × (s − b) × (s − c)).
  6. Print the area to two decimal places.
  7. Stop.

3 4 5 gives 6.00, the right-angled triangle everyone checks with, and 7 8 9 gives 26.83. Now try 1 2 3 and then 1 1 5. Without the check on step 3, the first makes one of the factors zero and the second makes the product negative, and the square root of a negative number is not a number at all: sqrt returns a value that prints as nan or -nan. A program that prints nan in either spelling for a bad triangle is a program that did not think about its input, and the three-way comparison is what the examiner wants to see. Notice that the same test also rejects zero and negative sides, since a side of 0 or less can never exceed the sum of the other two.

sqrt lives in <math.h>, and on your own machine the link step needs -lm at the end of the gcc command, exactly as chapter 3 described. The sandbox here links the math library for you.

Key Takeaways

  • A lab record entry is algorithm, program, output; write the algorithm as numbered steps that start with Start and end with Stop.
  • Integer division truncates toward zero and % takes the sign of the dividend: -7 / 2 is -3 and -7 % 2 is -1.
  • Division by zero is undefined behaviour; test the divisor before dividing, never after.
  • 4 / 3 is 1. Write 4.0 / 3.0 in any formula meant to be evaluated in real arithmetic.
  • %lf reads a double in scanf, %f prints one in printf, and %.2f fixes two decimal places.
  • Heron's formula needs the triangle inequality checked first, or sqrt of a negative product produces nan.