Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

Computer Programming C++ Sheet 1 & 2 Answer

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Computer Programming C++ Sheet 1 & 2 Answer

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Sheet 1

(Variable / Data Types)


❖ Can the following terms be used as an identifier? Why not?

Identifier: An identifier starts with a letter A to Z or a to z.


an underscore (_) followed by zero or more letters,
underscore, and digits (0 to 9). C++ does not allow
punctuation characters.

o a) void: No, because it’s a keyword reserved by the language.


(function)
o b) Max_Num: Yes.
o c) double: No, because it’s a keyword reserved by the
language. (data type)
o d) G: No, because it’s not long enough to be understandable.
o e) Sue’s: No, because it has punctuation in it.
o f) # insert: No, it’s a keyword reserved by the language.
(directive)
o g) Part20%: No, because C++ does not allow punctuation in
characters such as %.
o h) cout: No, it’s a keyword reserved by the language. (object)
❖ Indicate the data type of each literal :

❖ 14 integer ❖ 15.0 float ❖ ‘*’ char


❖ ‘$’ char ❖ -999 integer ❖ 0.123 float
❖ ‘R’ char ❖ “R” char ❖ “Hello” string
❖ 32e-4 float ❖ ‘-5’ char ❖ 0.0 float

❖ Show the output displayed by the following (assume exp = 5):


o a) cout<<”My name is Ali \n and I live in “;
o b) cout<<“Cairo, Egypt \n and I have “;

o c) cout<< exp <<“ years of programming experience. “;


❖ Write a program that takes the length and width of a piece of land,
and the length and width of a house to be built inside it and then
tell you how much grass you need to buy to cover the garden (in
square meters).

o Psuedocode: -

▪ Start

▪ Get width & length of both the house and the land

▪ Calculate each object’s area

▪ Subtract area of house from area of land

▪ Print area of remaining land to be covered with grass

(grass area) in square meters

▪ end
o Code: -

#include <iostream>

float WH, WL, LH , LL, AH , AL, Grass;


using namespace std;
int main()
{
cout << "Please enter the width and length of the house respectively.
\n";

cin >> WH >> LH ;

cout << "Now for the width and length of the land respectively. \n";

cin>> WL >> LL;

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)

(1) Evaluate the following expressions:


• 7 / 22  Evaluates to 0
• 22 / 7  Evaluates to 3
• 7 % 22  Evaluates to 7
• 22 % 7  Evaluates to 1

(2) Write the following as a valid C++ arithmetic expression

• 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";

cin >> Radius >> Height >> CostPerSquareCM ;

BaseArea = M_PI * pow(Radius, 2.0 );

SurfaceArea = 2*M_PI*Radius*Height;

TotalArea = BaseArea + SurfaceArea;

FinalCost = TotalArea * CostPerSquareCM;

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

exam is 30%, the second 30%, and the third 40%

❖ Pseudocode: -

o Start

o Get grades

o Calculate each grade according to its weight

o Calculate final grade

o Print final grade


❖ Code: -
#include <iostream>
float Grade1, Grade2, Grade3
,CalcGrade1,CalcGrade2,CalcGrade3,FinalGrade;
using namespace std;

int main()
{
cout << "Please enter first, second, and third subject's
grade respectively. \n";

cin >> Grade1 >> Grade2 >> Grade3;

CalcGrade1 = (Grade1 /100)*30;


CalcGrade2 = (Grade2 /100)*30;
CalcGrade3 = (Grade3 /100)*40;
FinalGrade = CalcGrade1 + CalcGrade2 + CalcGrade3;

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";

cin >> Angle >> Velocity >> Distance;

Time = Distance/(Velocity*cos(Angle));

Height = ( (Velocity * sin(Angle) * Time) - (g * pow(Time,2.0)


))/2;

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;

if(a>b && a>c){


Highest = a;
}
else if (b>a && b>c){
Highest = b;
}
else if (c>a && c>b){
Highest = c;
}
cout << "The highest number is " << Highest ;
return 0;
}
❖ Screenshot: -
(8) Write a C++ program that reads N integers and determines and prints the
largest and the smallest integer in the group

• 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;

for(int i = 0; i < n; i++) {


cout<<"Enter Element "<<(i+1)<< ": ";
cin>>num[i];
}

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;
}

You might also like