0% found this document useful (1 vote)
641 views4 pages

CISP360 - Lab Lesson 03: Pre-Lab Writing Assignment

This document provides instructions for three C++ programming labs focused on input/output, arithmetic operations, and type casting. Lab 1 demonstrates reading input with cin and calculating totals. Lab 2 uses math functions like sqrt() to calculate the hypotenuse of a right triangle given the other two sides. Lab 3 calculates batting average by casting the result of an integer division to a float.

Uploaded by

JohnTH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
641 views4 pages

CISP360 - Lab Lesson 03: Pre-Lab Writing Assignment

This document provides instructions for three C++ programming labs focused on input/output, arithmetic operations, and type casting. Lab 1 demonstrates reading input with cin and calculating totals. Lab 2 uses math functions like sqrt() to calculate the hypotenuse of a right triangle given the other two sides. Lab 3 calculates batting average by casting the result of an integer division to a float.

Uploaded by

JohnTH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CISP360 Lab Lesson 03

As with previous labs, for each lab listed below, you must submit your files to the Dropbox: Lab Lesson 03 on D2L.

Pre-Lab Writing Assignment:


Fill in the blank and write out the entire question in your submitted lab document.
1.

What is the final value (in C++) of the following expression?


(5 - 16 / 2 * 3 + (3 + 2 / 2) - 5)

2.

How would the following expression be written in C++?


2x + 34

3.

Implicit conversion is also known as data type _________ .

4.

Explicit type conversion is also known as type _________.

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.

The << in a cout statement is called the _________ operator.

8.

The #include< _________ > is needed for formatted output.

9.

The '\n' is a special character that _________ .

Lab 03.1 Working with the cin Statement


Goal:
1.

To introduce reading input from the keyboard with cin.

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

amount of items purchased


price of each item
total bill.
<< showpoint; // formatted output
of items bought" << endl;

input statement to bring in the quantity.


prompt to ask for the price.
input statement to bring in the price of each item.
assignment statement to determine the total bill.
output statement to print total bill, and message

a) Follow the comments in the code to complete the program.


b) Test your program using input numbers 22 for the number of items bought and 10.98 or the price of each item.
Use the example dialog below to format your output statements:
Please input the number of items bought
22
Please input the price of each item
10.98
The total bill is $241.56

c) Once you have the program working, change the instruction:


cout << setprecision (2) << fixed << showpoint;
to
cout << setprecision(2) << showpoint;

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?

Lab 03.2 Arithmetic Operations and Math Functions


Goal:
1.
2.

To use the cmath library.


To use order of operations when building arithmetic expressions.

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>

// needed for math functions like sqrt()

using namespace std;


int main()
{
float a,b; // the smaller two sides of the triangle
float hyp; // the hypotenuse calculated by the program
cout << "Please input the value of the two sides" << endl;
cin >> a >> b;
// Fill in the assignment statement that determines the hypotenuse
cout << "The sides of the right triangle are " << a << " and " << b << endl;
cout << "The hypotenuse is " << hyp << endl;
return 0;
}

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.

Lab 03.3 Working with Type Casting


Goal:
1.

To introduce type casting.

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)

Run this program and record the results.

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?

You might also like