LAB 3 C ++.docx

FAKULTI SAINS KOMPUTER DAN TEKNOLOGI MAKLUMAT JABATAN TEKNOLOGI KOMUNIKASI DAN RANGKAIAN

C++ PROGRAMMING

SKR3306

LAB ASSIGNMENT 3 REPORT

NAME : MUHAMMAD ABDUL SHAHID BIN HASRI
MATRIC NO. : 194086
Lab 3.1: Working with the cin Statement

Task 1 ( Coding )

// MUHAMMAD ABDUL SHAHID BIN HASRI

#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 put price of each item : ” << endl << “RM ” ;

cin >> itemPrice ;

totalBill = quantity*itemPrice ;

cout << “The total bill is :RM ” << totalBill ;

return 0; }

Task 2 ( Output )

Task 3 ( OUTPUT )

The function of fixed is represented the result as many digits in the decimal part as specified by the precision field precision) and with no exponent part.

Task 4 ( OUTPUT )

The function of setprecision() is sets the decimal precision to be used to format floating-point values on output operations. If we want 2 decimal place, just put 2 in the bracket, example

setprecision(2);

Task 5 ( OUTPUT )

Lab 3.2: Formatting Output

CODING AND OUTPUT

#include <iostream> #include <iomanip>

#include <stdio.h>// Fill in the code to bring in the library for #include <string>

// formatted output.

using namespace std;

int main() {

float price1, price2; // The price of 2 items

int quantity1, quantity2; // The quantity of 2 items cout << setprecision(2) << fixed << showpoint;

cout << “Please input the price and quantity of the first item” << endl; cin >> price1 >> quantity1 ;

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

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

cout << setw(15) << price1 << setw(7) << quantity1 << endl ;

cout << setw(15) << price2 << setw(7) << quantity2 ;

return 0;

}

Lab 3.3: Arithmetic Operations Math

Task 2 ( Coding & Output )

#include <iostream>

#include <iomanip>

#include <cmath> // needed for math functions like sqrt() using namespace std;

int main() {

int a,b,c; // the smaller two sides of the triangle
float hyp; // the hypotenuse calculated by the program

cout << setprecision(5) << fixed << showpoint;

cout << “Please input the value of the two sides” << endl;

cin >> a >> b;

c = pow(a,2)+ pow(b,2) ;

hyp = sqrt(c);

cout << “The sides of the right triangle are ” << a << ” and ” << b << endl; cout << “The hypotenuse is ” << hyp << endl;

return 0;

}

Task 3 ( Coding & Output )

#include <cmath> // needed for math functions like sqrt()

using namespace std;

int main() {

int a,b,c; // the smaller two sides of the triangle

float hyp; // the hypotenuse calculated by the program

cout << setprecision(2) << fixed << showpoint;

cout << “Please input the value of the two sides” << endl;

cin >> a >> b;

c = pow(a,2)+ pow(b,2) ;

hyp = sqrt(c);

cout << “The sides of the right triangle are ” << a << ” and ” << b << endl; cout << “The hypotenuse is ” << hyp << endl;

return 0; }

Lab 3.4: Working with Type Casting

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;

using namespace std;

const float AT_BAT = 421;

const float 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;

}

Does changing the data type of batavg from int to float solve the problem? NO

using namespace std;

const int AT_BAT = 421;

const int HITS = 123;

int main() {

float batAvg;

batAvg = static_cast<float>(HITS) / static_cast <float>(AT_BAT) ;

cout << “The batting average is ” << batAvg << endl; // output the result return 0;

}

Lab 3.5: Reading and Writing to a File

TASK 2 (OUTPUT)

TASK 3 ( CODING & OUTPUT )

#include <fstream> #include <iomanip> using namespace std;

int main() {

ifstream dataIn;

ofstream dataOut;

int quantity;

float itemPrice;

float totalBill;

dataIn.open(“transaction.dat”);

dataOut.open(“bill.out”);

dataOut << setprecision(2) << fixed << showpoint; dataIn >> quantity >> itemPrice;

totalBill = quantity*itemPrice;

dataOut << The total bill is << totalBill;

return 0;

}

Lab 3.6: Student Generated Code Exercises

Option 1

#include <iostream> #include <iomanip> #include <string>

using namespace std;

int main() {

float grade1’ grade2’ grade3;

float averageGrade;

cout << setprecision (2) << fixed << showpoint;

cout << “Please input the first grade” << endl;

cin >> grade1;

cout << “Please input the second grade” << endl;

cin >> grade2;

cout << “Please input the third grade” << endl;

cin >> grade3;

averageGrade = (grade1 + grade2 + grade3)/ 3;

cout << “The average of the three grades is ” << averageGrade << endl;

return 0;

}

OPTION 2

#include <iostream> #include <iomanip> #include <string>

using namespace std;

int main() {

int chair1, chair2, chair3;

float totalSales1, totalSales2, totalSales3, totalAll;

cout << setprecision (2) << fixed << showpoint;

cout << “Please input the number of American Colonial chairs sold” << endl; cin >> chair1;

cout << “Please input the number of Modern chairs sold” << endl;

cin >> chair2;

cout << “Please input the number of French Classical chairs sold” << endl; cin >> chair3;

totalSales1 = chair1 * 85.00;

totalSales2 = chair2 * 57.50;

totalSales3 = chair3 * 127.75;

totalAll = totalSales1 + totalSales2 + totalSales3;

cout << “The total sales of American Colonial chairs $” << totalSales1 << endl; cout << “The total sales of Modern chairs $” << totalSales2 << endl;

cout << “The total sales of French Classical chairs $” << totalSales3 << endl; cout << “The total sales of all chairs $” << totalAll << endl;

return 0; }

OPTION 3

#include <iostream> #include <iomanip> #include <string>

using namespace std;

int main() {

float totalSales, stateTax, localTax;

float totalStateTax, totalLocalTax;

cout << setprecision (2) << fixed << showpoint;

cout << “Please input the total sales for the month” << endl;

cin >> totalSales;

cout << “Please input the state tax percentage in decimal form (.02 for 2%)” << endl; cin >> stateTax;

cout << “Please input the local tax percentage in decimal form (.02 for 2%)” << endl; cin >> localTax;

totalStateTax = totalSales * stateTax;

totalLocalTax = totalSales * localTax;

cout << “The total sales of the month is $” << totalSales << endl;

cout << “The state tax for the month is $” << totalStateTax << endl;

cout << “The local tax for the month is $” << totalLocalTax << endl;

return 0; }

 

资源下载
下载价格1.4 马来币MYR(2 元)
点点赞赏,手留余香 给TA打赏

AI创作

评论0

请先
支持多种货币
支持多种货币付款,满足您的付款需求
7天无忧退换
安心无忧购物,售后有保障
专业客服服务
百名资深客服7*24h在线服务
发货超时赔付
交易成功极速发货,专业水准保证时效性

站点公告

开放大学课程作业代写,有需要扫码加微信

显示验证码

社交账号快速登录

微信扫一扫关注
扫码关注后会自动登录