CSCE 121: Introduction to Program Design and Concepts
Lab Exercise Two
Objective
The aim of this lab work is to practice loops, conditionals, and related
operations.
There's also an exercise that uses float
s as a representation
that is useful for non-whole numbers.
Conceptually, however, the most important shift is that this labs starts to encourage you to write functions. They are an essential concept as they allow us to first make building blocks, then use our own building blocks to construct grander things, these themselves becoming building blocks in their own right for even larger things.
How far must I get?
This lab is assigned for the entire week. You should complete the questions that you're not able to finish in the lab session on your own. If you are finding it tough to keep up the pace, it may be wise to look over the questions and make a first attempt (even on paper) before arriving at your lab session. That way you can use the time most effectively in getting help with the questions that are sticking points for you.
The last question, which is marked with an asterisk, is just a bit of fun. If you're racing ahead of everyone else, you might like to poke around with it.
The smallest taxicab number
You may have heard of the Indian mathematician Srinivasa Ramanujan; he's the man
about whom a film was made a couple of years ago. Littlewood remarked
that every positive integer was one of Ramanujan's personal friends, and
the following story relates why.
Ramanujan was famous for several things, including his discovery of the infinite series for pi. Among them is this story of the number 1729, retold by G. H. Hardy, after a visit he made to Ramanujan, who was in hospital:
[I]n the taxi from London, Hardy noticed its number, 1729. He must have thought about it a little because he entered the room where Ramanujan lay in bed and, with scarcely a hello, blurted out his disappointment with it. It was, he declared, ‘rather a dull number,’ adding that he hoped that wasn't a bad omen.
‘No, Hardy,’ said Ramanujan, ‘it is a very interesting number. It is the smallest number expressible as the sum of two positive cubes in two different ways.’
Verify this claim by writing a program that takes an integer n
as
input and counts the number of ways that numbers up to n
can be
expressed as the sum of two positive cubes.
From that day onwards, numbers that are the sum of two positive cubes in multiple distinct ways have been known as Taxicab Numbers. The number 87539319 is the next one.
Warm in here?
Write a function that converts temperatures from degrees Fahrenheit to Celsius. Write a second function that converts temperatures from degrees Celsius to Fahrenheit. Now write a function that prompts the user for a temperature and its units, then converts it into the other units.
Here's a sample of five runs, showing the desired behavior:
:: ./temps Please enter a temperature: 103 Is that in Fahrenheit or Celsius? [F/C] F Well, 103F is 39.4444C. :: ./temps Please enter a temperature: 11.1 Is that in Fahrenheit or Celsius? [F/C] F Well, 11.1F is -11.6111C. :: ./temps Please enter a temperature: 10 Is that in Fahrenheit or Celsius? [F/C] C Well, 10C is 50F. :: ./temps Please enter a temperature: 45 Is that in Fahrenheit or Celsius? [F/C] f Well, 45F is 7.22222C. :: ./temps Please enter a temperature: 7.2 Is that in Fahrenheit or Celsius? [F/C] c Well, 7.2C is 44.96F.
Use mathematical function composition to test both the functions by checking that together they form the identity function.
Pepys' question
In 1693 Samuel Pepys
wrote to Isaac Newton asking him the following question:
Which is more likely: getting 1 at least once when rolling a
fair die six times or getting 1 at least twice when
rolling it 12 times?
Newton took the time to work it out and replied.
Write a program that could have provided Newton with a quick answer. You should do this without needing to use random numbers: think about the equations, enumerating the cases exhaustively, and then have the computer do the tedious arithmetic.
Downhill slalom*
Of course Newton was able to respond because they had more time back in those days; back in 1693 they weren't binge watching or playing computer games….
Save the following two files in your home directory on compute.tamu.edu
:
- ski.cpp — this ~100 line file has the main program. You'll be able to understand almost all of it.
- rawkb.h — this is an extra header file, needed to get access to the key strokes. It contains some obscure Linux hackery.
ski.cpp
via the usual commands: g++ ski.cpp -o ski
Run it and play around with it for a bit.
Here are some modifications you can make to customize it:
- Tweak the parameters to make it harder, faster, or whatever. See what they do.
- If you count the number of times the game's while loop runs, you can give the player a score.
- Print a huge game over banner at the end of the game. You can get some swank ASCII art here, for example.
- After the player crashes, you can prompt:
Play again? [Y/N]
and repeat... - You could make the game get progressively harder (either narrowing the
gapsize
or speeding it up) as the play goes on longer. - Perhaps the snow on either side should be dotted with trees every so often?
- If you are using a decent terminal program, then one can display color. Here's a simple example of how. (More ESC formatting codes are listed here.)
* This is material outside the scope for lab quizzes.
Acknowledgements
The idea of using the taxicab numbers and Pepys' question as a programming exercise is from
Sedgewick and Wayne's book "Computer Science: An Interdisciplinary Approach".
The photo of Srinivasa Ramanujan is an edited version of an image in the public domain.
The quotation of Hardy's hospital bedside conversation with
Ramanujan with is from
C. P. Snow's foreword to G. H. Hardy's “A Mathematician's Apology”
Cambridge University Press, p. 37, 1993. (The version with Snow's
foreword is highly recommended as his personal recollections on Hardy are
almost as long as Hardy's little book itself.)