You are on page 1of 9

Tutorial 2

Assignment Statement
1. (Practice) Write an assignment statement to calculate the circumference of a circle having a
radius of 3.3 inches. The formula for determining the circumference, c, of a circle is c = 2 π r,
where r is the radius and π equals 3.1416.
i) c=2πr
ii) c=2xπxr
iii) c=2*π*r
iv) c=π*2*r
v) 2*π*r=c

Answer: iii and iv


2. (Debug) Determine and correct the errors in the following programs.

a. i) No semicolon at width, area, ii) not declaring all the variables. Iii) no return 0;
iv) length has no value – not initialized.
b. i) formula before inputting/initialization of any value – will produce syntax error
ii) no closed curly braces after return 0
c.) i) no using namespace std. ii) wrong statement assigning – area = length * width.
Iii) should be ‘ <<’ instead of ‘,’ at cout statement.
3. (Program) Write a C++ program to calculate the dollar amount contained in a piggybank.
The bank currently contains 12 half-dollars, 20 quarters, 32 dimes, 45 nickels, and 27 pennies.

Assuming half-dollars = rm 1, quarters = 50 sen, dimes = 20 sen, nickels = 10 sen,


pennies = 5 sen.
4. (Program) Write a C++ program to calculate the sum of the numbers from 1 to 100. The
formula for calculating this sum is sum = (n / 2) . (2 . a + (n - 1) . d), where n = number of
terms to be added, a = the first number, and d = the difference between each number and the
next number (d = 1).
Interactive Input
5. (Program) Write, compile, and run a C++ program that displays the following prompts:

After each prompt is displayed, your program should use a cin statement to accept data from
the keyboard for the displayed prompt. After the depth of the swimming pool is entered, your
program should calculate and display the volume of the pool. The volume should be calculated
with the formula volume = length × width × average depth and be displayed in an output
message.

6. (Program) Write, compile and run a C++ program that prompts the user to input their name, age, state
they live in, blood type, height and weight. Then, calculate the Body Mass Index of the user (BMI)
using the following formula BMI = weight(kg)/height (𝑚2 ). Display the results in following output:
Welcome, username!
Here is your detail:
Age:
Blood type:
BMI:
State:
Thank you!

7. (Program) The perimeter, approximate surface area, and approximate volume of an in-ground
pool are given by the following formulas:
Using these formulas as a basis, write a C++ program that accepts the length, width, and average
depth measurements, and then calculates the pool’s perimeter, volume, and underground surface
area.
Cmath Library
8. (Practice) Write C++ statements for the following:

a.) area = ( c * b * sin (a) ) / 2


b. c = sqrt ( a*a + b*b)
c. p = sqrt (abs (m-n) )
d. sum = (a * ( pow(r,n) – 1 ) )/ (r – 1)
e. b = pow(sin (x), 2) + pow(cos(x) , 2 )
9. (Program) Write, compile, and run a C++ program that calculates and returns the fourth root
of the number 81.0, which is 3. After verifying that your program works correctly, use it to
determine the fourth root of 1,728.896400.
10. (Program) Write, compile, and run a C++ program to calculate the distance between two
points with the coordinates (7, 12) and (3, 9). Use the fact that the distance between two points
with the coordinates (x1, y1) and (x2, y2) is given by this formula:

11. (Program) A model of worldwide population growth, in billions of people, since 2000 is given
by this formula:

Using this formula, write, compile, and run a C++ program to estimate the worldwide population
in the year 2012. Verify the result your program produces by calculating the answer manually,
and then use your program to estimate the world’s population in the year 2020.
12. (Program) If a 20-foot ladder is placed on the side of a building at a 85-degree angle, as shown in
Figure 3.9, the height at which the ladder touches the building can be calculated as
height = 20 × sin 85°.
Calculate this height by hand, and then write, compile, and run a C++ program that determines and
displays the value of the height. After verifying that your program works correctly, use it to determine
the height of a 25-foot ladder placed at an angle of 85 degrees.

You might also like