Chapter 3: Prelab Assignment
Numeric Types, Expressions, and Output | 51
Name __________________________________________ Date _______________________
Section _________________________________________
Examine the following program carefully and answer the question in Exercise 1.
// Program Pres demonstrates the precedence of operators.
#include <iostream>
using namespace std;
int main ()
{
cout << fixed << showpoint;
cout << 4 + 3 * 5 << endl;
cout << (4 + 3) * 5 <<endl;
cout << 4 * 5 % 3 + 2 << endl;
cout << (4 * (5 / 3) + 2) << endl;
return 0;
}
Exercise 1: Show what is written by each of the output statements.
78828_CH03_DaleLab 5/22/09 1:49 PM Page 51
Examine the following program carefully and then answer the questions in
Exercises 2 and 3.
// Program Format demonstrates the use of fieldwidth specifications.
#include <iostream>
#include <iomanip>
using namespace std;
const int INT_NUMBER = 1066;
const float FLT_NUMBER = 3.14159;
main ()
{
float fltValue;
int intValue;
cout << fixed << showpoint;
intValue = INT_NUMBER + FLT_NUMBER;
fltValue = float(INT_NUMBER) + FLT_NUMBER;
cout << INT_NUMBER << endl;
cout << intValue << endl;
cout << setw(10) << intValue;
cout << setw(10) << intValue << intValue /10 << endl;
cout << setw(10) << fltValue << endl;
cout << setprecision(10) << fltValue << endl;
cout << setw(10) << setprecision(3) << fltValue
<< endl;
cout << fltValue << endl;
cout << intValue << setw(3) << intValue << setw(7)
<< intValue << endl;
return 0;
}
Exercise 2: Show what is written by each of the output statements.
Exercise 3: Circle a statement that contains type conversion, and underline a
statement that contains type coercion.
52 | Chapter 3
78828_CH03_DaleLab 5/22/09 1:49 PM Page 52

Get A Laboratory Course in C++, 5th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.