07 Practice Input Type Coercion Type Casting
07 Practice Input Type Coercion Type Casting
Task-1:
As mathematical historians have told the story, the German mathematician Carl Friedrich Gauss (1777-
1855) began to show his mathematical talent at a very early age. When he was in elementary school,
Gauss was asked by his teacher to compute the sum of the numbers between 1 and 100. Gauss is said
to have given the answer instantly: 5050. Write a program that computes the answer to the question
Gauss’s teacher posed: Make the program more generic by asking user about the starting and ending
number of the range and display the sum of the numbers in range.
Sum from 100 to 200 = 15150
Sum from 1 to 100 = 5050
Task-2:
A customer in a store is purchasing five items. The prices of the five items are:
Write a program that holds the prices of the five items in five variables. Display each items price, the
subtotal of the sale, the amount of sales tax, and the total. Assume the sales tax is 6%.
Task-3:
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.
Task-4:
Complete the following table by writing the value of each expression in the Value column according
C++ language rules.
Expression Value
28 / 4 – 2
6 + 12 * 2 – 8
4+8*2
6 + 17 % 3 – 2
2 + 22 * (9 - 7)
(8 + 7) * 2
(16 + 7) % 2 – 1
12 / (10 - 6)
(19 - 3) * (2 + 2) / 4
5%10%3
Task-5:
Assume a program has the following variable definitions:
int units;
float mass;
double weight;
and the following statement:
weight = mass * units;
PUCIT, University of the Punjab, Lahore, Pakistan. Page 1 of 4
© Fareed Ul Hassan Baig
CMP-140 Programming Fundamentals
BS SE/CS Fall 2018 Issue Date: 6-Mar-2019
Practice - 07
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.
Task-6:
Assume a program has the following variable definitions:
int a, b = 2;
float c = 4.2;
and the following statement:
a = b * c;
What value will be stored in a?
A. 8.4
B. 8
C. 0
D. None of the above
Task-7:
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
}
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;
//In your book following syntax is used
//quotient = float<static_cast>(number1) / number2;
//but in class initially we shall use C Style type cast
quotient = (float)number1 / number2;
cout << quotient
}
Task-8:
During each summer, John and Jessica grow vegetables in their back yard and buy seeds and fertilizer
from a local nursery. The nursery carries different types of vegetable fertilizers in various bag sizes.
When buying a particular fertilizer, they want to know the price of the fertilizer per pound and the cost
of fertilizing per square foot. The following program prompts the user to enter the size of the fertilizer
bag, in pounds, the cost of the bag, and the area, in square feet, that can be covered by the bag. The
program should output the desired result. However, the program contains logic errors. Find and correct
the logic errors so that the program works properly.
//Logic errors.
#include <iostream>
#include <iomanip>
using namespace std;
void main()
PUCIT, University of the Punjab, Lahore, Pakistan. Page 2 of 4
© Fareed Ul Hassan Baig
CMP-140 Programming Fundamentals
BS SE/CS Fall 2018 Issue Date: 6-Mar-2019
Practice - 07
{
double cost;
double area;
double bagSize;
cout << fixed << showpoint << setprecision(2);
cout << "Enter the amount of fertilizer, in pounds, "<< "in one bag: ";
cin >> bagSize;
cout << endl;
cout << "Enter the cost of the " << bagSize << " pound fertilizer bag: ";
cin >> cost;
cout << endl;
cout << "Enter the area, in square feet, that can be "<< "fertilized by one bag: ";
cin >> area;
cout << endl;
cout << "The cost of the fertilizer per pound is: $"<< bagSize / cost << endl;
cout << "The cost of fertilizing per square foot is: $"<< area / cost << endl;
}
Task-9:
Assume that qty and salesReps are both integers. Use a type cast expression to rewrite the following
statement so it will no longer perform integer division.
unitsEach = qty / salesReps;
Task-11: Currency
Write a program that will convert U.S. dollar amounts to Japanese yen and to Euros, storing the
conversion factors in the constants YEN_PER_DOLLAR and EUROS_PER_DOLLAR.
To get the most up-to-date exchange rates, search the Internet using the term currency exchange
rate. If you cannot find the most recent exchange rates, use the following:
1 Dollar = 83.14 Yen
1 Dollar = 0.7337 Euros
Format your currency amounts in fixed-point notation, with two decimal places of precision, and be
sure the decimal point is always displayed.
Rate is the monthly interest rate, which is the annual interest rate divided by 12.
(12% annual interest would be 1 percent monthly interest.) N is the number of payments and L is the
amount of the loan. Write a program that asks for these values and displays a report similar to:
Both the random numbers will be greater than zero and maximum 5 digits.
Sample Run:
*****Kangaroo Math Competition*****
247
+ 129
--------
Hey Kido! When you solve it in your mind à Press Enter key to verify your answer:
247
+ 129
--------
376