You are on page 1of 26

CPP Programming Practical Slips Solution

2019 Pattern

Slip2a:
#include <iostream>
using namespace std;
inline int Max(int x, int y)
{
return (x > y)? x : y;
}
inline int Min(int x, int y)
{
return (x < y)? x : y;
}
// Main function for the program
int main()
{
int a,b;
cout<<"Enter Number 1:"<<endl;
cin>>a;//20
cout<<"Enter Number 2:"<<endl;
cin>>b;//25
cout<<"Maximum No:"<<Max(a,b)<<endl;
cout<<"Minimum No:"<<Min(a,b)<<endl;

return 0;
}

Slip3a:
#include<iostream>
using namespace std;
void swap(int * x, int * y)
{
int swap;
swap = *x;
*x = *y;
*y=swap;
}
int main()
{
int x=500, y=100;
swap(&x, &y); // passing reference to function
cout<<"Value of x is: "<<x<<endl;
cout<<"Value of y is: "<<y<<endl;
return 0;
}

Slip4a:
#include<iostream>
using namespace std;
class worker
{
char name[10];
int hr;
public:
void accept()
{
cout<<"enter name";
cin>>name;
cout<<"enter hours";
cin>>hr;
}
void calculate(int rate=50)
{
cout<<"salary of worker is Rs."<<(hr*30)*rate;
}
};
int main()
{
worker ob;
ob.accept();
ob.calculate(100);
return 0;
}

Slip6a:
#include<iostream>
using namespace std;
class Rectangle;
class Square
{
float side,area;
public:
void getdata()
{
cout<<"Enter the Side of a square: ";
cin>>side;
}
float sarea()
{
area=side*side;
return area;
}
friend float compare(Square,Rectangle);
};
class Rectangle
{
float l,b,area;
public :
void getdata()
{
cout<<"Enter length of Rectangle: ";
cin>>l;
cout<<"Enter breadth of rectangle: ";
cin>>b;
}
float rarea()
{
area=l*b;
return area;
}
friend float compare(Square,Rectangle);
};
float compare(Square c1,Rectangle c2)
{
float s,r;
s=c1.sarea();
r=c2.rarea();
if(s>r)
return s;
else return r;
}
int main()
{
Square ob1;
Rectangle ob2;
float a,r,s;
ob1.getdata();
ob2.getdata();
s=ob1.sarea();
cout<<"Area of Sequare: "<<s<<endl;
r=ob2.rarea();
cout<<"Area of Rectangle: "<<r<<endl;
a=compare(ob1,ob2);
cout<<"Greater are is : "<<a;
return 0;
}

Slip6b:
#include<iostream>
using namespace std;
class Matrix
{
int **p;
int r,c;
public:
Matrix(int x,int y);
~Matrix();
void getdata(int i,int j,int value)
{
p[i][j]=value;
}
int & putdata(int i,int j)
{
return p[i][j];
}
};
Matrix::Matrix(int x,int y)
{
r=x;
c=y;
p=new int *[r];
for(int i=0;i<r;i++)
{
p[i]=new int [c];
}
}
Matrix::~Matrix()
{
cout<<"Object Deleted ";
delete [] p;
}
int main()
{
int m,n;
cout<<"Enter the dimention of matrix"<<endl;
cin>>m>>n;
Matrix ob1(m,n);
cout<<"Enter the elements of Matrix row by row"<<endl;
int i,j,value;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>value;
ob1.getdata(i,j,value);
}
cout<<"\n";
}
cout<<"Printing Elements:"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<ob1.putdata(i,j)<<" ";
}
cout<<"\n";
}
cout<<"Printing Transpose Matrix:"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<ob1.putdata(j,i)<<" ";
}
cout<<"\n";
}
return 0;
}

Slip7a:
#include<iostream>
using namespace std;
class slip7a
{
public:
char str[50];
public:
int replace(char ch1, char ch2='b')
{
int i,cnt=0;
cout<<"enter string:";
cin>>str;
for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch1)
{
str[i]=ch2;
cnt++;
}
}
cout<<"After replacement string is"<<str<<endl;
return cnt;
}
};
int main()
{
slip7a ob1;
int num;
num=ob1.replace();
cout<<"Number of replacements are:"<<num<<endl;
return 0;
}
Slip7b:
#include<iostream>
using namespace std;
class Vector
{
int *a;
int size;
public:
Vector()
{
size=0;
a=new int[size];
}
Vector(int n)
{
int i;
size=n;
a = new int[size];
cout<<"Enter the vector Element: ";
for(i=0;i<size;i++)
{
cin>>a[i];
}
}
Vector(Vector &ob)
{
int i;
size = ob.size;
a=new int[size];
a=ob.a;
}
void display()
{
int i;
cout<<"\nThe vector is: (";
for(i=0;i<size-1;i++)
{
cout<<a[i]<<",";
}
cout<<a[size-1]<<")";
}
void Union(Vector &ob1, Vector &ob2)
{
int i,j,k;
size=ob1.size+ob2.size;//3+2
delete a;
a=new int[size]; // 10 20 30 40 60
for(i=0;i<ob1.size;i++)
{
a[i]=ob1.a[i];

}
for(j=i,k=0;j<size,k<ob2.size;j++,k++)
{
a[j]=ob2.a[k];
}

}
};
int main()
{
int m,w;
cout<<"Enter the dimensions for first vector:"<<endl;
cin>>m;
Vector v1(m);
cout<<"Enter the dimensions for Second vector:"<<endl;
cin>>w;
Vector v2(w);
Vector v3(v2);
Vector v4;
v1.display();
//v2.display();
v3.display();
v4.Union(v1,v3);
cout<<"\n";
cout<<"Union Of two vector is:";
v4.display();
return 0;
}

Slip8a:
#include<iostream>
using namespace std;
class Number
{
static int cnt;
int n;
public:
/* void set_n()
{
cnt++;
}
*/
void Display()
{
cnt++;
cout<<"Number of times display operation performed is:"<<cnt<<endl;
}
};
int Number::cnt=0;
int main()
{
Number ob1,ob2,ob3;
//ob1.set_n();
//ob2.set_n();
//ob3.set_n();
ob1.Display();
ob2.Display();
ob3.Display();
ob1.Display();
return 0;
}

Slip9b:
#include<iostream>
using namespace std;
class time
{
int hour;
int minutes;
int seconds;
public:
void gettime()
{
cout<<"Enter Hours:"<<"\n";
cin>>hour;
cout<<"Enter minutes :"<<"\n";
cin>>minutes;
cout<<"Enter Seconds :"<<"\n";
cin>>seconds;
}
void puttime(void)
{
cout<<hour<<"hours : ";
cout<<minutes<<"minutes : " ;
cout<<seconds<<"seconds : "; cout<< "\n";
}
void diff(time,time);

};
void time :: diff(time t1,time t2)
{
if(t1.seconds>t2.seconds)
seconds = t1.seconds-t2.seconds;
else
seconds=t2.seconds-t1.seconds;
if(t1.minutes>t2.minutes)
minutes = t1.minutes-t2.minutes;
else
minutes=t2.minutes-t1.minutes;
if(t1.hour>t2.hour)
hour=t1.hour-t2.hour;
else
hour=t2.hour-t1.hour;
}
int main()
{
time T1,T2,T3;
T1.gettime();
T2.gettime();
T3.diff(T1,T2);
cout<<"T1 = "; T1.puttime();
cout<<"T2 = ";T2.puttime();
cout<<"T3 = "; T3.puttime();
return 0;
}
Slip10a:
#include<iostream>
using namespace std;
class dynamic
{
int size,*ptr,*p;
public:
dynamic(int no)
{
size=no;
ptr=new int[size];
for(int i=0;i<size;i++)
{
cout<<"enter element";
cin>>ptr[i];
}
}
void display()
{
cout<<"elements are";
for(int i=0;i<size;i++)
{
cout<<ptr[i]<<"\t";
}
}
void evnodd()
{
int i;
cout<<"even nos are : \t";
for(i=0;i<size;i++)
{
if(ptr[i]%2==0)
{
cout<<ptr[i];
cout<<"\t";
}
}
cout<<"\n Odd nos are : \t ";
for(i=0;i<size;i++)
{
if(ptr[i]%2!=0)
{
cout<<ptr[i];
cout<<"\t";
}
}
}
~dynamic()
{delete ptr;
}
};
int main()
{
int n;
cout<<"enter size";
cin>>n;
dynamic d(n);
d.display();
d.evnodd();
return 0;
}
Slip10b:
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<iomanip>
using namespace std;
class City
{
public:
int ccode,pop;
char cname[20];
public:
void accept()
{
cout<<"Enter City Code:"<<endl;
cin>>ccode;
cout<<"Enetr City Name:"<<endl;
cin>>cname;
cout<<"Enter City Population:"<<endl;
cin>>pop;
cout<<"---------------------------------------------------------"<<endl;
}
void display()
{
cout<<setw(20)<<"City Code :"<<setw(10)<<ccode<<endl;
cout<<setw(20)<<"City Name :"<<setw(10)<<cname<<endl;
cout<<setw(20)<<"City population :"<<setw(10)<<pop<<endl;
}
void sort(City c[]);
};
void City::sort(City c[])
{
City c1;
int i,j;
for(i=0;i<=2;i++)
{
for(j=i+1;j<=2;j++)
{
if(c[i].pop > c[j].pop)
{
c1=c[i];
c[i]=c[j];
c[j]=c1;
}
}
}
}
int main()
{
City s[2],ob;
int i,j,code;
char name[20];
for(i=0;i<=2;i++)
{
s[i].accept();
}
ob.sort(s);
for(i=0;i<=2;i++)
s[i].display();
cout<<"Enter the code of city you want to dispaly:"<<endl;
cin>>code;
for(i=0;i<=2;i++)
{
if(s[i].ccode==code)
s[i].display();
}
return 0;
}

Slip11a:
#include<iostream>
using namespace std;
class MyArray
{
int size,*ptr;
public:
MyArray(int no)
{
size=no;
ptr=new int[size];//2 5 3
for(int i=0;i<size;i++)
{
cout<<"enter element ";
cin>>ptr[i];
}
}
void display()
{
cout<<"elements are ";
for(int i=0;i<size;i++)
{
cout<<ptr[i]<<"\t";
}
}
void sum()
{
int i,sum=0;

for(i=0;i<size;i++)
{
sum=sum+ptr[i];

}
cout<<"\n";
cout<<"Sum of elements is:"<<sum<<endl;
}
~MyArray()
{
delete ptr;
cout<<"Object Deleted!!"<<endl;
}
};
int main()
{
int n;
cout<<"enter the dimensions:";
cin>>n;
MyArray d(n);
d.display();
d.sum();
return 0;
}

Slip12a:
#include<iostream>
using namespace std;
class Date
{
int day,month,year;
public:
Date()
{
day=1;
month=5;
year=2021;
}
Date(int d1,int m1,int y1)
{
day=d1;
month=m1;
year=y1;
}
void dispaly()
{
if(month==1||month==01)
cout<<day<<"-"<<"Jan"<<"-"<<year;
else
if(month==2||month==02)
cout<<day<<"-"<<"Feb"<<"-"<<year;
else
if(month==3||month==03)
cout<<day<<"-"<<"March"<<"-"<<year;
else
if(month==4||month==04)
cout<<day<<"-"<<"April"<<"-"<<year;
else
if(month==5||month==05)
cout<<day<<"-"<<"May"<<"-"<<year;
else
if(month==6||month==06)
cout<<day<<"-"<<"June"<<"-"<<year;
else
if(month==7||month==07)
cout<<day<<"-"<<"July"<<"-"<<year;
else
if(month==8)
cout<<day<<"-"<<"Aug"<<"-"<<year;
else
if(month==9)
cout<<day<<"-"<<"Sept"<<"-"<<year;
else
if(month==10)
cout<<day<<"-"<<"Oct"<<"-"<<year;
else
if(month==11)
cout<<day<<"-"<<"Nov"<<"-"<<year;
else
cout<<day<<"-"<<"Dec"<<"-"<<year;
}
};
int main()
{
Date d1;
int d,m,y;
cout<<"Enter Day:";
cin>>d;
cout<<"Enter Month :";
cin>>m;
cout<<"Enter Year : ";
cin>>y;
Date d2(d,m,y);
d1.dispaly();
cout<<"\n";
d2.dispaly();
return 0;
}

Slip13a
#include<iostream>
using namespace std;
class Product
{
int prod_id,qnty;
char pname[20];
float price;
static int cnt;
public:
void getdata()
{
cout<<"Enter Product Id:"<<endl;
cin>>prod_id;
cout<<"Enter Product Name:"<<endl;
cin>>pname;
cout<<"Enter Product Quantity:"<<endl;
cin>>qnty;
cout<<"Enter Product Price:"<<endl;
cin>>price;
++cnt;
}
void putdata()
{
cout<<"ProductId: "<<prod_id<<endl;
cout<<"Product NAme: "<<pname<<endl;
cout<<"Product Quantity: "<<qnty<<endl;
cout<<"Price: "<<price<<endl;
cout<<"---------------------------------------------------"<<endl;
}
static void showcnt()
{
cout<<"No of products : "<<cnt<<endl;
}
};
int Product::cnt;
int main()
{
Product p1,p2,p3;

p1.getdata();
p2.getdata();
p3.getdata();
p1.putdata();
p2.putdata();
p3.putdata();
Product::showcnt();
return 0;
}

Slip13b:
#include<iostream>
using namespace std;
class Cuboid
{
public:
float length,breadth,height;
public:
void setvalues(float l,float b,float h)
{
length=l;
breadth=b;
height=h;
}
void getvalues()
{
cout<<"Length of Cube :"<<length<<endl;
cout<<"Breadth of cube:"<<breadth<<endl;
cout<<"Height of Cube:"<<height<<endl;
}
inline float volume()
{
return length*breadth*height;
}
inline float surface_area()
{
return 6*length*length;
}
};
int main()
{
Cuboid c;
float vol,area;
c.setvalues(2.5,3.7,4.6);
c.getvalues();
vol=c.volume();
cout<<"Volume of Cube:"<<vol<<endl;
area=c.surface_area();
cout<<"Surface Area of cube : "<<area<<endl;
return 0;
}

Slip14a:
#include<iostream>
using namespace std;
class circle
{
public:
float r;
public:
void accept()
{
cout<<"Enter the redius:"<<endl;
cin>>r;
}
inline float diameter()
{
return 2*r;
}
inline float circumference()
{
return 2*3.14*r;
}
inline float area()
{
return 3.14*r*r;
}
};
int main()
{
circle c;
float cir,dia,area;
c.accept();
cout<<"Redius is:"<<c.r<<endl;
cout<<"Diameter is: "<<c.diameter()<<endl;
cout<<"Circumference is"<<c.circumference()<<endl;
cout<<"Area is "<<c.area()<<endl;
return 0;
}

Slip15a:
#include<iostream>
using namespace std;
class Fraction
{
int Numerator,Denominator;
public:
Fraction()
{
}
Fraction(int nu,int de)
{
Numerator=nu;//ob1=2 ob2=4
Denominator=de;//ob1=3 ob2=7
}
void display()
{
cout<<"Fraction is :"<<Numerator<<"/"<<Denominator;
cout<<"\n";
}
void sum(Fraction f1,Fraction f2)
{
Denominator=f1.Denominator*f2.Denominator;//21
Numerator=((f1.Numerator*f2.Denominator)+(f2.Numerator*f1.Denominator));//14+12=26
cout<<"Sum of two Fraction:";
cout<<Numerator<<" / "<<Denominator;
cout<<"\n";
/*(2/3)+(4/7)=((2*7)/(3*7))+((4*3)/(7*3))
=(14/21)+(12/21)
=(14+12)/21
=26/21*/
}
};
int main()
{
int n,d,n1,d1;
cout<<"Enter the value for Numerator : ";
cin>>n; //2
cout<<"Enter the value for Denominator : ";
cin>>d;//3
Fraction ob1(n,d);
cout<<"Enter the value for Numerator : ";
cin>>n1; //4
cout<<"Enter the value for Denominator : ";
cin>>d1;//7
Fraction ob2(n1,d1);
Fraction ob3;
ob1.display();
ob2.display();
ob3.sum(ob1,ob2);
//cout<<"Sum of Two Faction is:";
//cout<<"\n";
//ob3.display();
return 0;
}

Slip25a:
#include<iostream>
using namespace std;
class A
{
public:
float a,b,c;
public:
void setvalues()
{
cout<<"Enter value1:";
cin>>a;
cout<<"Enter value2:";
cin>>b;
cout<<"Enter value3:";
cin>>c;
}
void getvalues()
{
cout<<"a :"<<a<<endl;
cout<<"b :"<<b<<endl;
cout<<"c :"<<c<<endl;
}
inline float mean()
{
return (a+b+c)/3;
}
inline float mode()
{
return (a==b)?a:c;
}
inline float median()
{
return b;
}
};
int main()
{
A ob1;
ob1.setvalues();
ob1.getvalues();
cout<<"Mean is :"<<ob1.mean()<<endl;
cout<<"Mode is: "<<ob1.mode()<<endl;
cout<<"Median is :"<<ob1.median()<<endl;
return 0;
}
Slip28a:
#include<iostream>
using namespace std;
class MyArray
{
int size,*ptr;
public:
MyArray(int no)
{
size=no;
ptr=new int[size];//2 5 3
for(int i=0;i<size;i++)
{
cout<<"enter element ";
cin>>ptr[i];
}
}
void display()
{
cout<<"elements are ";
for(int i=0;i<size;i++)
{
cout<<ptr[i]<<"\t";
}
}
void sum()
{

int i;
cout<<"\n";

for(i=size-1;i>=0;i--)
{
cout<<ptr[i]<<"\t";

}
~MyArray()
{
delete ptr;
cout<<"Object Deleted!!"<<endl;
}
};
int main()
{
int n;
cout<<"enter the dimensions:";
cin>>n;
MyArray d(n);
d.display();
d.sum();
return 0;
}

Slip30b:
#include<iostream>
using namespace std;
class Integer_Array;
class Float_Array;
class Integer_Array
{
int a1[10],i,n;
public:
void getdata()
{
cout<<"How many elements do you want: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the Element: "<<endl;
cin>>a1[i];
}
}
void putdata()
{
cout<<"Elements of Integer Array are: "<<endl;
for(i=0;i<n;i++)
{
cout<<"Element is :"<<a1[i]<<endl;
}
}
friend float avg(Integer_Array,Float_Array);
};
class Float_Array
{
float a2[10];
int i,n;
public:
void getdata()
{
cout<<"How many elements do you want: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the Element: "<<endl;
cin>>a2[i];
}
}
void putdata()
{
cout<<"Elements of Float Array are: "<<endl;
for(i=0;i<n;i++)
{
cout<<"Element is :"<<a2[i]<<endl;
}
}
friend float avg(Integer_Array,Float_Array);
};
float avg(Integer_Array ob1,Float_Array ob2)
{
float sum1=0,sum2=0,a; //ob1 2 4 5 8
int i,m; // ob2 2.5 1.2 3.5
m=ob1.n+ob2.n;// 7
for(i=0;i<ob1.n;i++)
{
sum1=sum1+ob1.a1[i];

}
for(i=0;i<ob2.n;i++)
{
sum2=sum2+ob2.a2[i];

}
a=(sum1+sum2)/m;
return a;
}
int main()
{
Integer_Array s1;
Float_Array s2;
float Ans;
s1.getdata();
s2.getdata();
s1.putdata();
s2.putdata();
Ans=avg(s1,s2);
cout<<"Avarage of two arrays is : "<<Ans<<endl;
return 0;
}

You might also like