0% found this document useful (0 votes)
54 views

Lab Assignment 1 - Sequential Control Structure - ANSWER SCHEME

The document contains 6 code examples demonstrating the use of sequential control structures in C++ programs. The examples show how to: 1) Convert between hours, minutes, seconds and total seconds. 2) Convert between pounds and kilograms/grams. 3) Calculate sum, difference, average and absolute difference of two numbers. 4) Calculate area, perimeter and diagonal of a rectangle. 5) Calculate the average of 5 test scores. 6) Calculate gross, paid distribution and net profit from movie ticket sales.

Uploaded by

hsna mardhiah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Lab Assignment 1 - Sequential Control Structure - ANSWER SCHEME

The document contains 6 code examples demonstrating the use of sequential control structures in C++ programs. The examples show how to: 1) Convert between hours, minutes, seconds and total seconds. 2) Convert between pounds and kilograms/grams. 3) Calculate sum, difference, average and absolute difference of two numbers. 4) Calculate area, perimeter and diagonal of a rectangle. 5) Calculate the average of 5 test scores. 6) Calculate gross, paid distribution and net profit from movie ticket sales.

Uploaded by

hsna mardhiah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSC126 – FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING

ANSWER SCHEME
LAB ASSIGNMENT 1 – SEQUENTIAL CONTROL STRUCTURE

1.1
#include<iostream>
using namespace std;

int main()
{
int hours, minutes, seconds;

cout<<"Enter hours, minutes and seconds: ";


cin >> hours >> minutes >> seconds;

seconds = hours * 3600 + minutes * 360 + seconds;

cout << "Total second: " << seconds;

return 0;
}

1.2
#include<iostream>
using namespace std;

int main()
{
double pound, kg, gram;

cout << "Enter weight in pound: ";


cin >> pound;

kg = 0.453592 * pound;
gram = 453.59237 * pound;

cout<<"In Kilogram: " << kg << " kg"<<endl;


cout<<"In Gram: " << gram << " gram"<<endl;
}

1.3
#include<iostream>
#include<math.h>
using namespace std;

int main()
{
double num1,num2,sum,difference,average,distance;

cout << "Enter number 1 and number 2: ";


cin >> num1 >> num2;

sum = num1 + num2;


difference = num1 - num2;
average = (num1+num2)/2;
distance = abs(difference);

cout <<"The sum: " << sum <<endl;


cout <<"The difference: " << difference <<endl;
cout <<"The average: " << average <<endl;
cout <<"The distance: " << distance <<endl;
}

1.4
#include<iostream>
#include<math.h>
using namespace std;

int main()
{
double width, height, area, perimeter, diagonal;

cout << "Enter width and height: ";


cin >> width >> height;

area = height * width;


perimeter = 2*height + 2*width;
diagonal = sqrt(pow(height,2)+pow(width,2));

cout<< "Area: " << area <<endl;


cout<< "Perimeter: " << perimeter <<endl;
cout<< "Diagonal: " << diagonal <<endl;
}

1.5
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
double mark1, mark2, mark3, mark4, mark5, average;

cout << "Enter 5 tests score: ";


cin >> mark1 >> mark2 >> mark3 >> mark4 >> mark5;

average = (mark1 + mark2 + mark3 + mark4 + mark5)/5.0;

cout<< setprecision(1) << fixed;


cout <<"The average tset marks: " <<setprecision(1)<<average;

}
1.6

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

int main()
{
string movName;
int noAdult, noChild;
double grossProfit, paidDist, netProfit;

cout<<"Enter Movie Name: ";


cin>>ws;
getline(cin,movName);
cout<< "Enter Adult Ticket Sold: ";
cin >> noAdult;
cout<< "Enter Child Ticket Sold: ";
cin >> noChild;

grossProfit = noAdult*6.00 + noChild*3.00;


paidDist = grossProfit * 0.80;
netProfit = grossProfit - paidDist;

cout<< setprecision(2) << fixed;

cout<< "\n\n\nMovie Name\t\t\t: " <<movName <<endl;


cout<< "Adult Ticket Sold\t\t: "<<noAdult <<endl;
cout<< "Child Ticket Sold\t\t: "<<noChild <<endl;
cout<< "Grost Box Office Profit\t\t: - RM " <<setprecision(2)
<<paidDist <<endl;
cout<< "Net Box Office Profit\t\t: RM " <<setprecision(2)
<<netProfit <<endl;

You might also like