Thursday, 28 April 2016
Friday, 22 April 2016
Tuesday, 19 April 2016
Wednesday, 13 April 2016
Friday, 8 April 2016
WAP to understand destructure
#include<iostream.h>
#include<conio.h>
class con
{
int f0,f1,fbi;
public:
con()
{
f0=0;
f1=1;
fbi=f0+f1;
}
~con()
{
cout<<"Data is deleted from memory";
}
void inc()
{
f0=f1;
f1=fbi;
fbi=f0+f1;
}
void dis()
{
cout<<fbi<<endl;
}
};
void main()
{
con obj;
int i;
for(i=0;i<=5;i++)
{
obj.dis();
obj.inc();
getch();
}
}
WAP for destructor
#include<iostream.h>
#include<conio.h>
class Line{
public:
void setlength(double len);
double getlength(void);
Line();
~Line();
private:
double length;
};
Line::Line(void)
{
cout<<"
Object is being crreted";
}
Line::~Line(void)
{
cout<<"
Destructor is called";
}
void Line::setlength(double len)
{
length=len;
}
double Line::getlength(void)
{
return length;
}
void main(){
Line l1;
clrscr();
l1.setlength(3.44);
cout<<"
Length of line : "<<l1.getlength();
getch();
}
WAP for constructor overloading
#include<iostream.h>
#include<conio.h>
#include<string.h>
class emp
{
private:
char name[20];
int age;
float bs;
public:
void read()
{
cout<<"name,age,bs
";
cin>>age>>bs;
}
void write()
{
cout<<"name="<<name<<"age="<<age<<"basic salary="<<bs;
}
emp()
{
strcpy(name," ");
age=0;
bs=0;
}
emp(char *n,int a,float b)
{
strcpy(name,n);
age=a;
bs=b;
}
/*emp(char *n,int a=0,float b=0)
{
strcpy(name,n);
age=a;
bs=b;
} */
emp(emp &e)
{
strcpy(name,e.name);
age=e.age;
bs=e.bs;
}
};
void main()
{
emp e1,e2("Ashish",25,29000.99),e3(e2),e4("Gadpayle",22,7000.99);
clrscr();
e1.write();
e2.write();
e3.write();
e4.write();
getch();
}
WAP for constructor with arguments in base
# include <iostream.h>
# include <conio.h>
class base
{
public:
base(int x)
{
cout<<"X VALUE IS "<<x<<endl;
cout<<"INSIDE BASE CLASS CONSTRUCTOR "<<endl;
}
void display()
{
cout<<"INSIDE DISPLAY OF BASE"<<endl;
}
};
class derived : public base
{
public:
derived(int x,int y) : base(x)
{
cout<<"Y VALUE IS "<<y<<endl;
cout<<"INSIDE DERIVED CLASS CONSTRUCTOR "<<endl;
}
void print()
{
cout<<"INSIDE PRINT OF DERIVED"<<endl;
}
};
void main()
{
clrscr();
derived d(10,20);
d.display();
d.print();
getch();
}
WAP to understand the constructor
#include<iostream.h>
#include<conio.h>
class code
{
public:
int id;
code()
{
id=0;
}
code (int a)
{
id=a;
}
code (code &x)
{
id=x.id;
}
void display()
{
cout<<"
id="<<id;
}
};
void main()
{
// int a;
// cout<<"
enter number :";
// cin>>a;
code c;
c.display();
code d(50);
d.display();
code c1);
c1.display();
getch();
clrscr();
}
WAP for area using default constructor
#include<iostream.h>
#include<conio.h>
class Area{
int len,bre;
public:
Area(){
len=5;
bre=6;
}
void getlength(){
cout<<"
Enter length and breadth ";
cin>>len>>bre;
}
int areacal()
{
return(len*bre);
}
void display(int temp)
{
cout<<"
Area = "<<temp;
}
};
void main(){
Area a1,a2;
clrscr();
int temp;
cout<<"
For Default ";
temp=a1.areacal();
a1.display(temp);
cout<<"
For Paramerised ";
a2.getlength();
temp=a2.areacal();
a2.display(temp);
getch();
}
WAP for default & parameterised constructor
#include<iostream.h>
#include<conio.h>
class Line{
public:
Line(double len);
void setlength(double len);
double getlength(void);
private:
double length;
};
Line::Line(double len)
{
length=len;
}
void Line::setlength(double len)
{
length=len;
}
double Line::getlength(void)
{
return length;
}
void main(){
Line l1(6.3);
clrscr();
cout<<"
Default : "<<l1.getlength();
l1.setlength(3.44);
cout<<"
Paramerised : "<<l1.getlength();
getch();
}
WAP for game constructo
#include<iostream.h>
#include<conio.h>
class Game{
int goals;
public:
Game(){
goals=0;
}
int getgoals(){
return goals;
}
int incrementgoals()
{
return goals++;
}
};
void main(){
Game g;
clrscr();
cout<<"
No od goal when game is stared.. : "<<g.getgoals();
g.incrementgoals();
g.incrementgoals();
cout<<"
No od goal when game is little later.. : "<<g.getgoals();
getch();
}
WAP for copy constructor
#include<iostream.h>
#include<conio.h>
class Game{
int goals;
public:
Game(){
goals=0;
}
int getgoals(){
return goals;
}
int incrementgoals()
{
return goals++;
}
};
void main(){
Game g;
clrscr();
cout<<"
No od goal when game is stared.. : "<<g.getgoals();
g.incrementgoals();
g.incrementgoals();
cout<<"
No od goal when game is little later.. : "<<g.getgoals();
getch();
}
WAP for bubble sort
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[20],i,j,n,temp;
cout<<"\n Enter size of array :";
cin>>n;
cout<<"\n Enter element of array ";
for(i=1;i<=n;i++)
cin>>a[i];
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\n The sorted array are :";
for(i=1;i<=n;i++)
cout<<" "<<a[i];
getch();
clrscr();
}
/* output
Enter size of array :6
Enter element of array 12 45 32 14 10 26
The sorted array are : 10 12 14 26 32 45
WAP for binary search method
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[15],i,n,lb,ub,mid,item;
cout<<"\n Enter size of array :";
cin>>n;
cout<<"\n Enter sorted element for array :";
for(i=1;i<=n;i++)
cin>>a[i];
cout<<"\n Enter item which is to be search :";
cin>>item;
lb=1;
ub=n;
mid=int((lb+ub)/2);
while(a[mid]!=item && lb<=ub)
{
if(item<a[mid])
ub=mid-1;
else
lb=mid+1;
mid=int((lb+ub)/2);
}
if(a[mid]==item)
{
cout<<"\n search is successful ";
cout<<"\n location is= "<<mid;
}
else
cout<<"\n Search is unsuccessful ";
getch();
clrscr();
}
/* output
Enter size of array :5
Enter sorted element for array :11 22 33 44 55
Enter item which is to be search :44
search is successful
location is= 4 */
program to find largest two number
#include<iostream.h>
#include<conio.h>
void main()
{
int a[22],fbig=0,sbig=0,i,n;
cout<<"\n enter size :";
cin>>n;
cout<<"\n enter element :";
for(i=1;i<=n;i++)
cin>>a[i];
fbig=a[1];
for(i=2;i<=n;i++)
{
if(fbig<a[i])
{
fbig=a[i];
}
}
sbig=a[2];
for(i=3;i<=n-1;i++)
{
if(sbig<a[i])
{
sbig=a[i];
}
}
cout<<"\nfirst largest :"<<fbig;
cout<<"\n second largest :"<<sbig;
getch();
clrscr();
}
WAP for sum of elements in matrix
#include<iostream.h>
#include<conio.h>
void main()
{
int a[5][5],n,m,i,j,small;
int sum=0;
cout<<"\n enter size of rows :";
cin>>n;
cout<<"\n enter size of collumns :";
cin>>m;
cout<<"\n enter element of 2d array :";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
cout<<"\n 2d array \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n";
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
sum=sum+a[i][j];
}
cout<<"\n";
}
cout<<"\n The sum of every element :"<<sum;
getch();
clrscr();
}
WAP to find small no in matrix
#include<iostream.h>
#include<conio.h>
void main()
{
int a[5][5],n,m,i,j,small;
cout<<"\n enter size of rows :";
cin>>n;
cout<<"\n enter size of collumns :";
cin>>m;
cout<<"\n enter element of 2d array :";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
cout<<"\n 2d array \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n";
}
small=a[1][1];
for(i=2;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(small>a[i][j])
{
small=a[i][j];
}
}
}
cout<<"\n The smallest element is "<<small;
getch();
clrscr();
}
WAP for linear search using while
#include<conio.h>
#include<iostream.h>
void main()
{
int item,a[20],n,i,loc;
cout<<"\n enter size of array :";
cin>>n;
cout<<"\n enter element of array :";
for(i=1;i<=n;i++)
cin>>a[i];
cout<<"\n enter element which is to be search :";
cin>>item;
a[n+1]=item;
loc=0;
while(a[loc]!=item)
{
loc++;
}
if(loc==n+1)
{
loc=0;
cout<<"\n ITEM is not found :";
}
else
cout<<"\n ITEM is found at location :"<<loc;
getch();
clrscr();
}
/* OUTPUT
enter size of array :6
enter element of array :12 45 23 52 48 45
enter element which is to be search :23
ITEM is found at location :3
WAP for finding a location
#include<iostream.h>
#include<conio.h>
void main()
{
int a[4][4],n,m,i,j,loc=0,key;
cout<<"\n enter size of rows :";
cin>>n;
cout<<"\n enter size of collumns :";
cin>>m;
cout<<"\n enter element of 2d array :";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
cout<<"\n 2d array \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n";
}
cout<<"\n\n Enter element which location is to be search :";
cin>>key;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(a[i][j]==key)
{
cout<<"\nelemt is present at "<< i << "row and "<<j<< "column";
break;
}
}
}
getch();
clrscr();
}
WAP to sort elements in ascending order
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[10],i,j,n,temp,k,ptr;
cout<<"\n Enter size of array :";
cin>>n;
cout<<"\n Enter element of array :";
for(i=1;i<=n;i++)
cin>>a[i];
for(i=2;i<=n;i++)
{
temp=a[i];
ptr=i-1;
while(temp<a[ptr])
{
a[ptr+1]=a[ptr];
ptr--;
}
a[ptr+1]=temp;
}
cout<<"\n The sorted elements are :\n";
for(i=1;i<=n;i++)
cout<<" "<<a[i];
getch();
clrscr();
}
/* OUTPUT
Enter size of array :7
Enter element of array :15 23 10 22 48 75 12
The sorted elements are :
10 12 15 22 23 48 75
WAP to insert element into array
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,loc,n,new1,a[20];
cout<<"\n enter size of array :";
cin>>n;
cout<<"\n enter element of array :";
for(i=1;i<=n;i++)
cin>>a[i];
cout<<"\n enter element is to be insert :";
cin>>new1;
cout<<"\n enter location where element is insert :";
cin>>loc;
i=n;
while(i>=loc)
{
a[i+1]=a[i];
i--;
}
a[loc]=new1;
n++;
cout<<"\n The result is :";
for(i=1;i<=n;i++)
cout<<" "<<a[i];
getch();
clrscr();
}
/* output
enter size of array :5
enter element of array :11 62 45 78 12
enter element is to be insert :33
enter location where element is insert :3
The result is : 11 62 33 45 78 12
WAP to count the repeated location
#include<conio.h>
#include<iostream.h>
void main()
{
int item,a[20],n,i,count=0,loc=0;
cout<<"\n enter size of array :";
cin>>n;
cout<<"\n enter element of array :";
for(i=1;i<=n;i++)
cin>>a[i];
cout<<"\n enter element which is to be search :";
cin>>item;
cout<<"\n The no of location is :";
i=1;
while(i<=n)
{
if(a[i]==item)
{
count=count+1;
cout<<i<<",\t";
i++;
}
else
{
i++;
}
}
if(a[i]==item)
cout<<"\n Total no. of repeated given element :"<<count;
else
cout<<"\n element is found";
getch();
clrscr();
}
WAP for copy one array into anather array
#include<conio.h>
#include<iostream.h>
void main()
{
int a[20],b[20],n,i;
cout<<"\n enter size of array :";
cin>>n;
cout<<"\n enter element of array :";
for(i=1;i<=n;i++)
cin>>a[i];
for(i=1;i<=n;i++)
{
b[i]=a[i];
}
cout<<"\n The copy of array is :";
for(i=1;i<=n;i++)
cout<<b[i];
getch();
clrscr();
}
WAP for addition of matrix.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[4][4],b[4][4],c[4][4],n,m,i,j;
cout<<"\n enter size of rows :";
cin>>n;
cout<<"\n enter size of collumns :";
cin>>m;
cout<<"\n enter element of 2d array :";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
cout<<"\n first matrix \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n\n";
}
cout<<"\n enter same size of rows :";
cin>>n;
cout<<"\n enter same size of collumns :";
cin>>m;
cout<<"\n enter element of 2d array :";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>b[i][j];
}
}
cout<<"\n second matrix \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<b[i][j];
}
cout<<"\n\n";
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<"\n addition of matrix is \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<"\n\n";
}
getch();
clrscr();
}
WAP for multiplication of matrix
#include<iostream.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2],n,m,i,j,k;
cout<<"\n enter size of rows :";
cin>>n;
cout<<"\n enter size of collumns :";
cin>>m;
cout<<"\n enter element of 2d array :";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
cout<<"\n first matrix \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n\n";
}
cout<<"\n enter same size of rows :";
cin>>n;
cout<<"\n enter same size of collumns :";
cin>>m;
cout<<"\n enter element of 2d array :";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>b[i][j];
}
}
cout<<"\n second matrix \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<b[i][j];
}
cout<<"\n\n";
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
c[i][j]=0;
for(k=1;k<=n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
cout<<"\n multiplication of matrix is \n\n\n:";
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<"\n\n";
}
getch();
clrscr();
}
Subscribe to:
Posts (Atom)