Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Labsheet 1- Introduction to C++

For each of the questions below, you are required to create a project, modify the main.cpp file
by removing the statement cout<<”Hello World!”<<endl; then include the lines of code to
perform the task that you need. You then build and run the program.
Note:
1. The statement return 0; should always be the last statement before the }.

2. In C++ the / is used for division. However the behaviour of the operator depends on its
operands. If both operands are integer values, the operator performs an integer
division and gives the quotient of the first operand divided by the second one. Thus
10/2 returns 5, while 10/4 returns 2. However, if (at least) one operand is a floating
point value, the operation performed is a floating point division. Thus 10/4.0 returns
2.5.

3. C++ uses the operators ++ and - - to increment/decrement a variable by 1. Thus if x


currently has value 5, x++ will result in x being assigned value 6, while x—will result in
x being assigned value 4. We can also have ++x and --x. Their meanings and uses will
be discussed at a later stage.

4. For each of the program below, ensure that you have a prompt for the user (an output
specifying the required input), for each input. Also ensure that the output is
informative enough

Question 1
Write a program that requests the user to input the radius of a circle and it calculates and
displays the area of the circle (Assume pi to be 3.142)

#include <iostream>
using namespace std;

int main()
{
float rad, area, pi;

cout<<"Enter radius:";
cin>>rad;

pi=3.142;
area=pi*rad*rad;

cout<<"Area="<<area;
return 0;
}
Question 2
Write a program that calculates the cost per square centimetre of a circular pizza, given
its diameter and price as inputs.

#include <iostream>
using namespace std;

int main()
{
float cost, d, pi, val, r;

cout<<"Enter price of pizza in MUR:";


cin>>cost;
cout<<"Enter diameter of pizza in cm:";
cin>>d;

pi=3.142;
r=(d/2);
val=cost/(pi*r*r);

cout<<"Price per square cm:"<<val;


return 0;
}
Question 3
Write a program that asks the user his id number (use a simple integer value), score in
CSE1017Y and CSE1019Y and output the user’s name with his average score.
Sample inputs: Please Enter your id number: 28
Enter your score in CSE1017Y: 65
Enter your score in CSE1019Y: 75 …
Sample outputs: id: 28
Score in CSE1017Y: 65
Score in CSE 1019Y: 75
AVERAGE SCORE: 70

#include <iostream>
using namespace std;

int main()
{
int id, x, y, ave;

cout<<"Please enter your ID number:";


cin>>id;
cout<<"Enter your score in CSE1017Y:";
cin>>x;
cout<<"Enter your score in CSE1019Y:";
cin>>y;

ave=(x+y)/2;

cout<<"ID number:"<<id<<endl;
cout<<"Score in CSE1017Y is "<<x<<endl;
cout<<"Score in CSE1019Y is "<<y<<endl;
cout<<"Average score is "<<ave;

return 0;
}
Question 4
A car travels a distance d1 at a speed s1, followed by a distance d2 at a speed s2 and
finally a distance d3 at a speed s3. Assuming all distances to be in km and all speeds in
km/h, write a program to input the values of s1, s2, s3 and d1, d2, d3 and display:
The total distance traveled.
The total time taken for the whole journey and
The average speed over the journey.

#include <iostream>
using namespace std;

int main()
{
float s1, s2, s3, d1, d2, d3;

cout<<"Enter speed s1:";


cin>>s1;
cout<<"Enter distance d1:";
cin>>d1;
cout<<"Enter speed s2:";
cin>>s2;
cout<<"Enter distance d2:";
cin>>d2;
cout<<"Enter speed s3:";
cin>>s3;
cout<<"Enter distance d3:";
cin>>d3;

cout<<"Total distance traveled is:"<<d1+d2+d3<<endl;


cout<<"Total time taken for the whole journey is:"<<((d1/s1)+(d2/s2)+(d3/s3))<<endl;
cout<<"Average speed for the whole journey is:"<< ((s1+s2+s3)/3)<<endl;

return 0;
}
Question 5
The Konditorei coffee shop sells coffee at Rs 25.00 a cup plus cost of delivery. For
delivery, there is a fixed initial cost of Rs 15.00 and an additional cost Rs2.50 per cup .
Eg. For one cup the delivery cost is Rs17.50, for 2 cups it is Rs 20.00, while for 10 cups it
is Rs 40.00. Write a program that calculates the cost of an order. (Note: Input should be
no. of cups ordered)

#include <iostream>
using namespace std;
int main()
{
float N;

cout << "Enter the number of cups ordered:";


cin>>N;

cout<<"The cost of the order is:"<<(N*25)+15+(N*2.5);


return 0;
}
Question 6
The figure below shows a concrete cylindrical pillar with a cylindrical hole in the middle.
The hole is of two parts. One part is of height h1, with radius r1, the other part is of height
h2, with radius r2. The pillar has height h and radius r. Write a program, that takes as
inputs the values of h, r, h1, r1 and r2 and calculates the volume of concrete required to
construct such a pillar with the given values. Note: h = h1 + h2

#include <iostream>
using namespace std;
int main()
{
float h, r, h1, r1, r2, pi;
double v, v1, v2;

cout << "Enter height h in m:";


cin>>h;
cout << "Enter radius r in m:";
cin>>r;
cout << "Enter height h1 in m:";
cin>>h1;
cout << "Enter radius r1 in m:";
cin>>r1;
cout << "Enter radius r2 in m:";
cin>>r2;

pi=3.142;
v1=pi*r1*r1*h1;
v2=pi*r2*r2*(h-h1);
v=pi*r*r*h;

cout<<"Volume of cement required is:"<<v-(v1+v2)<<endl;


return 0;
}
Question 7
Consider the program in question 6. Given that concrete is sold as full containers of size
y m3 , (where y is an integer). Modify the program so that it also allows as input the value
of y and the cost of a container and displays the cost of concrete for a pillar.

#include <iostream>
using namespace std;
int main()
{
float h, r, h1, r1, r2, pi, y, c;
double v, v1, v2;

cout << "Enter height h in m:";


cin>>h;
cout << "Enter radius r in m:";
cin>>r;
cout << "Enter height h1 in m:";
cin>>h1;
cout << "Enter radius r1 in m:";
cin>>r1;
cout << "Enter radius r2 in m:";
cin>>r2;
cout << "Enter volume of concrete per container in square m:";
cin>>y;
cout << "Enter cost of a single concrete container in Rs:";
cin>>c;

pi=3.142;
v1=pi*r1*r1*h1;
v2=pi*r2*r2*(h-h1);
v=pi*r*r*h;

cout<<"Volume of cement required is:"<<v-(v1+v2)<<endl;


cout<<"Cost of cement required is:"<<(((v-(v1+v2))/y)*c);
return 0;
}
Question 8
Two points in a plane are specified using the coordinates (x1, y1) and (x2, y2). Write a
program that calculates the slope of a line through two non-vertical points entered by the
user. Hint: m = (y2 – y1)/(x2 – x1)

#include <iostream>
using namespace std;
int main()
{
float y1, y2, x1, x2;

cout<<"Enter the first y coordinate, y1:";


cin>>y1;
cout<<"Enter the first x coordinate, x1:";
cin>>x1;
cout<<"Enter the second y coordinate, y2:";
cin>>y2;
cout<<"Enter the second x coordinate, x2:";
cin>>x2;

cout<<"The slope of the line is:"<<((y2-y1)/(x2-x1));


return 0;
}
Question 9
Write a program that accepts two points, like in question 5 above, and determines the
distance between them. d = √((x2 – x1)2 + (y2 – y1)2)

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
float y1, y2, x1, x2;

cout<<"Enter the first y coordinate, y1:";


cin>>y1;
cout<<"Enter the first x coordinate, x1:";
cin>>x1;
cout<<"Enter the second y coordinate, y2:";
cin>>y2;
cout<<"Enter the second x coordinate, x2:";
cin>>x2;

cout<<"The distance between the two points is: "<<sqrt(pow((y2-y1),2)+(pow((x2-x1),2)));


return 0;
}
Question 10
A toy is made of two balls, one fitted inside the other one as shown in the diagram below.
The space in-between the balls is filled with a liquid. Write a program that allows the
input of the radius of each of the balls and it calculates and displays the volume of the
liquid

#include <iostream>
using namespace std;

int main()
{
float Rbig, Rsmall, pi;
double vBig, vSmall;

cout << "Enter radius of the big ball in cm: ";


cin>>Rbig;
cout<<"Enter radius of the small ball in cm: ";
cin>>Rsmall;

pi=3.142;
vBig=(4/3)*pi*Rbig*Rbig*Rbig;
vSmall=(4/3)*pi*Rsmall*Rsmall*Rsmall;

cout<< "The volume of the liquid in square centimeter is: "<<vBig-vSmall;

return 0;
}

You might also like