Collaboration on this lab exercise is strongly ENCOURAGED. The exercise is intended for practice, not assessment -- most people who try should get all participation points. Feel free to ask for help from, and provide help to, others. You shouldn't blindly copy solutions from one another (or from anywhere else) nor simply write code for someone else (even if you explain it as you write), but you can certainly help each other debug, give plenty of suggestions and hints, **explain** why things work or don't work, etc. If you get done early, feel free to walk around and help others.
In functions with more complicated branching of control, one way to insure a reasonable return value is to gather together all the possibilities and issue only one return statement from the very end of the block statement. Here is a function with many branches, and hopefully a return statement for each branch:
/* PURPOSE: Convert a numeric compass position to it's verbal equivalent
RECEIVES: degrees - the compass needle angle in degrees
RETURNS: the value as a compass direction ("North", "North East", ...)
*/
string points_of_compass(int degrees)
{
degrees = degrees % 360;
double octant = degrees / 45.0 + 0.5;
if (octant >= 8)
return "North";
else if (octant >= 7)
return "North West";
else if (octant >= 6)
return "West";
else if (octant >= 5)
return "South West";
else if (octant >= 4)
return "South";
else if (octant >= 3)
return "South East";
else if (octant >= 2)
return "East";
else if (octant >= 1)
return "North East";
else if (octant >= 0)
return "North";
else
return "North West";
}
Rewrite the points_of_compass function as follows
Use the following main() function to test your points_of_compass function.
int main()
{ int degrees;
cout << "Please enter the compass heading (in degrees): ";
cin >> degrees;
string direction = points_of_compass(degrees);
cout << "You are heading " << direction << ".\n";
return 0;
}
The street gambler's game of 3-Card Monte is deceptively simple. Three cards are placed face down on a table. One of the three is the Queen of Spades, and you are shown where it is. The dealer then re-arranges the cards, and asks "Where is the Queen?"
void swap(string& a, string& b)
{
string temp;
temp = a;
a = b;
b = temp;
}
int three_card_monte(string card1, string card2, string card3)
{
swap(card1, card2); // call 1
swap(card2, card3); // call 2
swap(card1, card3); // call 3
if (card1 == "queen")
return 1;
else if (card2 == "queen")
return 2;
else /* (card3 == "queen") */
return 3;
}
int main()
{
string first = "queen";
string second = "king";
string third = "ace";
int guess;
int location;
location = three_card_monte(first, second, third);
cout << "Where's the Queen ( 1, 2 or 3 ) ? ";
cin >> guess;
if (guess == location)
cout << "Congratulations!" << "\n";
else
cout << "Better luck next time, it was number " << location << "\n";
return 0;
}
What are the values of the parameters to three_card_monte(card1, card2, card3)?
To what variable do a and b refer in the first, second and third calls to swap(a,b)?
| call | ||
| 1 | ||
| 2 | ||
| 3 |
What is the value of the variables that a and b refer to before and after the first, second, and third calls to swap(a,b)?
| call | ||||
| 1 | ||||
| 2 | ||||
| 3 |
Now, write (copy-and-paste) this program and run it. Based on your answers above, you should be able to predict where the Queen is.
Change the call to three_card_monte in main() to pass in second in the first position, first in the second position and keep third in the third position. The statement with the call should look like:
location = three_card_monte(second, first, third);
Again, predict where the Queen should be before running the program. Then run the program to check your answer.
A complicated program can be simplified by breaking it up into smaller and smaller subtasks until the subtasks are somewhat trivial. This is called stepwise refinement. Often these subtasks are implemented as their own function.
To draw a house using the graphics application one might break it up into the subtasks, draw_front() and draw_roof(). draw_front() is still a bit complicated, so you might break it up into the subtasks, draw_window() (calls this for as many windows as we need) and draw_door().
These subtasks are sufficiently small enough to easily program. We now organize them together to draw the house. Here is the algorithm based on the stepwise refinements we made:
We call draw_house() to draw the house.
Within the draw_house() function we call draw_front() to draw the front of the house and draw_roof() to draw the roof of the house.
Within the draw_front() function we call the function draw_door() to draw the door and draw_window() to draw a window. We will call the draw_window() function once for each window of the house. This function will require a parameter (a Point) to be passed in telling the function where to draw the window.
Write a program that implements these functions to draw a house.
Write a program that uses the procedures
display_H(Point p)
display_E(Point p)
display_L(Point p)
display_O(Point p)
to output the words HELLO and HOLE to the graphics window. Draw HOLE directly underneath HELLO. The output should look like:
HELLO
HOLE
Point p is the top left corner of the letter. Fit each letter in a 1 X 1 square. Draw Lines and Circles to form the letters. Do not use the Message class or cout.
You are to write a program that converts from 24-hour notation to 12-hour notation or from 12-hour notation to 24-hour notation. You should allow the user to choose which conversion they want.
For example, the 24-hour notation 14:25 converts to 2:25 PM in 12-hour notation. The input should be given as 2 integers and the am or pm can be stored in a string.
The group should start by deciding what functions are needed. At minimum, you should have an input function, an output function, a function to convert from 24 to 12-hour notation and a function to convert from 12 to 24-hour notation. The group should write the comments describing the behavoir of all the functions.
Comments should include pre-conditions. Pre-conditions are conditions that someone using a function must meet before calling the function. A pre-condition might be what the function's parameters should represent. For example, a paramater int& hour should represent hours using 12-hour notation before the function call. Your comments should also describe the expected result of the function, sometimes called a post-condition. A post-condition might specify what a reference parameter contains when the function returns. For example, a parameter int& hour represents the hour in 24-hour notation after the function returns.
Now, split the functions up among the group members. Each member should write one or more functions on their own, not worrying about how the other functions are being implemented as long as the pre and post-conditions are being met.
Finally, each member will write their own main() function. When writing main(), each group member need only read the comments for each function to know how to use/call each function. The program should allow the user to continue converting as long as they want. When done, each member should be able to put their main() together with each group member's functions, then compile and run the program.