Pre-lab homework. Read the whole lab, then turn in a flowchart (either neatly handdrawn or using the RAPTOR tool) for each of:
This lab will consist of several independent problems. Show each solution to the instructor when you complete it.
writeTime that
prints out a given number of seconds in the proper format to
either the screen or a file.
The following code fragments give examples of how I'd like to
use it:
| Example Code Fragment | Writes |
int s = 7223; writeTime(s,cout); |
02:00:23 to the screen. |
int s = 7223;
ofstream fout("test.txt");
writeTime(s,fout); |
02:00:23 to test.txt |
Test your function with a program that reads in a number of seconds from the user and either the word "screen" or a file name, and prints the time in hh:mm:ss format to either the screen or a file with the given name.
readPoint that would do it for us.
We should be able to use readPoint in the
following ways:
| Example Code Fragment | Input | Output |
double x=0, y=0; cout << "Enter point: "; readPoint(cin,x,y); cout << "x = " << x << ", "; cout << "y = " << y << endl; |
(3.5,-0.1) from user |
x = 3.5, y = -0.1 |
double x=0, y=0;
ifstream fin("data.txt");
readPoint(fin,x,y);
cout << "x = " << x << ", ";
cout << "y = " << y << endl; |
(0.5,-4.4) from data.txt |
x = 0.5, y = -4.4 |
Notice how readPoint modifies the two double values passed to it. Define function readPoint and test it thoroughly.
printbin(n) that prints the
binary representation of n. You may assume that n >= 0. A
sample run of your program would be as follows:
Enter non-negative integer: 37 In binary that's 100101
writereverse(istream& IN,ostream& OUT) that
reads in strings from the input stream IN (i.e. either typed by the
user or from a file) terminated with the word "end" and prints
the strings to the stream OUT out in reverse
order. For example, if the call from main looks like
writereverse(cin,cout) and the user types
I am sam endthen
writereverse should print out
sam am I
If you finish the above during lab time, try to solve the following:
a, a + b, a + 2b, a + 3b, ...Write a program that reads integers a and b from the user along with positive integer n, and prints out the first n terms of the arithmetic sequence defined by a and b, followed by "...". The trick is you may not use loops! Thus, you need to use recursion. Here is a sample run (user input in red):
Enter a, b and n separated by spaces: 2 5 10
2, 7, 12, 17, 22, 27, 32, 37, 42, 47, ...
listbins(string front, int n);
which, assuming that n is not negative, will print out all the
different strings you can get by tacking an n-bit binary
number onto the back of the string front.
So, for example, listbins("hello",2) should
produce the following output:
hello00 hello01 hello10 hello11
If you get listbins working you're done,
since listbins("",n) prints out all n-bit binary numbers.
This illustrates an important technique. Your first thought
is probably "I'd like a function listbins(int n);
that prints out all n-bit binary numbers." However, we need
to throw in an extra argument which, from the perspective of
a "user" of the function is unnecessary, but which from the
implementer's perspective we need so we can pass partial
results along to the next recursive call. In essence, the
more general, flexible and powerful function listbins(front,n)
is easier to write than the more specific function
listbins(n).
s = "do" and t = "ne"
then s + t is the string "done", and
s + 't' is the string "dot".