CSCE 121: Introduction to Program Design and Concepts
Lab Exercise One
Objective
The aim of this little assignment is to pick up some basic
input and output techniques, to practice using int
and
bool
variables, and to get used to the edit-compile-run cycle.
How far must I get? How do I get help?
You should try your best to finish these questions in your first or second lab session. If you don't complete them in the lab, you'll need to devote some outside time to complete the exercises. Don't spent more than 15 minutes without making any progress: if you really get stuck on something, ask for help. Options include: the person sitting next to you, the TA, a peer teacher, or even a public post on piazza asking for help! (The first few weeks will have very limited reading for the class, so you should have extra time allotted, and that is best used in programming.)
Warming up
Write a program that asks the user for two whole numbers, then tells how many
times the larger is divisible by the lesser. Do this without using any floating-point
mathematics, that is you should be able to get by with the +
,
-
, /
and %
arithmetic operations. (If
you don't know of the latter two, you can look here, or
ask for advice, or do a search.)
Here's an example run:
Please enter a number: 5 Please enter a second number: 64 Thank you. Did you know that 5 divides 64 a total of 12 times?
And here's another:
Please enter a number: 370 Please enter a second number: 12 Thank you. Did you know that 12 divides 370 a total of 30 times?
Warming up, revisited
- In your answer to the previous question, what if one of the numbers above was given as zero? Try it on your code.
- Add code that outputs an error message in the case of a zero.
- The input above is supposed to be a whole number, so you must handle the case of a negative integer input. Do this by looping until a satisfactory input is provided by the user.
Diamonds and crosses
Here are two problems, they're both similar so you can pick one to do in the lab. Save the other for practice, or if you needed a lot of help for the first one, then try do the second one all on your own.
Diamonds
Write a program that prompts the user for a number and prints a diamond shape with that many characters per side. Here are some examples that helps to make clear what is needed:
Gimme a number: 4 @ @ @ @ @ @ @ @ @ @ @ @ |
Gimme a number: 5 @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ |
Gimme a number: 6 @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ |
Note how the left edge is right up against the left hand side? Try to replicate that.
Crosses
Write a program that prompts the user for a number and prints a cross of that size. Again the examples help to illustrate:
A number please: 4 | | | ---+--- | | | |
A number please: 5 | | | | ----+---- | | | | |
A number please: 7 | | | | | | ------+------ | | | | | | |
Acknowledgements
These labs are based on my prior offering of CSCE121.