Physics and Astronomy |
Back to top
On this page
Contents Exercises for week 1These 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::Blocks1. Click on Create a New Project (also available from the File > New... menu) and choose Console Application2.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 clustersOn 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 nameNB: 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::BlocksWe 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:
You can copy-and-paste code from the on-line lecture notes for this part, but not for the assignments below. Unused variablesWe 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
(There is no need to keep this mini-exercise.) Exercise: Calories in a cheesecakeNotesYou may choose to do this as a number of separate projects or as a single program. If you choose the latter:
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 taskA well-known retailer's Finest Belgian Chocolate Cheesecake weighs 550g. One sixth of the cheesecake contains 399 Calories.
To show the demonstratorShow 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 |