Ex.
No:01
ADDING TWO OBJECTS USING FRIEND
DATE : FUNCTION
AIM:
To write a C++ program to create two class DM and DB which store the
value of distance. In DM store distance in meters and centimeters. In DB store
distance in feet and inches, and add these two objects.
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class db;
class dm
{
float meter,cmeter;
public:
void getdata(float m,float cm)
{
meter=m;
cmeter=cm;
}
friend dm calc(dm,db);
void display();
};
class db
{
float feet,inches;
public:
void getdata(float f,float i)
{
feet=f;
inches=i;
}
friend dm calc(dm,db);
};
dm calc(dm a,db b)
{
a.meter=a.meter+(b.feet*0.3);
a.cmeter=a.cmeter+(b.inches*2.5);
if(a.cmeter>=100)
{
a.meter=a.meter+1;
a.cmeter=a.cmeter-100;
}
return(a);
}
void dm::display()
{
cout<<meter<<"meters"<<endl;
cout<<cmeter<<"centimeters";
}
void main()
{
dm m,s;
db n;
float m1,cm,f,i;
clrscr();
cout<<"Enter the meter and centimeter:";
cin>>m1>>cm;
m.getdata(m1,cm);
cout<<"Enter the feet and Inches:";
cin>>f>>i;
n.getdata(f,i);
s=calc(m,n);
s.display();
getch();
}
OUTPUT:
Enter the meter and centimeter: 100 90
Enter the feet and Inches: 50 10
116meters
15centimeters
RESULT:
Thus the above program has been executed successfully and verified.
Ex. No:02
ARITHMETIC OPERATIONS USING
DATE : OPERATOR OVERLOADING
AIM:
To write a C++ program to create a class float that contains only float
data members and overload the four arithmetic operators, so that operators on
the objects of float.
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class FLOAT
{
float no;
public:
FLOAT(){}
void getdata()
{
cout<<"\n ENTER AN FLOATING NUMBER :";
cin>>no;
}
void putdata()
{
cout<<"\n\nANSWER IS :"<<no;
}
FLOAT operator+(FLOAT);
FLOAT operator*(FLOAT);
FLOAT operator-(FLOAT);
FLOAT operator/(FLOAT);
};
FLOAT FLOAT::operator+(FLOAT a)
{
FLOAT temp;
temp.no=no+a.no;
return temp;
}
FLOAT FLOAT::operator*(FLOAT b)
{
FLOAT temp;
temp.no=no*b.no;
return temp;
}
FLOAT FLOAT::operator-(FLOAT b)
{
FLOAT temp;
temp.no=no-b.no;
return temp;
}
FLOAT FLOAT::operator/(FLOAT b)
{
FLOAT temp;
temp.no=no/b.no;
return temp;
}
void main()
{
clrscr();
FLOAT a,b,c;
a.getdata();
b.getdata();
cout<<"\n\nAFTER ADDITION OF TWO OBJECTS";
c=a+b;
c.putdata();
cout<<"\n\nAFTER MULTIPLICATION OF TWO OBJECTS";
c=a*b;
c.putdata();
cout<<"\n\nAFTER SUBSTRACTION OF TWO OBJECTS";
c=a-b;
c.putdata();
cout<<"\n\nAFTER DIVISION OF TWO OBJECTS";
c=a/b;
c.putdata();
getch();
}
OUTPUT:
ENTER AN FLOATING NUMBER :12
ENTER AN FLOATING NUMBER :2
AFTER ADDITION OF TWO OBJECTS
ANSWER IS :14
AFTER MULTIPLICATION OF TWO OBJECTS
ANSWER IS :24
AFTER SUBSTRACTION OF TWO OBJECTS
ANSWER IS :10
AFTER DIVISION OF TWO OBJECTS
ANSWER IS :6
RESULT:
Thus the above program has been executed successfully and verified.
Ex. No:03
ADDING TWO POLAR COORDINATE USING
DATE : OPERATOR OVERLOADING
AIM:
To write a C++ program to add two polar coordinate using operator
overloading
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class polar
{
double r,a,x,y;
public:
void getdata();
polar operator + (polar);
void display();
};
void polar::getdata()
{
cout<<"Enter the radious and angle:";
cin>>r>>a;
}
polar polar::operator+(polar p)
{
double pi=3.14;
polar s;
s.x=(r*cos(a*pi/180))+(p.r*cos(p.a*pi/180));
s.y=(r*sin(a*pi/180))+(p.r*sin(p.a*pi/180));
s.r=sqrt(s.x*s.x+s.y*s.y);
s.a=atan(s.y/s.x)*(180/pi);
return(s);
}
void polar::display()
{
cout<<"The value of radious is:"<<r<<endl;
cout<<"The value of angle is:"<<a;
}
void main()
{
polar p1,p2,p3;
clrscr();
p1.getdata();
p2.getdata();
p3=p1+p2;
p3.display();
getch();
}
OUTPUT:
Enter the radius and angle:5 30
Enter the radius and angle:7 45
The value of radius is:11.900303
The value of angle is:38.75698
RESULT:
Thus the above program has been executed successfully and verified.
Ex. No:04
MATRIX OPERATION TO SOLVE
DATE : ((A-B)^2+B^2-2AB )
AIM:
To write a C++ program to Perform matrix operation to solve the
equation (A-B)^2+B^2-2AB .
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class MAT
{
public:
int i,j,c,r,k;
int a[20][20],b[20][20],ms[20][20],mm[20][20];
int x[20][20],y[20][20],z[20][20],result[20][20];
void getdata()
{
cout<<"\nEnter the value for row and column: ";
cin>>c>>r;
cout<<"\nEnter the value for matrix A\n";
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
cin>>a[i][j];
}
cout<<"\n";
}
cout<<"\nEnter the value for matrix B\n";
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
cin>>b[i][j];
}
cout<<"\n";
}
}
void calc()
{
for(i=0;i<c;i++) // A - B
{
for(j=0;j<r;j++)
{
ms[i][j]=a[i][j]-b[i][j];
}
}
for(i=0;i<c;i++) // (A-B)^2
{
for(j=0;j<r;j++)
{
x[i][j]=0;
for(k=0;k<c;k++)
{
x[i][j] =ms[i][k]*ms[k][j];
}
}
}
for(i=0;i<c;i++) // B^2
{
for(j=0;j<r;j++)
{
y[i][j]=0;
for(k=0;k<c;k++)
{
y[i][j] +=b[i][k]*b[k][j];
}
}
}
for(i=0;i<c;i++) // A * B
{
for(j=0;j<r;j++)
{
mm[i][j]=0;
for(k=0;k<c;k++)
{
mm[i][j] +=a[i][k]*b[k][j];
}
}
}
for(i=0;i<c;i++) // 2 * A * B
{
for(j=0;j<r;j++)
{
z[i][j]=0;
for(k=0;k<c;k++)
{
z[i][j]= 2*mm[i][j];
}
}
}
for(i=0;i<c;i++) // (A-B)^2+B^2-2AB
{
for(j=0;j<r;j++)
{
result[i][j]=x[i][j]+y[i][j]-z[i][j];
}
}
}
void putdata()
{
cout<<"\n\t\t (A-B)^2+B^2-2AB :\n";
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
cout<<"\t\t"<<result[i][j];
}
cout<<"\n";
}
}
};
void main()
{
clrscr();
MAT m;
m.getdata();
m.calc();
m.putdata();
getch();
}
OUTPUT:
Enter the value for row and column: 2 2
Enter the value for matrix A
22
22
Enter the value for matrix B
22
22
(A-B)^2+B^2-2AB :
-8 -8
-8 -8
RESULT:
Thus the above program has been executed successfully and verified.
Ex. No:05
AREA COMPUTATION
DATE :
AIM:
To write a C++ program to find the area computation using derived class.
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class area
{
protected:
float out;
public:
void square(float);
void triangle(float,float);
void rectangle(float,float);
};
class compute: public area
{
public:
void circle(float);
void elipse(float,float);
void sector(float,float);
void display();
};
void area::square(float a)
{
out=a*a;
}
void area::triangle(float b, float h)
{
out=0.5*b*h;
}
void area::rectangle(float b, float h)
{
out=b*h;
}
void compute::circle(float r)
{
float pi=3.141592;
out=pi*r*r;
}
void compute::elipse(float a,float b)
{
float pi=3.141592;
out=pi*a*b;
}
void compute::sector(float r,float rad)
{
float pi=3.141592;
out=0.5*r*r*rad*(pi/180);
}
void compute::display()
{
cout<<"Area is: "<<out<<"sq units"<<endl;
}
void main()
{
compute c;
clrscr();
float a,b;
cout<<"enter the value for Square (Side) :";
cin>>a;
c.square(a);
c.display();
cout<<"enter the value for Rectangle (sides) :";
cin>>a>>b;
c.rectangle(a,b);
c.display();
cout<<"enter the value for Triangle(B & H) :";
cin>>a>>b;
c.triangle(a,b);
c.display();
cout<<"enter the value for Circle (Radious) :";
cin>>a;
c.circle(a);
c.display();
cout<<"enter the value for Ellipse(A & B) :";
cin>>a>>b;
c.elipse(a,b);
c.display();
cout<<"enter the value for Sector(R & RAD) :";
cin>>a>>b;
c.sector(a,b);
c.display();
getch();
}
OUTPUT:
enter the value for Square (Side) :10
Area is: 100sq units
enter the value for Rectangle (sides) :15 20
Area is: 300sq units
enter the value for Triangle(B & H) :15 20
Area is: 150sq units
enter the value for Circle (Radious) :5
Area is: 78.539803sq units
enter the value for Ellipse(A & B) :10 15
Area is: 471.2388sq units
enter the value for Sector(R & RAD) :5 30
Area is: 6.544983sq units
RESULT:
Thus the above program has been executed successfully and verified.
Ex. No:06
VECTOR OPERATIONS
DATE :
AIM:
To write a C++ program to define a class for vector containing scalar
values. Applying overloading concepts for vector addition, subtraction, cross
product and dot product of vector by scalar quantity, replace the values in
position vector.
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class vector
{
float a,b,c,x,y,z,u;
public:
void getdata();
vector operator+(vector);
vector operator-(vector);
vector operator*(vector);
vector operator/(vector);
void display();
void display1();
};
void vector::getdata()
{
cout<<"Enter the Coefficient of vector : ";
cin>>a>>b>>c;
}
vector vector::operator+(vector p)
{
vector s;
s.x=a+p.a;
s.y=b+p.b;
s.z=c+p.c;
return s;
}
vector vector::operator-(vector p)
{
vector s;
s.x=a-p.a;
s.y=b-p.b;
s.z=c-p.c;
return s;
}
vector vector::operator*(vector p)
{
vector s;
s.x=((b*p.c)-(c*p.b));
s.y=((c*p.a)-(a*p.c));
s.z=((a*p.b)-(b*p.a));
return s;
}
vector vector::operator/(vector p)
{
vector s;
s.u=a*p.a+b*p.b+c*p.c;
return s;
}
void vector::display()
{
if((x<0)&&(y<0)&&(z<0))
cout<<x<<"I"<<y<<"J"<<z<<"K"<<endl;
if((x<0)&&(y<0)&&(z>=0))
cout<<x<<"I"<<y<<"J+"<<z<<"K"<<endl;
if((x<0)&&(y>=0)&&(z<0))
cout<<x<<"I+"<<y<<"J"<<z<<"K"<<endl;
if((x<0)&&(y>=0)&&(z>=0))
cout<<x<<"I+"<<y<<"J+"<<z<<"K"<<endl;
if((x>=0)&&(y<0)&&(z<0))
cout<<x<<"I"<<y<<"J"<<z<<"K"<<endl;
if((x>=0)&&(y<0)&&(z>=0))
cout<<x<<"I"<<y<<"J+"<<z<<"K"<<endl;
if((x>=0)&&(y>=0)&&(z<0))
cout<<x<<"I+"<<y<<"J"<<z<<"K"<<endl;
if((x>=0)&&(y>=0)&&(z>=0))
cout<<x<<"I+"<<y<<"J+"<<z<<"K"<<endl;
}
void vector::display1()
{
cout<<"Dot Product is:"<<u;
}
void main()
{
vector v1,v2,v3;
clrscr();
int x;
do
{
cout<<"Enter the case value : ";
cin>>x;
switch(x)
{
case 1:
v1.getdata();
v2.getdata();
v3=v1+v2;
cout<<"Vector Addition is :";
v3.display();
break;
case 2:
v1.getdata();
v2.getdata();
v3=v1-v2;
cout<<"Vector Subtraction is :";
v3.display();
break;
case 3:
v1.getdata();
v2.getdata();
v3=v1*v2;
cout<<"Vector Addition is :";
v3.display();
break;
case 4:
v1.getdata();
v2.getdata();
v3=v1/v2;
v3.display1();
break;
}
} while(x!=4);
getch();
}
OUTPUT:
Enter the case value : 1
Enter the Coefficient of vector : 1 2 3
Enter the Coefficient of vector : 4 5 6
Vector Addition is : 5I+7J+9K
Enter the case value : 2
Enter the Coefficient of vector : 4 5 6
Enter the Coefficient of vector : 1 2 3
Vector Subtraction is : 3I+3J+3K
Enter the case value : 3
Enter the Coefficient of vector : 40 50 60
Enter the Coefficient of vector: 10 20 30
Vector Addition is : 300I-600J+300K
Enter the case value : 4
Enter the Coefficient of vector : 5 6 7
Enter the Coefficient of vector : 1 2 3
Dot Product is : 38
RESULT:
Thus the above program has been executed successfully and verified.
Ex. No:07
SIMPSON’s 1/3 RULE
DATE :
AIM:
To write a C++ program to integrate a function using Simpson’s 1/3 rule.
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class simpson
{
double x[130],y[130],h,u,l,odd,even,sum;
int j,n;
public:
void getdata();
double formula(double);
void calculate();
};
void simpson::getdata()
{
cout<<"Enter the upper value of integral";
cin>>u;
cout<<"Enter the lower value of integral";
cin>>l;
cout<<"Enter the interval value";
cin>>n;
}
double simpson::formula(double x)
{
return(l/x);
}
void simpson::calculate()
{
h=(u-l)/n;
for(j=1;j<=n-1;j++)
{
x[j]=l+j*h;
y[j]=formula(x[j]);
}
odd=0;even=0;
for(j=1;j<=n-1;j=j+2)
{
odd=odd+y[j];
}
for(j=2;j<=n-1;j=j+2)
{
even=even+y[j];
}
sum=(h/3)*(formula(u)+formula(l)+4*odd+2*even);
cout<<"The integral value of (i/x) is:"<<sum<<endl;
}
void main()
{
simpson s;
clrscr();
double r;
s.getdata();
s.formula(r);
s.calculate();
getch();
}
OUTPUT:
Enter the upper value of integral2
Enter the lower value of integral1
Enter the interval value6
The integral value of (i/x) is:0.69317
RESULT:
Thus the above program has been executed successfully and verified.
Ex. No:08
GAUSS SEIDAL ITERATION METHOD
DATE :
AIM:
To write a C++ program to solve the system of equations using gauss
seidal method.
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class gauss
{
double a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12;
double a[3][4],x1,x2,x3,y1,y2,y3,z1,z2,z3;
int i,j;
public:
void getdata();
void calculation();
};
void gauss::getdata()
{
cout<<"Enter the Matrix Values:";
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
cin>>a[i][j];
}
}
void gauss::calculation()
{
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
a1=a[0][0];
a2=a[0][1];
a3=a[0][2];
a4=a[0][3];
a5=a[1][0];
a6=a[1][1];
a7=a[1][2];
a8=a[1][3];
a9=a[2][0];
a10=a[2][1];
a11=a[2][2];
a12=a[2][3];
}
}
x2=0;x3=0;
x1=(a4-(x2*a2+x3*a3))/a1;
x2=(a8-(x1*a5+x3*a7))/a6;
x3=(a12-(x1*a9+x2*a10))/a11;
y1=x1;y2=x2;y3=x3;
cout<<y1<<"\t"<<y2<<"\t"<<y3<<endl;
for(i=0;i<9;i++)
{
x1=y1;
x2=y2;
x3=y3;
x1=(a4-(x2*a2+x3*a3))/a1;
x2=(a8-(x1*a5+x3*a7))/a6;
x3=(a12-(x1*a9+x2*a10))/a11;
cout<<x1<<"\t"<<x2<<"\t"<<x3<<endl;
z1=y1;
y1=x1;
z2=y2;
y2=x2;
z3=y3;
y3=x3;
}
cout<<endl;
cout<<endl;
cout<<"x1="<<z1<<"\t"<<"x2="<<z2<<"\t"<<"x3="<<z3<<endl;
}
void main()
{
gauss g;
clrscr();
g.getdata();
g.calculation();
getch();
}
OUTPUT:
Enter the Matrix Values:
8 -3 2 20
4 11 -1 33
6 3 12 36
2.5 2.090909 1.227273
2.977273 2.028926 1.004132
3.009814 1.996807 0.995891
2.99983 1.999688 1.000163
2.999842 2.000072 1.000061
3.000012 2.000001 0.999994
3.000002 1.999999 0.999999
3 2 1
3 2 1
3 2 1
x1=3 x2=2 x3=1
RESULT:
Thus the above program has been executed successfully and verified.
Ex. No:09
RUNGE KUTTA FOURTH ORDER
DATE :
AIM:
Write a program in c++, to solve differential equations using runge kutta
fourth order method
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class runge
{
double x0,y0,d,w,t,k1,k2,k3,k4,k,temp1,temp2,x1,y1;
public:
void getdata();
double formula(double,double);
void calculation();
};
void runge::getdata()
{
cout<<"Enter the value of X :";
cin>>x0;
cout<<"Enter the value of Y :";
cin>>y0;
cout<<"Enter the value of h :";
cin>>d;
}
double runge::formula(double w,double t)
{
return (3*w+(t/2));
}
void runge::calculation()
{
cout<<"y'=(3*x+(y/2))"<<"\n";
for(int i=0;i<9;i++)
{
double x=x0;
double y=y0;
double h=d;
k1=h*formula(x,y);
k2=h*formula((x+(h/2)),(y+(k1/2)));
k3=h*formula((x+(h/2)),(y+(k2/2)));
k4=h*formula((x+h),(y+k3));
k=(k1+2*k2+2*k3+k4)/6;
y1=k+y0;
x1=x0+h;
cout<<x1<<"\t"<<k<<"\t"<<y1<<endl;
temp1=x0;
x0=x1;
temp2=y0;
y0=y1;
}
}
void main()
{
int w,t;
runge r;
clrscr();
r.getdata();
r.calculation();
r.formula(w,t);
getch();
}
OUTPUT:
Enter the value of X : 0
Enter the value of Y : 1
Enter the value of h :0.1
y'=(3*x+(y/2))
0.1 0.066524 1.066524
0.2 0.100698 1.167222
0.3 0.136623 1.303845
0.4 0.174391 1.478236
0.5 0.214095 1.69233
0.6 0.255834 1.948164
0.7 0.299714 2.247878
0.8 0.345843 2.593721
0.9 0.394337 2.988058
RESULT:
Thus the above program has been executed successfully and verified.