Name/Section: Roble, Pearl Borgy V.
/ CPE251B
Rating:
Date performed: 13/08/2025
Date submitted:
Laboratory Exercise No.4
EXPRESSIONS, TYPE CASTING, COERCION, FORMATTING, RANDOM
III. Lab Activities:
1. Assume that the following variables are defined:
int age;
double pay;
char section;
Write a single cin statement that will read input into each of these variables.
2. Complete the following table by writing the value of each expression in the
Value column according C++ language rules.
1. 28 / 4 - 2
28 / 4 = 7
7-2=5
Value: 5
2. 6 + 12 * 2 - 8
12 * 2 = 24
6 + 24 = 30
30 - 8 = 22
Value: 22
3. 4 + 8 * 2
8 * 2 = 16
4 + 16 = 20
Value: 20
4. 6 + 17 % 3 - 2
17 % 3 = 2 (remainder when 17 is divided by 3)
6+2=8
8-2=6
Value: 6
5. 2 + 22 * (9 - 7)
(9 - 7) = 2
22 * 2 = 44
2 + 44 = 46
Value: 46
6. (8 + 7) * 2
(8 + 7) = 15
15 * 2 = 30
Value: 30
7. (16 + 7) % 2 - 1
(16 + 7) = 23
23 % 2 = 1
1-1=0
Value: 0
8. 12 / (10 - 6)
(10 - 6) = 4
12 / 4 = 3
Value: 3
9. (19 - 3) * (2 + 2) / 4
(19 - 3) = 16
(2 + 2) = 4
16 * 4 = 64
64 / 4 = 16
Value: 16
3. Assume a program has the following variable definitions:
int units;
float mass;
double weight;
weight = mass * units;
Which automatic data type conversion will take place?
A.mass is demoted to an int, units remains an int, and the result of mass *
units is an int.
B.units is promoted to a float, mass remains a float, and the result of mass *
units is a float.
C.units is promoted to a float, mass remains a float, and the result of mass *
units is a double.
Answer: C.units is promoted to a float, mass remains a float, and the result of
mass * units is a double.
Because mixed-type arithmetic promotes to the higher-ranked type. And the
result is assigned to a double, it converts to double.
IV. Assessment:
1. Each of the following programs has some errors. Locate as many as you
can.
Program-1
using namespace std; void main ()
{
double number1, number2, sum; cout << "Enter a number: ";
cin << number1;
cout << "Enter another number: "; cin << number2;
number1 + number2 = sum;
cout "The sum of the two numbers is " << sum
}
Fixed Program:
Fixes Made:
1. Added #include <iostream> at the start.
2. Changed void main() to int main() (standard C++).
3. Changed cin << to cin >>.
4. Corrected number1 + number2 = sum; to sum = number1 + number2;
5. Added << after cout and ended with endl.
Program-2
#include <iostream> using namespace std; void main()
{
int number1, number2; float quotient;
cout << "Enter two numbers and I will divide\n"; cout << "the first by the
second
for you.\n";
cin >> number1, number2;
quotient = float<static_cast>(number1) / number2; cout << quotient
}
Fixes Made:
1. Added #include <iostream>
2. Changed cin >> number1, number2; to cin >> number1 >> number2;
3. Fixed incorrect float<static_cast> syntax to static_cast<float>(number1)
4. Added proper cout format with << and endl.
2. Average of Values to get the average of a series of values, you add the
values up and then divide the sum by the number of values. Write a program
that stores the following values in five different variables: 28, 32, 37, 24, and
33. The program should first calculate the sum of these five variables and
store the result in a separate variable named sum. Then, the program should
divide the sum variable by 5 to get the average. Display the average on the
screen.
Explanation:
The sum holds the total of the 5 numbers, we’ll use static_cast<double> to
ensure floating-point division. The output will display the result with decimal
places instead of cutting it off to a whole number.