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

请先
  • 游客 下载了资源 可爱小猫和万圣节系列3d打印图纸
  • 游客 下载了资源 国开(成都)《电工电子技术#》形考任务3辅导资料
  • 游客 下载了资源 被遗忘的魔杖持有者3d打印图纸stl/3mf文件档案
  • 游客 下载了资源 可爱的小鸡3d打印图纸
  • 游客 下载了资源 天猫淘宝电商主图促销活动折扣边框装修美工背景模板PSD设计素材
  • 游客 下载了资源 坐立不安的僵尸钥匙扣3d打印图纸
  • 游客 下载了资源 花好月圆流量数据
  • 游客 下载了资源 挂钟3d打印图纸
  • 游客 下载了资源 Furbkin - Furby 南瓜万圣节表情包3d打印图纸stl文件档案
  • 游客 下载了资源 游戏男孩形状的盒子3d打印图纸stl
  • 游客 下载了资源 LED 灯月亮灯 001 [高分辨率] 3d打印图纸stl文件
  • 游客 下载了资源 新中式国风植物花卉剪影插画艺术海报茶叶包装AI矢量设计素材PSD
  • 游客 下载了资源 潮流酸性逆反差优雅厚重像素无衬线中英文字体安装包字库PS素材AI
  • 游客 下载了资源 国家开放大学奖学金申请表
  • u******* 签到打卡,获得1元奖励
  • u******* 签到打卡,获得1元奖励
点击浏览器地址栏的⭐图标收藏本页
国开电大期末考试精准答案,原原本本的一题一题复刻,不需要自己一题一题找的那种,需要扫码加微信(也兼代写作业)
显示验证码

社交账号快速登录

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