You are on page 1of 11

Lab Sheet 3

3.1
EXERCISE 1
// This program will read in the quantity of a particular item and its price.
// It will then print out the total price.
// The input will come from the keyboard and the output will go to
// the screen.
// Suhayl Azmin Azaruddin
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int quantity; // contains the amount of items purchased
float itemPrice; // contains the price of each item
float totalBill; // contains the total bill.

cout << setprecision(2) << fixed << showpoint; // formatted output


cout << "Please input the number of items bought" << endl;
cin >> quantity;

cout << "Please input the Price of each item" << endl;
cin >> itemPrice;
totalBill = quantity * itemPrice;

cout << "The total bill is " << totalBill << endl;

return 0;
}
EXERCISE 2
// This program will read in the quantity of a particular item and its price.
// It will then print out the total price.
// The input will come from the keyboard and the output will go to
// the screen.
// Suhayl Azmin Azaruddin
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int quantity; // contains the amount of items purchased
float itemPrice; // contains the price of each item
float totalBill; // contains the total bill.

cout << setprecision(2) << showpoint; // formatted output


cout << "Please input the number of items bought" << endl;
cin >> quantity;

cout << "Please input the Price of each item" << endl;
cin >> itemPrice;
totalBill = quantity * itemPrice;

cout << "The total bill is " << totalBill << endl;

return 0;
}

The fixed attribute keep the value represented with exactly as many digits in the decimal part
EXERCISE 3
// This program will read in the quantity of a particular item and its price.
// It will then print out the total price.
// The input will come from the keyboard and the output will go to
// the screen.
// Suhayl Azmin Azaruddin
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int quantity; // contains the amount of items purchased
float itemPrice; // contains the price of each item
float totalBill; // contains the total bill.

cout << setprecision(4) << fixed << showpoint; // formatted output


cout << "Please input the number of items bought" << endl;
cin >> quantity;

cout << "Please input the Price of each item" << endl;
cin >> itemPrice;
totalBill = quantity * itemPrice;

cout << "The total bill is " << totalBill << endl;

return 0;
}
3.2
EXERCISE 1
// This program will bring in two prices and two quantities of items
// from the keyboard and print those numbers in a formatted chart.
// Suhayl Azmin Bin Azaruddin
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float price1, price2;
int quantity1, quantity2;
cout << setprecision(2) << fixed << showpoint;
cout << "Please input the price and quantity of the first item" << endl;
cin >> price1; cin >> quantity1;

cout << "Please input the price and quantity of the second item" << endl;
cin >> price2; cin >> quantity2;

cout << setw(15) << "PRICE" << setw(12) << "QUANTITY\n\n";


cout << setw(15) << price1 << setw(12) << quantity1 << endl;
cout << setw(15) << price2 << setw(12) << quantity2 << endl;
return 0;
}
3.3
EXERCISE 1
// This program will input the value of two sides of a right triangle and then
// determine the size of the hypotenuse.
// Suhayl Azmin Azaruddin
#include <iostream>
#include <cmath> // needed for math functions like sqrt()
using namespace std;
int main()
{
float a, b; // the smaller two sides of the triangle
float hyp; // the hypotenuse calculated by the program
cout << "Please input the value of the two sides" << endl;
cin >> a >> b;

hyp = sqrt((pow(a, 2)) + (pow(b, 2)));


// Fill in the assignment statement that determines the hypotenuse

cout << "The sides of the right triangle are " << a << " and " << b << endl;
cout << "The hypotenuse is " << hyp << endl;
return 0;
}
EXERCISE 2

// This program will input the value of two sides of a right triangle and then
// determine the size of the hypotenuse.
// Suhayl Azmin Azaruddin
#include <iostream>
#include <cmath> // needed for math functions like sqrt()
#include <iomanip>
using namespace std;
int main()
{
float a, b; // the smaller two sides of the triangle
float hyp; // the hypotenuse calculated by the program
cout << "Please input the value of the two sides" << endl;
cin >> a >> b;

hyp = sqrt((pow(a, 2)) + (pow(b, 2)));


// Fill in the assignment statement that determines the hypotenuse

cout << "The sides of the right triangle are " << a << " and " << b << endl;
cout << setprecision(2) << fixed << showpoint;
cout << "The hypotenuse is " << hyp << endl;
return 0;
}
3.4
EXERCISE 1
// This program will determine the batting average of a player.
// The number of hits and at bats are set internally in the program.
// Suhayl Azmin Azaruddin
#include <iostream>
using namespace std;
const int AT_BAT = 421;
const int HITS = 123;
int main()
{
int batAvg;
batAvg = HITS / AT_BAT; // an assignment statement

cout << "The batting average is " << batAvg << endl; // output the result

return 0;
}

EXERCISE 2
// This program will determine the batting average of a player.
// The number of hits and at bats are set internally in the program.
// Suhayl Azmin Azaruddin
#include <iostream>
using namespace std;
const int AT_BAT = 421;
const int HITS = 123;
int main()
{
float batAvg;
batAvg = HITS / AT_BAT; // an assignment statement

cout << "The batting average is " << batAvg << endl; // output the result

return 0;
}

No, the result is still the same.


EXERCISE 3
// This program will determine the batting average of a player.
// The number of hits and at bats are set internally in the program.
// PLACE YOUR NAME HERE
#include <iostream>
using namespace std;
const int AT_BAT = 421;
const int HITS = 123;
int main()
{
float batAvg;
batAvg = (float) HITS / (float) AT_BAT; // an assignment statement

cout << "The batting average is " << batAvg << endl; // output the result

return 0;
}

3.5
OPTION 1
// Suhayl Azmin Azaruddin
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setprecision(2) << fixed << showpoint;
double FG, GG, TG, AVG;
cout << "Please input the first grade\n";
cin >> FG;
cout << "Please input the second grade\n";
cin >> GG;
cout << "Please input the third grade\n";
cin >> TG;

AVG = (FG + GG + TG) / 3;


cout << "The Average of the three grades is " << AVG << endl;

return 0;
}
OPTION 2
// Suhayl Azmin Azaruddin
#include <iostream>
#include<iomanip>
using namespace std;
const float PPCAC = 85.00; // PPAC = Price per chair American Colonial
const float PPCM = 57.50; // PPCM = Price per chair Modern
const float PPCFC = 127.75; // PPCFC = Price per chair French Colonial
int main()
{
double QAC, QM, QFC, TAC,TM, TFC, Total;
// QAC=Quantity of American Colonial, QM=Quantity of Moderm, QFC=Quantity of
French Colonial,
// TAC=Total of American Colonial, TM=Total of Modern, TFC=Total of French
Colonial.
cout << setprecision(2) << fixed << showpoint;
cout << "Please input the number of American Colonial chairs sold \n";
cin >> QAC;

cout << "Please input the number of Modern chairs sold \n";
cin >> QM;

cout << "Please input the number of French Classical chairs sold \n";
cin >> QFC;

TAC = QAC * PPCAC;


TM = QM * PPCM;
TFC = QFC * PPCFC;
Total = (TAC + TM+ TFC);

cout << "The total sales of American Colonial chairs $" << TAC <<endl;
cout << "The total sales of Modern chairs $" << TM << endl;
cout << "The total sales of French Classical chairs $" << TFC << endl;
cout << "The total sales of all chairs is $" << Total << endl;

return 0;
}
OPTION 3
//Suhayl Azmin Azaruddin
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double tsm, stp, ltp, stm, ltm;
// tsm=total sale for the month, stp=state tax percentage, ltp=local text
percentage.
// stm=state tax for the month, ltm=local tax for the month.
cout << setprecision(2) << fixed << showpoint;
cout << "Please input the total sales for the month\n";
cin >> tsm;

cout << "Please input the state tax percentage in decimal form ( .02 for 2%)\n";
cin >> stp;

cout << "Please input the local tax percentage in decimal form ( .02 for 2%)\n";
cin >> ltp;

stm = tsm * stp;


ltm = tsm * ltp;

cout << "The total sales for the month is $" << tsm << endl;
cout << "The Local Tax for the month is $" << stm << endl;
cout << "The Local tax for the month is $" << ltm << endl;

return 0;

You might also like