ITP Assignment1,2
ITP Assignment1,2
1: PROGRAMMING EXERCISES
1. Write C++ expressions for the following expressions.
2.A milk carton can hold 3.78 liters of milk. Each morning, a dairy farm ships
cartons of milk to a local grocery store. The cost of producing one liter of milk is
$0.38, and the profit of each carton of milk is $0.27. Write a program that does
the following:
a. Prompts the user to enter the total amount of milk produced in the morning.
b. Outputs the number of milk cartons needed to hold milk. (Round to int.)
c. Outputs the cost of producing milk.
d. Outputs the profit for producing milk
3.
4.
5.
6.
7. One metric ton is approximately 2205 pounds. Write a program that prompts
the user to input the amount of rice, in pounds, in a bag. The program outputs
the number of bags needed to store one metric ton of rice.
8. Cindy uses the services of a brokerage firm to buy and sell stocks. The firm
charges 1.5% service charges on the total amount for each transaction buy or
sell. When Cindy sells stocks, she would like to know if she gained or lost on a
particular investment. Write a program that allows Cindy to input the
number of shares sold, the purchase price of each share, and the selling price
of each share. The program outputs the amount invested, the total service
charges, amount gained or lost, and the amount received after selling the
stock.
a. cout << 25 / 3 ;
b. cout << 20 - 12 / 4 * 2;
c. cout << 32 % 7;
d. cout << 3 - 5 % 7;
e. cout << 19.0 / 4;
f. cout << 28 - 5 / 2.0;
g. cout << 17 + 5 % 2 – 3;
h. cout << 15.0 + 3.0 * 2.0 / 5.0;
a. cout << (x + z) % y ;
b. cout << (x + y) % w;
c. cout << (y + w) % x;
d. cout << (x + y) * w;
e. cout << (x % y) % z;
f. cout << (y % z) % x;
g. cout << (x * z) % y;
h. cout << ((x * y) * w) * z;
3. Suppose a, b, and sum are int variables and c is a double variable. What value
is assigned to each variable after each statement executes? Suppose a = 3, b = 5
and c = 14.1.
4. Suppose x, y, and z are int variables and w and t are double variables. What
value is assigned to each of these variables after the last statement executes?
x = 17; y = 15; x = x + y / 4;
z = x % 3 + 4;
w = 17 / 3 + 6.5;
t = x / 4.0 + 15 % 4 - 3.5;
5. Suppose x and y are int variables and ch is a char variable. Consider the
following input: 5 28 36 What value (if any) is assigned to x, y, and ch after each
of the following statements executes?
cin >> x >> y >> ch;
cin >> ch >> x >> y;
cin >> x >> ch >> y;
6. Given the input: 46 A 49 and the C++ code:
int x = 10, y = 18; char z = '*';
cin>> x >> y >> z;
cout<< x << " " << y << " " << z << endl;
What is the output?
7. Suppose that x and y are int variables, z is a double variable, and ch is a char
variable. Suppose the input statement is: cin >> x >> y >> ch >> z; What values, if
any, are stored in x, y, z, and ch if the input is:
a. 35 62.78
b. 86 32A 92.6
c. 12.45A 32
8. What would be the output of the following statements?
9.