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.
Write a void function that just outputs your name to the screen. To test this function you will need to write a program (main function) that just calls this function and then ends. Recall, a void function has the return type void which means the function does not return anything. This function will also not use any parameters to pass anything in.
You may use the following code for your main function:
Write a void function that prints to the screen 2 integers passed in using value parameters. Your main() should call your void function twice. You do not need 2 functions. You will call the same function twice. The first time use int variables to pass in the values. With the second call to the function, use integer constants to pass in the values.
Now write a void function that will get 2 integers from the user and pass them back to main using reference parameters. Unlike the pass-by-value parameters in part 3, the call to this function will have to use variables and not constants. Why is this?
Put this function in the same program from part 3. Your main should now call this function to get the 2 integer values and then pass them into the function from part 3 to output them.
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 char variable as either 'a' or 'p'. However, when you output the time in 12-hour notation, you should output PM not p.
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. You should then agree on pre and post-conditions for all the functions.
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. Post-conditions are the expected result of the function. 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 funtion 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. The program should allow the user to continue converting as long as they want.
What is the output of the following program if the user enters the values 4 and 3 for x and y?
#include<iostream>
using namespace std;
void swap(int& a, int& b);
void check(int c, int d);
int main()
{
int x;
int y;
cout << "Enter two integers\n";
cin >> x >> y;
check(x,y);
cout << endl << x << " <= " << y;
return 0;
}
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
void check(int c, int d)
{
if (c > d)
{
swap(c,d);
}
}
After tracing this program by hand and coming up with the values you think it should output, write this program (copy and paste) and then trace the values of the variables. What are the values of c and d in the function check after the if statement? (You can write a cout statement in the check function to see what they are at that point.) Are they what you expected? Why is the output not what we expect when x is greater than y?
Without changing main or the function definitions, rewrite this program so that it will always have the correct output. In other words, the last cout statement will always make sense.
Please feel free to help your fellow labmates reach this point!!
The following exercises are for your own benefit. You are not required to do these. In fact, they may contain material not covered in class or lab yet. As such, the TAs will give priority to helping students on the previous sections.
Challenge 1: