CS 010 - Introduction to Computer Science I
Lab 2. Algorithms, variables, I/O, syntax errors

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:

Write a program that takes 2 integers as input and outputs their sum and product. Test your program with multiple sets of inputs. In other words, run your program multiple times giving different values for the input each time.

For both Part 1 and Part 2, experiment with entering the wrong type of data at the keyboard (e.g. try to input character data into a variable of type int, etc.). What happens when you try to input double values into int variables?


Part 2:

Modify the program from Part 1 to take 2 inputs of type double instead of int. Again, test it with multiple sets of inputs (with both correct and incorrect types).


Part 3:

Write a program that takes two doubles as input using one cin statement to read in both values. Be sure to prompt the user to enter 2 values. Output the 2 numbers and their quotient with exactly one digit after the decimal point. Format your output to look like this:

         first_number / second_number = quotient. (e.g. 48.0 / 1.2 = 40.2)


Part 4:

Write an algorithm (in English) for the following problem definition:

Convert the number of days until Summer Vacation to the number of hours until Summer Vacation, the number of minutes until Summer Vacation, the number of seconds until Summer Vacation, and finally the number of months until Summer Vacation. The number of days will be provided by the user.


Part 5:

Translate the algorithm from Part 4 into a C++ program. Use integer variables.

Your program should output the number of hours, the number of minutes, the number of seconds, and the number of months (assume 1 month = 30 days) until Summer Vacation, each on a separate line. Your output should look something like this:

         Enter the number of days until Summer Vacation:
         10
         There are 240 hours until Summer Vacation
         There are 14400 minutes until Summer Vacation
         There are 864000 seconds until Summer Vacation
         There are 0 months until Summer Vacation

Now change the variables to doubles and recompile and run your program again. How does this affect the output?


Part 6:

Write an algorithm (in English) for the following problem definition.

Past experience of science and engineering students have shown that to be successful, the average student should spend between 3 and 4 hours per unit per week studying and attending lectures and labs. Design an algorithm that will calculate the range of hours a student should be studying per week based on the number of units they are currently taking.

So, if a student is taking 12 units, this algorithm should calculate that they need to be spending between 36 and 48 hours per week studying and attending lectures and labs if they want to be successful.


Part 7:

Translate the algorithm from part 6 into C++ code.


Part 8:

Find a group that has completed the program for Part 5 and exchange code with them. Change the other group's code to cause one or more syntax errors then pass it back to them. Do not tell them where you made changes. When you get your code back, recompile the program to find and correct the syntax errors.

Some examples to use are:

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:

Do Programming Project #1 from chapter 2. Do not include the functionality that would allow the user to repeat the calculation as often as they wish (we have not done loops yet).

Much of the code for this is given in CodeMate if you would like a headstart. If you feel like a real challenge, do not use the code given in CodeMate.


Challenge 2:

Type-up or copy-and-paste the following program, compile and run it ignoring the warnings.

#include<iostream>
using namespace std;

int main()
{
  double double_num = 10.85;
  int int_num = double_num * 100;

  cout << double_num << " * 100 = " << int_num << endl << endl;

  cout << double_num << " * 100 = " << double_num * 100 << endl;

  return 0;
}

Try to explain why the first line does not output the expected result, yet the second one does.