May 2017 - Week 10
May 2017 - Week 10
com/doc/tutorial/files/
TDB2073 Structured Programming & Database Systems
Lab/Tutorial Exercise 8 – Stream 1
Semester May 2017
Perform ALL tasks
1. Given below is a C++ program which interactively gets data from standard input
(keyboard) and displays output on standard output (screen). The program calculates and
print bills for customers of Patrick gas station. Whenever a customer wants to fill up his
gas tank, he will be asked for the gas amount to be filled in ringgit. He will also be asked
if he needs any assistance from pump attendant. If yes (code – 1) and his gas purchase
is below 50 ringgit, an additional of 50 sen will be added to the gas bill, otherwise there
will be no extra charge. The customer can make payment for his bill by cash (code -- 0)
or by card (code -- 1). If he opts for the second method, his printed bill will show an extra
5% service charge (of total).
[NOTE: the program stops when there is no more customer to input]
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double amt, total;
int assist, payment;
int count = 0;
char ans;
cout << "** PATRICK GAS STATION **" << endl;
do {
total = 0;
cout << "Customer " << ++count << ": " << endl;
cout << " Purchase amount RM: ";
cin >> amt;
cout << " Assistance (0-No, 1-Yes) : ";
cin >> assist;
cout << " Payment (0-Cash, 1-Card) : ";
cin >> payment;
if (assist == 0)
total = amt;
else {
if (amt < 50)
total = amt + 0.50;
else
total = amt;
1
}
if (payment == 1)
total = total + 0.05 * total;
cout << "=======================================" << endl;
cout << "TOTAL charge ==> RM " << fixed << setprecision(2) <<
total << endl;
cout << "=======================================" << endl << endl;
a. Copy and paste the code into your DevCpp editor [NOTE: you can get the softcopy of
the lab question from e-learning].
b. Compile and run the code
c. Add/modify/delete the necessary codes in order to make the program reads data
from input file, instead of the keyboard, and writes output into an output file, instead
of the screen.
2. Write a complete C++ program that reads data into an integer array named testvals
(size will be determined from input). The program includes two functions named
calc_avg() and calc_variance().
The calc_avg() function calculates and returns the average of the values stored in
array testvals. Function calc_variance() on the other hand, calculates and returns
the variance of the data. The formula to calculate variance is given in TABLE Q2.
The values returned from calc_avg() and calc_variance() should be displayed
using the cout statements in main().
2
TABLE Q2
Variance = ∑( Xi – XAvg )2 / N
Where;
∑ = Summation
Xi = Each value in testvals
XAvg = Average
N = The number of elements in testvals
[NOTE: The first integer value refers to the number of input sets (lines). For each input
set, there will be the number of elements in the array, followed by the data for each of the
array element. The output for each input set will be the average and variance of the
elements in the array].