cin and Computation Worksheet Answers

  1.    cout << "Enter length and width of the room: ";
       cin >> length >> width;
       area = length * width;
       perim = 2 * length + 2 * width;
       cout << "the room is " << length << " by " << width << endl;
       cout << "the area is " << area << endl;
       cout << "the perimeter is " << perim << endl;
  2.    float length, width;
       float area;
       float perim;
  3.    cout << "Enter length and width of the room: ";
       cin >> length >> width;
       cout << "the room is " << length << " by " << width << endl;
       perim = 2 * length + 2 * width;
       cout << "the perimeter is " << perim << endl;
       area = length * width;
       cout << "the area is " << area << endl;
  4. Yes. The values for length and width are read in before they are printed and before they are used for any calculations. The perimeter is calculated before it is printed and so is the area.
  5. No, this will not work. The variables length and width are used to calculate the area and the perimeter before the user's input is stored in them.
  6. The variables length and width contain random values that cannot be predicted. We call these values garbage. If we use them to calculate, then the answer will also be garbage. This leads to the famous saying (well, it's famous in computer science) "garbage in, garbage out".

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002