CS 010 - Introduction to Computer Science I
Lab 6. Pass-by-reference and Void Functions; Mid-quarter evaluations

Points(10 overall)

Collaboration policy:

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.


Part 1

Evaluation Instructions
Anonymous evaluations

Part 2

Time Notation Conversion

You should form into groups of 2 to write this program.

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.


Part 3: Program Debugging

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 check this or use the debugger.)Why?

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. What happens now to the values of c and d after swap is called inside the check function?


Challenge Exercises:

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: