Skip to content
Physics and Astronomy
Home Our Teaching Resources C programming Exercises
Back to top
On this page
Contents

Exercises for week 1

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 simple arithmetic.

Creating projects using Code::Blocks

1. Click on Create a New Project (also available from the File > New... menu) and choose Console Application

2.Choose the "C" language

(NOT "C++").

3. Choose where to store your project.

We suggest you create a folder "PHY2027" and put all your projects inside there.

Where to save your project on Harrison clusters

On the Harrison lab computers do not save your project to the "Network" folder. Instead, if you go into the "Computer" folder you should see the "Personal Filespace (U)" drive. Create your "PHY2027" folder inside there:

When you use a PC in the forum etc you will find your files there.

4. Choose a sensible Project name

NB: I have had trouble when I have typed a folder name in the "Folder to Create Project" box. if this happens to you, use the Folder Selection just by it to choose a folder. be sure to choose a folder inside your home space, again with a space-free name.

"main.c" appears inside the "Sources" folder.

Experiment with Code::Blocks

We suggest you do this part in pairs. Copy a working program from the lecture notes and deliberately make some mistakes. Notice what errors Code::Blocks 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 semi-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.

Unused variables

We have enabled two extra options on our C compiler: one that warns us of things that are technically legal but usually indicate a mistake and another that treats any code with a warning as having an error such as the following:

// Demonstrate unused variable

int main() {
  float x;

  return 0;
}

This can be confusing when you see it for the first time, especially if you have declared a variable knowing you will need it later.

Mini-exercise

  1. Copy the above code into a project and try to Build. You should get a message like:
    main.c:4: warning: unused variable 'x'
    
  2. Now comment out the variable declaration:
      //  float x;
    
    

    You should now be able to Build OK.

(There is no need to keep this mini-exercise.)

Exercise: Calories in a cheesecake

Notes

You may choose to do this as a number of separate projects or as a single program. If you choose the latter:

  • Do each step in turn, checking your answer is correct before going on to the next one.
  • Do not delete the earlier calculations when try the next one: you should end up with one or more programs that perform all the calculations.

NB: in all exercises and assessments where there is an arbitrary number (such as 550 in the "Calories" example below) do not have that number repeated throughout the code. If you need to use a value more than once declare a suitable-named variable and give it that value. Whever we say "calculate" we want the result printed to the screen and checked by hand.

The task

A well-known retailer's Finest Belgian Chocolate Cheesecake weighs 550g. One sixth of the cheesecake contains 399 Calories.

  • Calculate the Calories per 100g.
    How would you check this worked? (For example, is there something you couild print out?)

    Now try out your check.
  • Calculate how many calories each portion would contain if the cheesecake were shared equally between four people.
    How would you check this worked? (For example, is there something you couild print out?)

    Now try out your check.
  • Calculate the fraction of the cheesecake that would form the entire Calorie allowance of a woman on a 500 Calorie day of the popular 5:2 diet.
    How would you check this worked? (For example, is there something you couild print out?)

    Now try out your check.
  • If it takes 3,500 Calories for a person to put on a pound of fat, how much fat would a person expect to put on if they ate a whole cheesecake?
    How would you check this worked? (For example, is there something you couild print out?)

    Now try out your check.
  • A sumo wrestler needs to put on one stone (14 pounds) of weight. How many cheesecakes should s/he buy? (It's OK to print out a floating-point number such as 3.17)
    How would you check this worked? (For example, is there something you couild print out?)

    Now try out your check.

To show the demonstrator

Show a demonstrator your final code and ask for their feedback. (They won't be able to tell you what mark you would have got if you were to have handed it in.) Log in

                                                                                                                                                                                                                                                                       

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