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

Exercises 1

Experiment with XCode

We suggest you do this part in pairs. Copy a working program from the lecture notes and deliberately make some mistakes. Notice what errors XCode gives you so that when you see these errors in future you know how to deal with them. When you make a deliberate mistake, be sure to remember what it is!

Suggestions:

  • Miss out stuff:
    • A sem-colon.
    • A bracket (both types).
    • A quote.
    • An equals sign;
    • The parentheses after main
  • Use the wrong type of brackets.
  • Use the wrong type of quote.
  • Use a variable you haven't declared.
  • Just type in some rubbish!
The aim is to make it easier to work out what has gone wrong when you make some accidental mistakes for real. Try to finish this by the end of the first hour of the lab class.

You can copy-and-paste code from the on-line lecture notes for this part, but not for the assignments below.

Assignments

  1. Write a program to demonstrate the mathematical identity:

    log(x*y/z) = log(x) + log(y) - log(z)

    Be sure to include a test at the end to demonstrate that it worked (i.e. print out the difference between the two "identical" expressions).

  2. Start a new project for the second assignment. Remember to use the @Exeter Physics template, not the empty one. If there is no main.c file you have chosen the wrong template!

    Write a program to find a solution to a quadratic equation. Choose paramaters such that the discriminant is positive, i.e. that the solutions are real. You need only calculate one of the roots but you must, of course, print out the value of the equation for that root to check that the value is indeed (approximately) zero.

The nominal time allowed for this is the second hour of the lab class plus at least three hours of out-of-class work. This isn't a lot to do in this time, so be sure to obey the mega-rule. See all of the points we made at the end of the example program at the end of the lecture and make sure your indentation is correct, which should be easy in this case.

We are looking for a relatively small amount of high-quality work.

Check your code is indented properly.

You should bring your printed-out programs to next week's class for marking.

If you get stuck email your code to me as plain text (copy and paste it into your email).

                                                                                                                                                                                                                                                                       

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