Thursday Section

QU

23rd January 2003

 

Friday Section

Q2, QH

24th January 2003

EEN118 LAB TWO

The purpose of this lab is to get you familiar with designing your own programs. Some will be text-based, and some will use graphics. Nothing requires you to use anything that we have not covered in class yet. In fact it is very important that you only use the features of C++ that we have studied in class. If you already know something of programming, and think you can see a better solution using things we have not yet covered, don't do it. There is a purpose behind this plan.

If some of these exercises seem just too easy to be worth doing, that's good, but do them anyway. Make absolutely sure you really can do it all easily, not just that you think you can. Don't treat the exercises too contemptuously: fate doesn't like being tempted.

 

This week's exercises all appear in sections 2 and 3 of the class notes. You may wish to refer to those sections for reminders of some of the details.

Remember, the point of this lab is to get you used to writing programs properly. If you can't work out how to do something, ask the Lab Guy. You should be concentrating on getting used to the form or shape of a correct program. If you didn't see the solution right away, and needed help from the Lab Guy, that isn't a problem. Just be sure that once you have made it into a working program, you really do understand it. Your working programs should not contain anything that you don't completely understand.

Pay particular attention to giving your programs a good clear layout. All the statements of a function should be neatly lined up at their left-hand edges, and should be indented more than the heading of the function itself. This seems pointless for small programs, but the habits you make now will stick with you for a long time, and if they are bad they will get you. It is much easier to enter a program correctly in the first place than to type it messily and fix it up later.

 

 

[Ex 2] How Long is a Year?

Write a program that calculates the number of seconds in a year, and prints the answer nicely.

[Ex 4 and 5] Black Magic, Stars.

Either: Write a program that draws a tidy regular pentagram.

Or: Write a program draw a star with an even number of points, like a star of David.

 

 

 

 

 

 

 

 

 

 

 

[Ex 6 and 7] Stick Men, Round Heads.

Write a program that draws a stick-man or stick-woman, with his or her left hand raised. It doesn't have to be very good. Try to give him or her a proper round head, but don't worry if you can't see a good way of doing that, it isn't that important.

 

 

 

 

 

 

 

(Hint: on a white background, drawing in white has the effect of erasing)

 

[Ex 11 and 12] Suicidal Artillery, Plotting the Trajectory.

A projectile shot straight upwards from a cannon with a muzzle velocity of v feet per second rises to a maximum height then crashes back to earth after a total of 2v/g seconds in the air (where g is the "acceleration due to gravity", 32 feet per second per second). The height of the projectile above ground at any time can be calculated by this formula:

h = vt-gt2

in which t is the time (in seconds) since the cannon was fired.

Write a program that prints the altitude of such a projectile at times 1, 2, 3, 4, ..., up to 10 seconds into its flight, for some value of v. You will have to edit the program to make it work for a different value of v; don't worry about trying to make it interactive.

If it seems that your program is going to be a little messy, you are right, but don't worry about it. I would expect the formula to appear 10 times in your program. Soon you will see new techniques that let you avoid such things. For now, let the editor do most of the work (cut and paste), and type carefully. You won't appreciate good techniques if you have never suffered from the problems that they prevent.

Then, use the graphics functions to plot a line graph showing the trajectory instead of just printing it as a table of numbers. The vertical axis should be altitude in feet, and the horizontal should be time in seconds. You will need to allow more than one pixel per second on the horizontal axis if you are to be able to see the plot properly.