Skip to content
School of Physics
Home Our Teaching Resources C programming ex2.html
Back to top

Exercises 2

These form an important part of your learning but are not formally assessed as a part of your course mark. Bring them to a demonstrator next week for feedback. The aim at the moment is for you to be able to use the % operator and integer division, simple if statements and complex arithmetic.

One of the exercises has an optional part involving a more advanced use of if statements (one inside another).

Example

Here is a simple example of an if (properly indented of course!):
/*
 * Simple if demonstration
 */
#include <stdio.h>
int main() {
  int mustbepositive = -2;      /* !! */

  if ( mustbepositive >= 0 ) {
    printf("OK: %d\n", mustbepositive);
    return 0;
  }
  else {
    printf("ERROR: it's negative: %d\n", mustbepositive);
    return 1;
  }
} 

Here it is without indentation:

/*
 * Simple if demonstration with no indentation
 */
#include <stdio.h>
int main() {
int mustbepositive = -2;      /* !! */
if ( mustbepositive >= 0 ) {
printf("OK: %d\n", mustbepositive);
return 0;
}
else {
printf("ERROR: it's negative: %d\n", mustbepositive);
return 1;
}
} 

Exercises

Minutes and seconds

The example in the notes of converting a number of seconds to minutes and seconds has one obvious problem: it doesn't test to see if the result is correct. (It's quite possible of course that it was tested when it was written and the test was later removed.) Fix this!

  1. First, recalculate the total number of seconds and print out the difference between this and the original. Check it is zero.
  2. Extend the calculation (and the test) to convert an integer number of seconds to weeks, days, hours, minutes and seconds.

Quadratic equation

Review the section on complex numbers.

  1. Put the guts of the code inside an if { ... } statement that checks if the discriminant is greater than or equal to zero.

    Tip: if you think you will be using an expression several times it's often easier to create a variable to store its value.

  2. Have an else { ... } that at this stage just says the roots are complex.
  3. Now have the else calculate a complex root. Print it out. This stage will involve you declaring a double complex variable. Do this at the top of main and give it a sensible, clear name.
  4. Check your result for the complex case. Inside the else { ... } calculate the absolute value of the polynomial for the complex solution, i.e. substitute the solution you have just found back into the original polynomial and take the absolute value of the result. Print this out - it should be very small, ideally zero (why might it not be exactly zero?).

    Tip: Remember that the cabs() function returns the absolute value of a complex expression.

  5. Optional, (advanced)

    Put a second if statement inside the original else { ... } that prints a sensible warning if the absolute value of the quadratic is greater than a certain value (say 1e-12).

    Deliberately make a small mistake in the calculation of the complex root (e.g. /(2.0001*a)) and check that your test does indeed pick it up.

Optional: people and cars problem

Write a program to solve the "people and cars" problem to produce an output like "4 cars with three people and 2 cars with four people". It's OK to have output like "4 cars with three people and 0 cars with four people"
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Share
Back to top