CISP360 - Lab Lesson 03: Pre-Lab Writing Assignment
CISP360 - Lab Lesson 03: Pre-Lab Writing Assignment
As with previous labs, for each lab listed below, you must submit your files to the Dropbox: Lab Lesson 03 on D2L.
2.
3.
4.
5.
List the preprocessor directive that must be included for cin and cout to be used in a C++ program.
_________
6.
Blank spaces or unseen control characters in a data file are referred to as _________.
7.
8.
9.
Overview:
Copy and complete the program below, lab031.cpp, which will read in the quantity of a particular item and its price. It
will calculate and print out the total price.
// Add your program header here
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int quantity;
// contains the
float itemPrice; // contains the
float totalBill; // contains the
cout << setprecision(2) << fixed
cout << "Please input the number
// Fill in
// Fill in
// Fill in
// Fill in
// Fill in
return 0;
}
the
the
the
the
the
Rerun the program with the same data given in Exercise 1 b) above and record your results. What do you think the
fixed attribute in the cout statement does?
d) Now put the fixed attribute back in and change the instruction to make the precision 4. Rerun the program with
the same data given in b) and record your results. What do you think the setprecision( ) attribute in the cout
statement does?
The attribute showpoint forces all floating point output to show a decimal point even if the values are whole
numbers. In some environments this is done automatically.
e) Add the following directive to the program: #include <string> in the header. Alter the program so that the
program first asks for the name of the product (which can be read into a string object) so that the following
sample run of the program will appear.
Please input the name of the item
Milk
Please input the number of items bought
4
Please input the price of each item
1.97
The item that you bought is Milk
The total bill is $7.88
f)
Now altar the program, if you have not already done so, so that the name of an item could include a space within
its string.
Please input the name of the item
Chocolate Ice Cream
Please input the number of items bought
4
Please input the price of each item
1.97
The item that you bought is Chocolate Ice Cream
The total bill is $7.88
Post-Lab Questions:
1.
Explain the difference between your output in parts e) and f). How did you fix your program to read in input with
spaces?
Overview:
Copy and complete the program below, lab032.cpp, which will input the two sides of a right triangle and then
determine the size of the hypotenuse. How can this be implemented in C++? Hint: You will use two pre-defined math
functions (one of them twice) learned in this lesson. One of them will be inside the other.
// Add your program header here
#include <iostream>
#include <cmath>
a)
Fill in the missing statement so that the following sample run is implemented:
Please input the value of the two sides
9 3
The sides of the right triangle are 9 and 3
The hypotenuse is 9.48683
b) Alter the program so that the sample run now looks like the following:
Please input the value of the two sides
9 3
The sides of the right triangle are 9 and 3
The hypotenuse is 9.49
Note: This is not a trivial change. You must include another directive as well as use the formatted features
discussed in the earlier labs of this lesson. Notice that the change is made only to the value of the hypotenuse and
not to the values of 9 and 3.
Overview:
Copy and complete the program below, lab033.cpp, which will determine the batting average of a play. The number of
hits and at bats will be set internally in the program.
// Add your program header here
#include <iostream>
using namespace std;
const int AT_BAT = 421;
const int HITS = 123;
int main()
{
int batAvg;
batAvg = HITS / AT_BAT;
cout << "The batting average is " << batAvg << endl;
// an assignment statement
// output the result
return 0;
}
a)
b) There is a logic error in this program centering around data types. Does changing the data type of batavg from
int to float solve the problem? Make that change and run the program again and record the result.
c) Continue to work with this program until you get the correct result. The correct result should be 0.292162. Do not
change the data type of the two named constants. Instead, use a typecast to solve the problem.
Post-Lab Questions:
1) How did you determine the validity in your calculations for batting average?
2) What change did you make to get the correct results?