Computer Programming C++ Sheet 1 & 2 Answer
Computer Programming C++ Sheet 1 & 2 Answer
o Psuedocode: -
▪ Start
▪ Get width & length of both the house and the land
▪ end
o Code: -
#include <iostream>
cout << "Now for the width and length of the land respectively. \n";
AH = WH * LH;
AL = WL * LL;
Grass = AL - AH;
cout << "The area of the grass to be bought is equal to " << Grass
<< " Square Meters";
return 0;
}
o Screenshot: -
Sheet 2
(Arithmetic Operators)
• X= a+ (b/c)
• Y = (((a+b) * 0.324)/2)
• Z = 1 + (3/(a+b))-d
(3) Evaluate the following:
• floor (15.8) 15
b) floor (15.8 + 0.5 ) 16
c) ceil ( - 7.2 ) * pow (4.0, 2.0 ) -8 * 16 = -128
d) sqrt ( floor ( fabs ( -16.8 ) ) ) sqrt ( floor ( -16 ) ) sqrt(-16)
no value (error)
e) log10 ( 1000.0 ) 3
(4) A Manufacturer wishes to determine the cost of producing an open-top
cylindrical container. The surface area of the container is the sum of the area
of the circular base plus the area of the outside (the circumference of the base
times the length of the container). Write a program to take the radius of the
base, the height of the container, and the cost per square cm of the material.
Calculate and display the cost of each cylindrical container.
❖ Pseudocode: -
o Start
o Get radius, height, and cost per square cm
o Calculate area of circular base and surface area of the outside
of the cylindrical container.
o Calculate cost
o Print cost
o End
❖ Code: -
#include <iostream>
#include <math.h>
float Radius, Height, CostPerSquareCM
,BaseArea,SurfaceArea,TotalArea, FinalCost;
using namespace std;
int main()
{
cout << "Please enter the Radius, Height, Cost Per Square cm
respectively. Measurements are calculated in cm and cost in $. \n";
SurfaceArea = 2*M_PI*Radius*Height;
cout << "The cost for this container is $" << FinalCost;
return 0;
}
❖ Screenshot: -
(5) Write a program that asks a student for his grade (out of 100) in 3 exams and
then print out his final grade (out of 100), given that the weight of the first
❖ Pseudocode: -
o Start
o Get grades
int main()
{
cout << "Please enter first, second, and third subject's
grade respectively. \n";
cout << "The final grade is " << FinalGrade << " out of
100.";
return 0;
}
❖ Screenshot: -
(6) Write a computer program that computes the duration of a
projectile’s flight and its height when it reaches the target.
DISTANCE
Time = , Height = ( (velocity . sin(θ) . time) – (g . time2 ))/2
velocity.cos(θ )
Try your program with inputs: angle (θ) =0.3 radians, velocity =
800 m/sec, and distance = 11000 m.
o Pseudocode: -
▪ Start
▪ Get Angle, Velocity, and distance
▪ Calculate time
▪ Calculate Height
▪ Print time and height
▪ End
o Code: -
#include <iostream>
#include <math.h>
float Angle,Distance,Time,Height,Velocity;
int g = 10;
using namespace std;
int main()
{
cout << "Please enter Angle in radians,Velocity in m/s, and
distance in meters respectively. \n";
Time = Distance/(Velocity*cos(Angle));
cout << "The duration of the projectile's flight is " << Time << "
Seconds and the height at the moment of reaching the target is " <<
Height <<" Meters";
return 0;
}
o Screenshot: -
(7) Write a C++ program that takes in three numbers x 1, x2, x3 and
returns the max number.
❖ Pseudocode: -
▪ Start
▪ Get the three numbers
▪ Use If else conditional to find which number is
highest
▪ Print highest number
▪ End
❖ Code: -
#include <iostream>
float a,b,c,Highest;
using namespace std;
int main()
{
cout << "Please enter the three numbers. \n";
cin >> a >> b >> c;
• Pseudocode: -
▪ Start
▪ Get number of elements (n)
▪ Get elements from user by looping from 0 to n,
▪ Compare every element with each other
▪ Copying largest number to “largest” variable
▪ Copying Smallest number to “smallest” variable
▪ Print largest and smallest value
▪ End
• Screenshot: -
• Code: -
#include <iostream>
using namespace std;
int main(){
int n, largest,smallest;
int num[100];
cout<<"Enter number of elements you want to enter: ";
cin>>n;
largest = num[0];
for(int i = 1;i < n; i++) {
if(largest < num[i])
largest = num[i];
smallest = num[0];
if(smallest > num[i])
smallest = num[i];
}
cout<<"Largest element in array is: "<<largest<< " and
the smallest element is " << smallest;
return 0;
}