You are on page 1of 26

20CE158 Practicals (CE-4 Batch-B4)

Program: 1 Write a C++ program that will print output in the following form. Make sure your output
looks exactly as shown here (including spacing, line breaks, punctuation, and the title and
author). Use cout and cin objects and endl manipulator.
******************************
* Programmimg Assignment 1 *
* Computer Programmimg I *
* Author : ??? *
* Due Date: Thursday, Dec. 20 *
******************************
Question: Difference between \n and endl

Code #include<iostream>
using namespace std;
int main()
{
cout<<"******************************"<<endl;
cout<<"* Programming Assignment 1 *"<<endl;
cout<<"* Computer Programming 1 *"<<endl;
cout<<"* Author : Pratham Patel *"<<endl;
cout<<"* Due Date : Thu 20 *"<<endl;
cout<<"******************************"<<endl;
}

Output
Program: 2 Write a program to create the following table. Use endl and setw manipulator.

1234

2468

3 6 9 12

4 8 12 16

Code #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j;
for(i=1;i<5;i++)
{
for(j=1;j<5;j++)
{
cout<<setw(4)<<i*j;
}
cout<<endl;
}
}
Output
Program: 3 Write a C++ program to add two floating numbers using pointer. The result should contain
only two digits after the decimal. Use fixed, scientific and setprecision () manipulators
for controlling the precision of floating point numbers.
Code #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float i,j;
cout<<"Enter 2 numbers = ";
cin>>i>>j;
float *p1,*p2;
p1=&i;
p2=&j;
cout<<"First Number : "<<*p1<<endl;
cout<<"Second Number : "<<*p2<<endl;
cout<<fixed<<setprecision(2)<<"Sum using Fixed = "<<*p1+*p2<<endl;
cout<<scientific<<setprecision(2)<<"Sum using Scientific = "<<*p1+*p2<<endl;
}
Output
Program: 4 Write a C++ program to find out sum of array element using Recursion.
Question: Show stepwise solution of winding and unwinding phase of recursion

Code #include <bits/stdc++.h>


using namespace std;
int recursion(int p[],int q)
{
if(q<0)
return 0;
else
return p[q] + recursion(p,q-1);
}
int main()
{
int i;
cout<<"Enter the size of array = ";
cin>>i;
int j[i];
cout<<"Input : ";
for(int k=0;k<i;k++)
{
cin>>j[k];
}
cout<<"Sum of elements of array is "<<recursion(j,i-1);
}
Output
Program 5 Find error in the following code and give reasons for each error:
Can we declare an array of references? Can we assign NULL value to reference variable?
Is Reference variable a pointer variable? Can we declare a reference variable without
initializing it? Does Reference Variable change the original value of variable?
#include<iostream>
using namespace std;
int main()
{
int no1=10, no2=12;
int & x=no1;
int & r;
int & c = NULL;
int & d[2] = {no1,no2};
cout<<"x = "<< x+20;
cout<<"no1="<< no1+10;
return 0;
}
Code #include<iostream>
using namespace std;
int main()
{
int no1=10,no2=12;
int &x=no1;
int &r;
int &c=NULL;
int &d[2]={1,2};
cout<<"x="<<x+20;
cout<<"no1="<<no1;
return 0;
}
Output
Program 6 Find output of the following code: Explain how scope Resolution operator is used to
access global version of a variable.
#include<iostream.h>
#include<conio.h>
int m=30;
int main()
{
int m=20;
{
int m=10;
cout<<”we are in inner block”<<endl;
cout<<”value of m=”<<m<<”\n”;
cout<<”value of ::m=”<<::m<<”\n”;
}
cout<<”we are in outer block”<<endl;
cout<<”value of m=”<<m<<”\n”;
cout<<”value of ::m=”<<::m<<”\n”;
getch();
return 0;}
Code #include<iostream>
#include<conio.h>
using namespace std;
int m=30;
int main()
{
int m=20;
{
int m=10;
cout<<"we are in inner block"<<endl;
cout<<"value of m="<<m<<"\n";
cout<<"value of ::m="<<::m<<"\n";
}
cout<<"we are in outer block"<<endl;
cout<<"value of m="<<m<<"\n";
cout<<"value of ::m="<<::m<<"\n";
getch();
return 0;
}

Practicals
Output
Program 7 Write a program to enter a size of array. Create an array of size given by user using “new”
Dynamic memory management operator (free store operator). Enter the data to store in array
and display the data after adding 2 to each element in the array. Delete the array by using
“delete” memory management operator.
Code #include<iostream>
using namespace std;
int main()
{
int size;
cout<<"enter the size of array"<<endl;
cin>>size;
int *arr;
arr= new int[size];
for(int i=0;i<size;i++)
{
cout<<arr[i]<<endl;
}
cout<<"enter the elements of array"<<endl;
for(int i=0;i<size;i++)
{
cin>>arr[i];
}
for(int i=0;i<size;i)
{
arr[i]+=2;
}
for(int i=0;i<size;i++)
{
cout<<arr[i]<<endl;
}
cout<<endl;
delete arr;
return 0;
}
Output
Program 8 Find Error in the following code of a program and give explanation why these errors exist.

NOTE: USE reference of other material as there is a mistake in 6th edition of Bala

Guruswamy.

1. //This is an example of

constant pointer

#include <iostream>

using namespace std;

int main()

int var1 = 35,var2 = 20;

int *const ptr = &var1;

ptr = &var2;

cout<<"var1= "<<*ptr;

return 0;

2. //This is an example of

pointer to constant

#include <iostream>

using namespace std;

int main()

int var1 = 43;


const int* ptr = &var1;

*ptr = 1;

var1=34;

cout<<"var1 = "<< *ptr;

return 0;

3. //This is an example of

constant pointer to a constant

#include <iostream>

using namespace std;

int main()

int var1 = 0,var2 = 0;

const int* const ptr = &var1;

*ptr = 1;

ptr = &var2;

cout<<"Var1 = "<<*ptr;

return 0;

8.1 Code #include<iostream>


using namespace std;
int main()
{
//contant pointer
int var1 = 35,var2 = 20;
int *const ptr = &var1;
// ptr = &var2;
var1=1;
cout<<"var1= "<<*ptr;
return 0;
}
Output
8.2 Code //pointer to constant
#include<iostream>
using namespace std;
int main()
{
int var1 = 43,var2=10;
const int* ptr = &var1;
//*ptr = 1;
ptr=&var2;
var1=34;
cout<<"var1 = "<< *ptr;
return 0;
}
Output
Code 8.3 //constant pointer to a constant
#include <iostream>
using namespace std;
int main()
{
int var1 = 0,var2 = 0;
const int* const ptr = &var1;
//*ptr = 1;
//ptr = &var2;
cout<<"Var1 = "<<*ptr;
return 0;
}
Output
Program 9 Find the output of following program. Explain the use of bool data type.
#include<iostream>
using namespace std;
int main()
{ bool a = 321, b;
cout << "Bool a Contains : " << a<<endl;
int c = true; int d = false;
cout<<"c = "<<c <<endl<<"d = "<<d;
c = a + a;
cout << "\nInteger c contain : " << c;
b = c + a;
cout << "\nBool b contain : " <<b;
return 0;}
Code #include<iostream>
using namespace std;
int main()
{
bool a = 321, b;
cout << "Bool a Contains : " << a<<endl;
int c = true; int d = false;
cout<<"c = "<<c <<endl<<"d = "<<d;
c = a + a;
cout << "\nInteger c contain : " << c;
b = c + a;
cout << "\nBool b contain : " <<b;
return 0;
}
Output
Program 10 Define three functions named divide(). First function takes numerator and denominator as an
input argument and checks it’s divisible or not, Second function takes one int number as input
argument and checks whether the number is prime or not and Third function takes 3 float
number as argument and finds out average of the numbers. Use concept of Function
Overloading / static binding.
Code #include <bits/stdc++.h>
using namespace std;
void divide(int i,int j)
{
if(i%j==0)
{
cout<<i<<" is divisible by "<<j;
}
else
{
cout<<i<<" is not divisible by "<<j;
}
}
void divide(int i)
{
bool n=1;
for(int j=2;j<i;j++)
{
if(i%j==0)
{
n=0;
break;
}
}
if(n==0)
{
cout<<i<<" is not a prime number";
}
else
{
cout<<i<<" is a prime number";
}
}
void divide(float i,float j,float k)
{
cout<<"avg value of "<<i<<", "<<j<<" and "<<k<<" is "<<(i+j+k)/3;
}
int main()
{
int i,j;
float e,d,f;
cout<<"Enter two numbers : ";
cin>>i>>j;
divide(i,j);cout<<endl;
cout<<"Enter a number : ";
cin>>i;
divide(i);cout<<endl;
cout<<"Enter three numbers : ";
cin>>e>>d>>f;
divide(e,d,f);
}
Output

Program 11 Write a function called tonLarge() that takes two integer arguments call by reference and then
sets the larger of the two numbers to 100 using Return by reference. Write a main() program to
exercise this function.

Practicals
Code #include <bits/stdc++.h>

using namespace std;

int& tonLarge(int &i,int &j)

if(i>j)

return i;

else

return j;

int main()

int r,s;

cout<<"Input : ";

cin>>r>>s;

tonLarge(r,s) = 100;

cout<<"Output : "<<r<<" "<<s;

Output

Program 12 Write a function called power () that takes two arguments: a double value for n and an intfor p,
and returns the result as double value. Use default argument of 2 for p, so that if this argument
is omitted, the number will be squared. Write a main () function that gets values from the user
to test this function.

Code #include <bits/stdc++.h>

using namespace std;

double power(double n,int p=2)

double i=1;

for(int j=0;j<p;j++)
{

i=n*i;

return i;

int main()

double n,p;

cin>>n;

cout<<fixed<<setprecision(2)<<power(n)<<endl;

cin>>n>>p;

cout<<fixed<<setprecision(2)<<power(n,p)<<endl;

Output

Define a C++ Structure Rectangle with data member’s width and height. It has get_values()
member functions to get the data from user and area() member functions to print the area of
rectangle. Also create a C++ Class for the above program. Define both functions inside the class.
Member function defined inside the class behaves like an inline function and illustrate the
difference between C++ Structure and C++ Class.

Code :

#include <bits/stdc++.h>

using namespace std;

struct rectangle{

int i,j;

void get_values()

cout<<"Input length and breadth : ";


cin>>i>>j;

void area()

cout<<"area of rectangle : "<<i*j<<endl;

};

class rect

int i,j;

public:

void get_values()

cout<<"Input length and breadth : ";

cin>>i>>j;

void area()

cout<<"area of rectangle : "<<i*j;

};

int main()

rectangle r1;

r1.get_values();

r1.area();
rect r2;

r2.get_values();

r2.area();

Output

Program: 14 Write a C++ program having class Batsman. It has private data members: batsman name, bcode
(4 Digit Code Number), innings, not out, runs, batting average. Innings, not out and runs are in
integer and batting average is in float.Define following function outside the class using scope
resolution operator.

1) Public member function getdata()to read values of data members.

2) Public member function putdata()to display values of data members.

3) Private member function calcavg() which calculates the batting average of a batsman. Also
make this outside function inline.

Hint : batting average = runs/(innings - notout)

Code #include <bits/stdc++.h>

using namespace std;

class Batsman

int bcode,innings,notout,runs;

float battingaverage;

string name;

public:

void calcavg();

void get_data();

void put_data(){

cout<<name<<endl;

cout<<bcode<<endl<<innings<<endl<<notout<<endl<<runs<<endl;

}
};

void Batsman :: get_data()

cin>>name;

cin>>bcode>>innings>>notout>>runs;

inline void Batsman :: calcavg()

cout<<abs(runs/(innings-notout))<<endl;

int main()

Batsman b1,b2,b3;

b1.get_data();

b1.put_data();

b1.calcavg();

b2.get_data();

b2.put_data();

b2.calcavg();

b3.get_data();

b3.put_data();

b3.calcavg();

Output
Program 15 Define class Currency having two integer data members rupee and paisa. A class has member
functions enter() to get the data and show() to print the amount in 22.50 format. Define one
member function sum() that adds two objects of the class and stores answer in the third object
i.e. c3=c1.sum (c2). The second member function should add two objects of type currency
passed as arguments such that it supports c3.add(c1,c2); where c1, c2 and c3 are objects of
class Currency. Also Validate your answer if paisa >100. Write a main( )program to test all the
functions. Use concepts of Object as Function Arguments, function

returning object and function overloading.

Code #include <bits/stdc++.h>

using namespace std;

class currency

int rupee,paisa;

float i;

public:

void print();

currency sum(currency t);

void enter();

void show()

if(paisa<0)

cout<<"Nothing to say...";

exit(0);

}
float j;

i=paisa/100;

j=paisa%100;

i = rupee + i + j/100;

cout<<fixed<<setprecision(2)<<i<<endl;

};

void currency :: enter()

cin>>rupee>>paisa;

void currency :: print()

cout<<i<<endl;

currency currency :: sum(currency t)

currency temp;

temp.i = i + t.i;

return temp;

int main()

currency c1,c2,c3;

c1.enter();

c1.show();

c2.enter();
c2.show();

c3 = c1.sum(c2);

c3.print();

Output

Program 16 Define a class Dist with int feet and float inches. Define member function that displays distance
in 1’-2.5” format. Also define member function scale ( ) function that takes object by reference
and scale factor in float as an input argument. The function will scale the distance accordingly.

For example, 20’-5.5” and Scale Factor is 0.5 then answer is 10’-2.75”

Code #include <bits/stdc++.h>

using namespace std;

class dist

int f;

float i;

public:

dist scale(dist &t,float s);

void display();

void getdata()

cout<<"Input feet and inches : ";

cin>>f;

cin>>i;

};

void dist :: display()


{

cout<<"Output before scaling : "<<f<<"'"<<i<<"''"<<endl;

dist dist :: scale(dist &t,float s)

cout<<"Output after scaling : "<<t.f*s<<"'"<<t.i*s<<"''";

int main()

dist d1,d2;

d1.getdata();

cout<<"Input scale factor : ";

float k;

cin>>k;

d1.display();

d2.scale(d1,k);

Outpurt

Program 17 Create a Class Gate for students appearing in Gate (Graduate Aptitude test for Engineering)
exam. There are three examination center Vadodara, Surat, and Ahmedabad where Gate exams
are conducted. A class has data members: Registration number, Name of student, Examination
center. Class also Contains static data member ECV_Cnt, ECS_Cnt and ECA_Cnt which counts
the number of students in Vadodara, Surat and Ahmedabad exam center respectively. Class
Contains two Member function getdata () which gets all information of students and counts
total students in each exam center and pudata () which prints all information about the
students. Class also contains one static member function getcount () which displays the total
number of students in each examination center. Write a program for 5 students and display the
total number of students in each examination center. Use static data member, static member
function and Array of Objects.

Code #include <bits/stdc++.h>

using namespace std;


class Gate{

int regNo;

static int ECV_Cnt,ECS_Cnt,ECA_Cnt;

string Name,center;

public:

static void getcount();

void getdata();

void putdata()

cout<<"Registered No. : "<<regNo<<endl;

cout<<"Name of Student : "<<Name<<endl;

cout<<"Examination Centre : "<<center<<endl;

};

int Gate :: ECV_Cnt;

int Gate :: ECS_Cnt;

int Gate :: ECA_Cnt;

void Gate :: getdata()

cin>>regNo;

cin>>Name;

cin>>center;

if(center == "Vadodara" || center == "VADODARA" || center == "vadodara")

ECV_Cnt++;

else if(center == "Surat" || center == "SURAT" || center == "surat")


{

ECS_Cnt++;

else if(center == "Ahmedabad" || center == "AHMEDABAD" || center == "ahmedabad")

ECA_Cnt++;

void Gate :: getcount()

cout<<"Students in Vadodara : "<<ECV_Cnt<<endl;

cout<<"Students in Surat : "<<ECS_Cnt<<endl;

cout<<"Students in Ahmedabad : "<<ECA_Cnt<<endl;

int main()

Gate G[5];

for(int i=0;i<5;i++)

G[i].getdata();

G[i].putdata();

Gate :: getcount();

Output

Program 18 Define a class Fahrenheit with float temp as data member.Define another class Celsius with
float temperature as data member. Both classes have member functions to input and print
data. Write a non-member function that receives objects of both the classes and declare which
one is higher than another according to their values. Also define main() to test the function.
Define all member functions outside the class. (Formula for converting Celsius to Fahrenheit is
F = (9C/5) + 32). Use the concept of friend function.

Code #include <bits/stdc++.h>

using namespace std;

class Celsius;

class Fahrenheit

float i;

public:

void getdata()

cout<<"Enter Temperature in Fahrenheit : ";

cin>>i;

void putdata()

cout<<"Temperature in Fahrenheit : "<<i<<"°F"<<endl;

friend void great(Fahrenheit f, Celsius c);

};

class Celsius

float j;

public:

void getdata()
{

cout<<"Enter Temperature in Celsius : ";

cin>>j;

void putdata()

cout<<"Temperature in Celsius : "<<j<<"°C"<<endl;

friend void great(Fahrenheit f, Celsius c);

};

void great(Fahrenheit f, Celsius c)

float k = (9*c.j/5) + 32;

if(k<f.i)

cout<<"Fahrenheit is bigger";

else if(f.i<k)

cout<<"Celsius is bigger";

else

cout<<"Both are equal";

int main(void)
{

Fahrenheit f1;

Celsius c1;

f1.getdata();

f1.putdata();

c1.getdata();

c1.putdata();

great(f1,c1);

Output

Program 19 Create a Class Date having data members: int dd, mm, yyyy. Class has one member function to
input the dates and another member function which prints the dates. Write a main() function
which takes two dates as input. Write a friend function swapdates() which takes two objects by
reference of type Date and swaps both the dates. Use the concept of Friend function which
takes objects by reference

Code #include <bits/stdc++.h>

using namespace std;

class date{

int dd,mm,yy;

public:

void get();

void put()

cout<<dd<<"-"<<mm<<"-"<<yy<<endl;

friend void swap(date &i,date &j);

};

void date :: get()


{

cin>>dd>>mm>>yy;

void swap(date &i,date &j)

int r,s,t;

r = i.dd;i.dd = j.dd;j.dd = r;

s = i.mm;i.mm = j.mm;j.mm = s;

t = i.yy;i.yy = j.yy;j.yy = t;

int main()

date d1,d2;

d1.get();

d1.put();

d2.get();

d2.put();

swap(d1,d2);

d1.put();

d2.put();

Output

Program 20 Create a class LAND having data members: length, width, area1. Write member functions to
read and display the data of land. Also, calculates the area of the land. Create another class
TILES having data members: l, w, area2. Write a member function to get the data of tile.
Calculate the area of one tile. Class TILE has a member function named number_of_tiles()
which is a friend of class LAND and takes the object of class LAND by reference which calculates
the number of tiles which can be put over the land area. Write the main function to test all the
functions. Use the concept of member function of one class can be a friend function of another
class.
Code #include <bits/stdc++.h>

using namespace std;

class land;

class tile{

int l1,w1,area1;

public:

void get();

void put()

cout<<"Length of tile: "<<l1<<endl;

cout<<"Width of tile: "<<w1<<endl;

cout<<"Area of tile: "<<area1<<endl;

void numberoftiles(land &t1);

};

void tile :: get()

cout<<"Enter length and Width of a tile: ";

cin>>l1>>w1;

area1 = l1*w1;

class land{

int l2,w2,area2;

public:

void get();

void put()

cout<<"Length of land: "<<l2<<endl;


Output

You might also like