CSC161 2011S Imperative Problem Solving

Laboratory: Program Correctness and assert

Summary: In this lab, you will explore the use of C's assert macro.

Prerequisites: Familiarity with functions, separate compilation, and arrays.


a. Create a directory for this lab.

b. In that directory, create our standard Makefile.

c. Review the man page for assert.


Exercise 1: Quotient

Consider the following program, named quotient.c, which is intended to take the quotient of two values entered from the command line.

#include <stdio.h>
#include <stdlib.h>

main (int argc, char *argv[])
  int x = atoi (argv[1]);
  int y = atoi (argv[2]);

  int result = x / y;

  printf ("%d/%d = %d.\n", x, y, result);

  return EXIT_SUCCESS;
} // main

a. Make a copy of the program and compile it.

b. Verify that the program works as expected when given appropriate inputs, such as 4 and 2.

c. What do you expect the program to do when the second parameter is 0?

d. Check your answer experimentally.

Exercise 2: Adding an Assertion

a. Add a call to assert that ensures that the second parameter is nonzero. Then recompile.

b. What do you expect your program to do when the program is given appropriate inputs, such as 128 and 5?

c. Check your answer experimentally.

d. What do you expect your program to do when the second parameter is 0?

e. Check your answer experimentally.

Exercise 3: Cancelling Assertions

The man page for assert indicates that we can turn assertion checking off by adding the -DNDEBUG flag.

a. Arrange to compile your program with that flag set.

b. What do you expect your program to do when the second parameter is 0?

c. Check your answer experimentally.

d. Remove the -DNDEBUG flag.

Exercise 4: Additional Assertions

You program should fail to work correctly if the number of parameters is not 2.

a. Add an assert statement that validates the number of parameters.

b. Verify that your added code has the intended effect.

Exercise 5: Even More Assertions

In the past, a student suggested that I use the following as an alternative to x = atoi (argv[1]), noting that the call to sscanf returns 0 if it fails to find an integer and 1 otherwise.

  assert (sscanf (argv[1], "%d", &x) == 1);

What do you think about this strategy? Be prepared to discuss your answer with the class.

Exercise 6: Primality

Consider a function, int is_prime (int n), which returns 1 if n is prime and 0 otherwise.

What preconditions and postconditions should this function have?

Be prepared to discuss the preconditions and postconditions with the class.

Exercise 7: Infrastructure for Prime Testing


Compile them together to build a simple tester.

Exercise 8: A Unit Tester

Write unit tests for is_prime.

Exercise 9: Code!

Okay, you're finally ready. Write is_prime and verify that it works correctly.

Hint: If a number is compound, then at least one of its divisors is less than the square root of the number.

Hint: You can check if a number is evenly divisible by another number using mod. (x is divisible by y if x % y == 0.

Exercise 10: Correctness

Add assert statements at appropriate places in is_prime.

For Those With Extra Time

a. Discuss your answer to problem 5 with other students.

b. Go over your unit tests with a colleague or colleagues, and see if one of you has tests that the other one doesn't.



Monday, 11 October 2010 [Samuel A. Rebelsky]

Wednesday, 9 March 2011 [Samuel A. Rebelsky]


Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed Mar 9 08:56:44 2011.
The source to the document was last modified on Wed Mar 9 08:56:36 2011.
This document may be found at
A PDF version of this document may be found at

Samuel A. Rebelsky,

Copyright © 2010 Samuel A. Rebelsky. This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.