- bar.cc - Make a function called RepeatedlyPrintX that takes one parameter called n (an
int) and returns nothing. The function should print 'x' to the screen n
times, followed by a newline. Test your function by calling it from main using a few different
arguments, like this (the first line printed should just be "xxxxxxxxxx"):
int main(void) {
RepeatedlyPrintX(10);
RepeatedlyPrintX(5);
RepeatedlyPrintX(7);
return 0;
}
- labeledbar.cc - Make a function called LabeledBar that takes two parameters, one called label (a string)
and one called n (an int). The function returns nothing. The function should print the label, then a space, then call
the RepeatedlyPrintX function so that a bar of width n is printed. Test your function by calling it from main using a
few different arguments, like this (each line printed should look like "dog xxxxxxxxxx", etc.):
int main(void) {
LabeledBar("dog", 10);
LabeledBar("cat", 5);
LabeledBar("rat", 7);
return 0;
}
- bargraph.cc - Make a program that asks the user to enter a file name (e.g. like
this one), opens that file, then repeatedly does
the following (until the end of file is reached): read a word and an int from from the file, then call the LabeledBar
function. The goal is for a nice bar graph to be printed. Be sure to print a warning message if the file could not be
opened, and make sure you close the file at the end of the program.
- prompt.cc - Make a function called PromptForAnInt. It takes no parameters and it returns an int. The
function should print out "Please enter an integer: ", then get an integer from the keyboard, then return that
integer. Test your program like this:
int main(void) {
int x = PromptForAnInt();
int y = PromptForAnInt();
cout << "You entered " << x << " and " << y << endl;
return 0;
}
- primefunction.cc - Make a function called IsPrime. It takes one parameter called n (an int), and returns a
bool. The function should return true if n is prime, otherwise it should return false. (Hint: The code for the body of
this function is nearly identical to the code from the prime.cc program above... just replace the cout statements with
"return true;" or "return false;" statements. Test your program by calling it from main like this:
int main(void) {
int x = PromptForAnInt();
if (IsPrime(x))
cout << x << " is prime!" << endl;
else
cout << x << " is not prime!" << endl;
return 0;
}
- primefinder.cc - Make a program that asks the user to enter a number, then finds and prints the lowest
prime number greater than or equal to that integer. So if they type 20, the program would check if 20 is prime (it's
not), 21 (nope), 22 (not this either), then finally 23 (aha, a prime). Use the PromptForAnInt and IsPrime functions as
appropriate.
- smallrect.cc - Make a function called Print3x5Rectangle that takes no parameters and returns nothing.
The function should call RepeatedlyPrintX(5) three times so that a rectangle
is printed to the screen. Test your function by calling it from main.
- rect.cc - Make a function called PrintRectangle that takes two parameters called width
(an int) and height (an int) and returns nothing. It should call
RepeatedlyPrintX in a loop so that a rectangle of the given width and height
is printed. Test your function by calling it from main a few times.
- square.cc - Make a function called PrintSquare that takes one parameter called size (an
int) and returns nothing. It should call PrintRectangle so that a square of
the given size is printed. Test your function by calling it from main a few times.
- draw.cc - Make a program that opens a file of the user's choice (e.g. like this one) and repeatedly does
the following (until the end of the file is reached): Read a word from the
file. If the word is "square", then read an int and call the PrintSquare
function so that a square gets printed. If the word is "rectangle", then
read two ints and call the PrintRectangle function. If the word is "skip",
then print out a blank line. Congratulations, you have made an ascii-art
drawing program!
- change.cc - Make a function called MakeChange that returns nothing but takes one int parameter called price and five int
reference parameters called dollars, quarters, dimes, nickles, pennies. The function should assign the five reference
variables to the appropriate values based on the price. So if price is 527, then dollars should be assigned 5, quarters should
be 1, pennies should be 2, and nickles and dimes should be assigned 0. Test your function with a main program like this:
int main(void) {
int d, q, m, n, p;
int price = PromptForAnInt();
MakeChange(price, d, q, m, n, p);
cout << "That is "
<< d << " dollars "
<< q << " quarters "
<< m << " dimes "
<< n << " nickles and "
<< p << " pennies"
<< endl;
return 0;
}
- spellcheck.cc - Make a function called SpelledCorrectly that takes one parameter called word (a string) and returns a
boolean. The function should search through the "/usr/share/dict/words" file, and it should return true if the word
was found in that file, otherwise it should return false. (Hint: The body of this function is almost identical to the
spelling.cc program above.). Now write a main function that asks the user to enter a file name, opens that file,
then repeatedly does the following (until the end of the file is reached): read a word from the file, call the
SpelledCorrectly function, and prints a friendly message if it returned false.