CS 150 - Intro to Programming - Fall 2001
Lab Exercise 2

Topics:

As you do today's lab, write down your answers to the questions on a sheet of paper as you go along. Make sure to put your name at the top and turn in your answers before you leave the lab.

Part 1 - Variable declaration and initialization

We have discussed in class how to declare and initialize variables. It is important for you to understand the difference between declaring a variable and initializing it. A variable is declared when it appears in a type declaration statement. But it is not initialized until a value is put into it with an assignment statement. The examples below illustrate the differences.

int a, b; //declares two integer variables - their initial value is undefined
int c = 5; //declares two integer variables and assigns them an initial value
int d = 7;
double x, y; //declares floating point variables - their initial value is undefined
double w = 6.223; //declares floating point variables and assigns them an initial value
double r = 5.44;

To illustrate the initial value problem, look at the following program (don't run it yet, just examine it):

/* filename: yourLastNameLab2Part1.cpp
   author: Your Name
   date last modified: Today's Date

   purpose: Output the values of uninitialized objects
*/

#include <iostream>

using namespace std;

int main()
  {
  double d;
  int i;
  char c;
  bool b;

  cout << "d's value is " << d << endl;
  cout << "i's value is " << i << endl;
  cout << "c's value is " << c << endl;
  cout << "b's value is " << b << endl;
  
  return 0;
  }
  1. What output do you expect this program to produce when it is run?
  2. Create a CodeWarrior project (yourLastNameLab2) and add this program to it. Name the source file containing the program yourLastNameLab2Part1.cpp and add it to your project.
  3. Run the program. Explain the results?
  4. Edit the program to assign an initial value to each of the variables.
  5. Run the program and Explain the results.


Part 2 - Error Types

As discussed in class there are three main types of programming errors that you will encounter: syntax, logical and run-time. Create a new text file (see lab 1 for details), then Copy the paint1Error.html program from the codeSamples directory (codeSamples->intro) on the course web site. Name the source file containing the program yourLastNameLab2Part2.cpp and add it to your project (be sure to remove part1) .

a. Run the program. Identify each error that occurs in the program by:

Continue trying to run the program and fixing "bugs" until the program gives the desired results.
Note : For each run of the program, only identify and fix the first error/warning that appears in the Errors & Warnings window. Often one error can cause several others - fixing the first occurrence can eliminate other errors.

The compiler issues two main types of messages when trying to compile and run a program - warning messages and error messages. Direct violations of the syntax rules result in error messages. If the compiler issues an error message, the program will not compile. The compiler may also issue warning messages. While warning messages are not direct violations of the syntax, they usually signal a likely mistake and should not be ignored. Warning messages alone will not prevent a program from compiling. The next set of steps will illustrate these type of compiler messages.


b. Remove the word int from the line containing int main(). Try to run the program. What happens?
replace the word int before going on.

c.Comment out the line #include <iostream> by placing a // at the beginning of the line. (//#include <iostream>). Run the program. Observe the messages concerning cin and cout. What conclusion can you draw about the relationship between cin, cout and the #include <iostream> preprocessor directive? Remove the comment before going to the next step.

d.Comment out the line using namespace std; Run the program. Again observe the messages concerning cin and cout. What conclusion can you draw about the relationship between cin, cout, the #include <iostream> preprocessor directive and using namespace std; ? Remove the comment before going to the next step.

e.Change the first occurrence of the line: length = feet + inches/12.0; to read length == feet + inches/12.0; (add an extra = character). Run the program. What type of messages do you get? Does the program still run? Does the program produce correct results? Do you think it is a good idea to ignore warning messages even if your program still works? Remove the extra = character and make sure the program still runs before going to the next step.

Part 3 - Using cin and cout

Write a C++ program that interactively prompts for 3 integers and displays the inputted value to the screen, the sum of the 3 integers and a good-bye message. Name the source file containing the program yourLastNameLab2Part3.cpp. The dialogue should look exactly as below (when the user inputs 8, 7 and 1). The example user input appears in bold:

Enter a number from 1 to 10: 8
Enter a number from 1 to 10: 7
Enter a number from 1 to 10: 1

//output

You entered 8, 7, and 1
For a total of 16
Bye for now.

Make sure the variables used to store the input is defined as a type int, not a double or float.
Verify that your program works correctly for inputs in the range 1 through 10.

Before going on, Save your program as yourLastNameLab2Part3b.cpp. (File -> SaveAs). This makes a copy of of the file so we can modify the program as outlined below while keeping the original.

Modify your program to print the following results. Make use of the "escape sequences" to format the output as shown below. Note the use of tabs to align columns, new lines as separators and the use of the " double quote character in the output (see page 53 in the book for details of the escape sequences). The dialogue should look exactly as below (when the user inputs 8, 7 and 1). The example user input appears in bold:

Enter a number from 1 to 10: 8
Enter a number from 1 to 10: 7
Enter a number from 1 to 10: 1

//output

#1	#2	#3
8	7	1
For a total of 16
"Good planning saves time" - see you later!!

Part 4 - More cin and cout

Write a C++ program that interactively prompts for two floating point numbers and displays the product and the quotient to the screen. Name the source file containing the program yourLastNameLab2Part4.cpp. The dialogue should look exactly as below (when the user inputs 2.3 and 4.7). The example user input appears in bold:

     Enter first number: 2.3
     Enter second number: 4.7

     The product of 2.3 and 4.7 is 10.81
     and the quotient is 0.49
     Bye for now.

Make sure the variables used to store the input is defined as a type float, not an int.
Make sure to set the output to 2 decimal places. See the book, page 55, for the "magic formula"
Verify that your program works correctly.

Be sure to follow the check out procedure:

Files for this lab:

yourLastNameLab2Part1.cpp
yourLastNameLab2Part2.cpp
yourLastNameLab2Part3.cpp
yourLastNameLab2Part3b.cpp
yourLastNameLab2Part4.cpp