BasicElementsOfC++ - Tutorial Problems

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Tutorial Problems

1. Write a C++ program that displays the results of the expressions 3.0 * 5.0, 7.1 * 8.3 - 2.2,
and 3.2 / (6.1 * 5). Calculate the value of these expressions manually to verify that the displayed
values are correct.

2. Write a C++ program that displays the results of the expressions 15 / 4, 15 % 4, and 5 * 3 - (6
* 4). Calculate the value of these expressions manually to verify that the displayed values are correct.

3. State whether the following variable names are valid. If they are invalid, state the reason.
prod_a c1234 abcd _c3 12345
newamp watts $total new$al
a1b2c3d4 9ab6 sum.of average
volts1 finvolt

4. (a) Write a declaration statement to declare that the variable count will be used to store an
integer.
(b) Write a declaration statement to declare that the variable volt will be used to store a
floating-point number.
(c) Write a declaration statement to declare that the variable power will be used to store a
double-precision number.
(d) Write a declaration statement to declare that the variable keychar will be used to store a
character.

5. Write declaration statements for the following variables:


a. num1, num2, and num3 used to store integer number
b. amps1, amps2, amps3, and amps4 used to store double-precision numbers
c. volts1, volts2, and volts3 used to store double-precision numbers
d. codeA, codeB, codeC, codeD, and codeE used to store characters

6. (Electrical Eng.) a. Write, compile, and execute a C++ program to calculate the resistance of a
series circuit consisting of twelve 56-ohm, twenty 39-ohm, thirty-two 27-ohm, and twenty-
seven 15-ohm resistors. The total resistance of a series circuit is the sum of all individual
resistances. Your program should produce the following display (replacing the xxxx with the
actual resistance value your program calculates):
The total resistance, in ohms, is xxxx
b. Manually check the values computed by your program. After verifying that your program
is working correctly, modify it to calculate the resistance of a series circuit consisting of
seventeen 39-ohm resistors, nineteen 27-ohm resistors, and forty-two 15-ohm resistors.

7. (Thermodynamics) a. Design, write, compile, and execute a program that determines the
work performed by a piston engine providing a force of 1000 N over a distance of 15
centimeters. The following formula is used to determine the work, W, performed:
W=Fd
F is the force provided by the piston in Newtons.
d is the distance the piston moves in meters.
b. Manually check the values computed by your program. After verifying that your program
is working correctly, modify it to determine the work performed by six pistons, each
providing a force of 1500 N over a distance of 20 centimeters.

8. (Civil Eng.) a. Design, write, compile, and execute a program that determines the stress on a
steel I-beam having a rectangular moment of inertia of 21.4 in4, and a height of 6 inches
when a load of 700 lbs is placed 8 feet from the fixed end. The stress placed on the fixed end
of a symmetrical steel I-beam, as shown in Figure 2.16, can be determined by this formula:

S is the stress in lbs/in2.


L is the weight, in lbs, of the load placed on the beam.
I is the beam’s rectangular moment of inertia in units of in4.
d is the distance in inches the load is placed from the fixed end of the beam (technically
referred to as the “moment arm”).
c is one-half the height in inches of a symmetrical beam.

You might also like