C++ ProGrams By Sukhdeep Singh

76
19.To Find Area of a square,a rectangle, a circle by using Function Overloading. #include<iostream.h> #include<conio.h> class circle { public: float p,r,a; void input () { cout<<"Ender The Value Of Radius"; cin>>r; } void area () { p=3.15; a=3.15*r*r; cout<<endl<<"Area Of Circle="<<a; } float area (float b) { b=r; a=b*b; cout<<endl<<"Area Of Square="<<a; return (a); } float area (float c,float d) { c=r; d=3.14; a=2*c*d; cout<<endl<<"Area Of Rectangle="<<a; return (a); }}; void main () { clrscr (); float b,c,d;

description

Hi That Programs Is Provided By Sukhdeep Singh Malhotra.

Transcript of C++ ProGrams By Sukhdeep Singh

Page 1: C++ ProGrams By Sukhdeep Singh

19.To Find Area of a square,a rectangle, a circle by using Function Overloading.

#include<iostream.h>#include<conio.h>class circle{public:float p,r,a;void input (){cout<<"Ender The Value Of Radius";cin>>r;}void area (){p=3.15;a=3.15*r*r;cout<<endl<<"Area Of Circle="<<a;}float area (float b){b=r;a=b*b;cout<<endl<<"Area Of Square="<<a;return (a);}float area (float c,float d){c=r; d=3.14;a=2*c*d;cout<<endl<<"Area Of Rectangle="<<a;return (a);}};void main (){clrscr ();float b,c,d;circle s;s.input ();s.area ();s.area (b);s.area (c,d);getch ();}

Page 2: C++ ProGrams By Sukhdeep Singh

OutPut Is:-Ender The Value Of Radius3

Area Of Circle=28.35Area Of Square=9Area Of Rectangle=18.84

Page 3: C++ ProGrams By Sukhdeep Singh

20.To illustrate concept Of Virtual Function.

#include<iostream.h>#include<conio.h>class base{public:virtual void show (){cout<<endl<<"Base Function";}virtual void display (){cout<<endl<<"Base Class";}};class der:public base{public:virtual void display (){cout<<endl<<"Derived class";}void show (){cout<<endl<<"Derived Function";}};int main (){clrscr ();base b;der d;base *k;k=&b;k->show();k->display();k=&d;k->show();k->display();getch ();return 0;}

Page 4: C++ ProGrams By Sukhdeep Singh

OutPut Is:-

Base FunctionBase ClassDerived FunctionDerived class

Page 5: C++ ProGrams By Sukhdeep Singh

21.To illustrate concept Of Static Function.

#include<iostream.h>#include<conio.h>class stat{public:int code;static int count;void sukha (){code=count++;}void sukha1 (){cout<<endl<<"Object Number:"<<code;}static void display (){cout<<endl<<"Count:="<<count;}};int stat::count;void main (){clrscr ();stat s,s1,s3;s.sukha();s1.sukha();stat::display ();s3.sukha ();stat::display ();s.sukha1 ();s1.sukha1 ();s3.sukha1 ();getch ();}

OutPut Is:-

Count:=2Count:=3Object Number:0Object Number:1Object Number:2

Page 6: C++ ProGrams By Sukhdeep Singh

22.To Implement the “this” Opearator

#include<iostream.h>#include<conio.h>#include<string.h>class sukha{public:char n[10];float a;sukha (char *s,float b){strcpy(n,s);a=b;}sukha big (sukha x){if (x.a >= a)return (x);elsereturn (*this);}void display (){cout<<"Name:="<<n<<endl;cout<<"Age="<<a;}};void main (){clrscr ();sukha p("sukha",19.50);sukha p1("sunny",18.00);sukha p2("Money",19.30);p2.big(p);cout<<endl<<"Elder Person is=";p.display ();p1.big(p);cout<<endl<<"Elder Person is=";p.display ();p1.big(p2);cout<<endl<<"Elder Person is=";p.display ();getch ();

Page 7: C++ ProGrams By Sukhdeep Singh

}OutPut Is:-

Elder Person is=Name:=sukhaAge=19.5Elder Person is=Name:=sukhaAge=19.5Elder Person is=Name:=sukhaAge=19.5

Page 8: C++ ProGrams By Sukhdeep Singh

23.To Write The name and roll no. of a student in a file with the concept of Classes with objects.

#include<iostream.h>#include<fstream.h>#include<iomanip.h>class sukha{char n[10];int c;float r;public:void input (){cout<<endl<<"Enter Name";cin>>n;cout<<"Enter code";cin>>c;cout<<"Enter Cost";cin>>r;}void output (){cout<<endl<<"Name= "<<n;cout<<endl<<"Code= "<<c;cout<<endl<<"Rate= "<<r;}};int main (){sukha k[3]; int m;fstream file;file.open("Stock",ios::in|ios::out);cout<<"Enter the No Of Items";cin>>m;cout<<"Enter Details for items";for (int i=0;i<m;i++){k[i].input();file.write((char *) & k[i],sizeof (k[i]));}file.seekg(0);cout<<endl<<"Output Is:-";

Page 9: C++ ProGrams By Sukhdeep Singh

for (i=0;i<m;i++){file.read((char *) & k[i],sizeof (k[i]));k[i].output ();}file.close ();return 0;}

OutPut Is:-

F:\TC\BIN>fileobEnter the No Of Items 3Enter Details for itemsEnter Name PhysicsEnter code 1Enter Cost 550

Enter Name ChemistryEnter code 2Enter Cost 600

Enter Name MathEnter code 3Enter Cost 450

Output Is:-Name= PhysicsCode= 1Rate= 550Name= ChemistryCode= 2Rate= 600Name= MathCode= 3Rate= 450

Page 10: C++ ProGrams By Sukhdeep Singh

24.To Insert And Delete Elements From Queue

#include<iostream.h>#include<conio.h>class stack{public:int a[10],i,n,m;char p;void input (){cout<<"Enter the Limit Of Array";cin>>n;cout<<"Enter the Elements";for (i=0;i<n;i++){cin>>a[i];}}void output (){cout<<"Choose The Action (I For Insertion and D For Deletion)";cin>>p;if (p=='d'){cout<<"Delete Action:-";for (i=1;i<n;i++){cout<<endl<<a[i];}}else if (p=='i'){cout<<"Enter the Element";cin>>m;int k;k=n+1;cout<<"Insert Action:-";for (i=0;i<k;i++){a[k-1]=m;}for (i=0;i<n+1;i++){cout<<endl<<a[i];}

Page 11: C++ ProGrams By Sukhdeep Singh

}}};void main (){clrscr ();stack s;s.input ();s.output ();getch ();}

OutPut Is:-

Enter the Limit Of Array5Enter the Elements12345Choose The Action (I For Insertion and D For Deletion)iEnter the Element6Insert Action:-123456

Page 12: C++ ProGrams By Sukhdeep Singh

25.To Finds The Roots of An equation using Bisection Method.

#include<iostream.h>#include<conio.h>#include<process.h>float g(float t){return(t*t*t-t-4);}class bi{public:float f,f1,f2,x,x1,x2,e;int i;void input (){cout<<"Enter the Intial approximation x1,x2";cin>>x>>x1;cout<<"Enter a Very Small number epsilno:";cin>>e;}void output (){f=g(x);f1=g(x1);if (f*f1>0){cout<<endl<<"Intial Approximation x= "<<x<<" x1="<<x1<<"are not proper";exit (0);}i=0;do{x2=(x+x1)/2;i++;cout<<endl<<"Next Approximation after "<<i<<" iteration is "<<x2;f2=g(x2);if (f2<0){x=x2;}else{x1=x2;}

Page 13: C++ ProGrams By Sukhdeep Singh

}while (((x1-x)>e) && (f2!=0));cout<<endl<<"Root Is=="<<x2;}};void main (){clrscr ();bi h;h.input ();h.output ();getch ();}

OutPut Is:-

Enter the Intial approximation x1,x212Enter a Very Small number epsilno:0.001

Next Approximation after 1 iteration is 1.5Next Approximation after 2 iteration is 1.75Next Approximation after 3 iteration is 1.875Next Approximation after 4 iteration is 1.8125Next Approximation after 5 iteration is 1.78125Next Approximation after 6 iteration is 1.796875Next Approximation after 7 iteration is 1.789062Next Approximation after 8 iteration is 1.792969Next Approximation after 9 iteration is 1.794922Next Approximation after 10 iteration is 1.795898Root Is==1.795898

26.To Finds The Roots of An equation using Regula Falsi Method

Page 14: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>#include<process.h>float g(float t){return(t*t*t-t-4);}class bi{public:float f,f1,f2,x,x1,x2,e;int i;void input (){cout<<"Enter the Intial approximation x1,x2";cin>>x>>x1;cout<<"Enter a Very Small number epsilno:";cin>>e;}void output (){f=g(x);f1=g(x1);if (f*f1>0){cout<<endl<<"Intial Approximation x= "<<x<<" x1="<<x1<<"are not proper";exit (0);}i=0;do{x2=((f*x1)-(f1*x))/(f-f1);i++;cout<<endl<<"Next Approximation after "<<i<<" iteration is "<<x2;f2=g(x2);if (f2<0){x=x2;}else{x1=x2;}}

Page 15: C++ ProGrams By Sukhdeep Singh

while (((x1-x)>e) && (f2!=0));cout<<endl<<"Root Is=="<<x2;}};void main (){clrscr ();bi h;h.input ();h.output ();getch ();}

OutPut Is:-

Enter the Intial approximation x1,x212Enter a Very Small number epsilno:0.001

Next Approximation after 1 iteration is 1.666667Next Approximation after 2 iteration is 1.888889Next Approximation after 3 iteration is 1.814815Next Approximation after 4 iteration is 1.765432Next Approximation after 5 iteration is 1.798354Next Approximation after 6 iteration is 1.78738Next Approximation after 7 iteration is 1.794696Next Approximation after 8 iteration is 1.797135Next Approximation after 9 iteration is 1.796322Root Is==1.796322

27.To Finds The Roots of An equation using Newton Raphson Method

Page 16: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>#include<process.h>float g(float t){return(t*t*t-t-4);}float j(float t1){return(3*t1*t1-1);}class bi{public:float f,f1,f2,x,x1,x2,e;int i,n;void input (){cout<<"Enter the Intial approximation x1";cin>>x;cout<<"Enter the No. Of Iterations";cin>>n;}void output (){f=g(x);f1=j(x);i=0;for (i=0;i<n;i++){x2=(x-(f/f1));cout<<endl<<"Next Approximation after "<<i<<" iteration is "<<x2;f=g(x2);f1=j(x2);x=x2;}cout<<endl<<"Root Is=="<<x2;}};void main (){clrscr ();bi h;h.input ();

Page 17: C++ ProGrams By Sukhdeep Singh

h.output ();getch ();}

OutPut Is:-

Enter the Intial approximation x12Enter the No. Of Iterations5

Next Approximation after 0 iteration is 1.818182Next Approximation after 1 iteration is 1.796613Next Approximation after 2 iteration is 1.796322Next Approximation after 3 iteration is 1.796322Next Approximation after 4 iteration is 1.796322Root Is==1.796322

28.To Interpolate a Value Using Lagrangian Interpolation Method

Page 18: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>class sukha{public:float a[10],b[10],i,n,m,j,x,s,t;void input (){cout<<"Enter the limit Of Series";cin>>n;cout<<"Enter the Elements Of x And y(x)";for (i=0;i<n;i++){cin>>a[i]>>b[i];}cout<<"Enter the Value Of x";cin>>x;};void output (){s=0;for (j=0;j<n;j++){m=1;for (i=0;i<n;i++){if (i!=j){m=m*((x-a[i])/(a[j]-a[i]));}}t=b[j]*m;s=s+t;}cout<<endl<<"Interpolation Is=="<<s;}};void main (){clrscr ();sukha g;g.input ();g.output ();getch ();

Page 19: C++ ProGrams By Sukhdeep Singh

}

OutPut Is:-

Enter the limit Of Series6Enter the Elements Of x And y(x)1 62 83 104 125 146 16Enter the Value Of x3.5

Interpolation Is==11

29.To Interpolate a Value Using Newton Forward Difference Method.

Page 20: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>class sukha{public:float a,d[10][10],x[10],y[10],i,k,j,m,l,u,s,n,p;void input (){cout<<"Enter the Value Of N";cin>>n;cout<<endl<<"Enter "<<n<<" Pairs of (x,y)";for (i=0;i<n;i++){cin>>x[i]>>y[i];}cout<<"Enter the Value Of X to interpollate the value of Y:";cin>>a;}void output (){i=1;while (a<x[i]){i++;}k=i-1;u=((a-x[k])/(x[k+1]-x[k]));for (j=0;j<n-1;j++){for (i=0;i<n-j;i++){if (j==0){d[i][j]=y[i+1]-y[i];}else{d[i][j]=d[i+1][j-1]-d[i][j-1];}}}s=y[k];for (i=0;i<n-k;i++){

Page 21: C++ ProGrams By Sukhdeep Singh

p=1;for (j=0;j<=i;j++){p=p*(u-j)/(j+1);s=s+d[k][i]*p;}}cout<<endl<< “Interpolation Is:- “<<s;}};void main (){sukha o;clrscr ();o.input ();o.output ();getch ();}

OutPut Is:-

Enter the Value Of N5

Enter 5 Pairs of (x,y)1 23 45 67 89 10Enter the Value Of X to interpollate the value of Y:2.5

Interpolation Is:- 2.777778

30.To Solve Numerical Integration using Trapezodial Rule.

Page 22: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>class tpd{public:float a[10],b[10],n,m,h,c,d,e,f,g,j;float i;void input (){cout<<"Enter the Value Of a and b";cin>>n>>m;cout<<"Enter the Value Of n";cin>>c;}void output (){cout<<"xi yi";for (i=0;i<=c;i++){h=(m-n)/c;a[i]=(1/(1+(i*i)));cout<<endl<<"x"<<i+1<<"="<<i<<" "<<a[i];}e=a[0]+a[c];cout<<endl<<"Sum Of y1 and y"<<c+1<<"="<<e<<endl;f=0;cout<<"Sum Of y2 to y"<<c-1<<" is:-"<<endl;for (i=1;i<=c-1;i++){f=f+a[i];cout<<a[i]<<"+";}g=f*2;j=(h/2)*(e+g);cout<<endl<<"Answer Is:-"<<j;}};void main (){clrscr ();tpd t;t.input ();t.output ();getch ();

Page 23: C++ ProGrams By Sukhdeep Singh

}

OutPut is:-

Enter the Value Of a and b12Enter the Value Of n6xi yix1=0 1x2=1 0.5x3=2 0.2x4=3 0.1x5=4 0.058824x6=5 0.038462x7=6 0.027027Sum Of y1 and y7=1.027027Sum Of y2 to y5 is:-0.5+0.2+0.1+0.058824+0.038462Answer Is:- 0.235133

31.To Find Arithmetic Mean Of N Number.

Page 24: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>class gm{public:float a[10],b[10],c[10],d[10],e[10],n[10],i,m,s,z,z1,z2;void input (){cout<<"Enter the No. of Elements";cin>>m;cout<<"Enter the Value of X series and f";for (i=0;i<m;i++){cin>>a[i]>>b[i]>>d[i];}}void output (){s=0;z=0;cout<<" X f m fm";for (i=0;i<m;i++){s=s+d[i];c[i]=((a[i]+b[i]))/2;e[i]=d[i]*c[i];z=z+e[i];cout<<endl<<a[i]<<"-"<<b[i]<<" "<<" "<<d[i]<<" "<<c[i]<<" "<<e[i];}z1=z/s;cout<<endl<<"The Value of N is="<<s;cout<<endl<<"The Sum Of fm is="<<z;cout<<endl<<"Arithmetic Mean is="<<z1;}};void main (){clrscr ();gm g;g.input();g.output ();getch ();}

OutPut is:-

Page 25: C++ ProGrams By Sukhdeep Singh

Enter the No. of Elements5Enter the Value of X series and f10 20 220 30 330 40 440 50 550 60 6 X f m fm10-20 2 15 3020-30 3 25 7530-40 4 35 14040-50 5 45 22550-60 6 55 330The Value of N is=20The Sum Of fm is=800Arithmetic Mean is=40

32.To Find Geometric Mean Of N Number.

Page 26: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>#include<math.h>class gm{public:float a[10],b[10],c[10],d[10],e[10],n[10],i,m,s,z,z1,z2;void input (){cout<<"Enter the No. of Elements";cin>>m;cout<<"Enter the Value of X series and f";for (i=0;i<m;i++){cin>>a[i]>>b[i]>>d[i];}}void output (){s=0;z=0;cout<<" X f m logm flogm";for (i=0;i<m;i++){s=s+d[i];c[i]=((a[i]+b[i]))/2;n[i]=log10(c[i]);e[i]=n[i]*d[i];z=z+e[i];cout<<endl<<a[i]<<"-"<<b[i]<<" "<<" "<<d[i]<<" "<<c[i]<<" "<<n[i]<<" "<<e[i];}z2=z/s;cout<<endl<<"Value Of N="<<s;z1=exp(z2*log(10));cout<<endl<<"Sum Of fLog(m) is=="<<z;cout<<endl<<"G.M is:-Antilog("<<z2<<")=="<<z1;}};void main (){clrscr ();gm g;g.input();g.output ();getch ();

Page 27: C++ ProGrams By Sukhdeep Singh

}

OutPut Is:-

Enter the No. of Elements5Enter the Value of X series and f0 1 21 2 32 3 43 4 54 5 6 X f m logm flogm0-1 2 0.5 -0.30103 -0.602061-2 3 1.5 0.176091 0.5282742-3 4 2.5 0.39794 1.591763-4 5 3.5 0.544068 2.720344-5 6 4.5 0.653212 3.919275Value Of N=20Sum Of fLog(m) is==8.157589G.M is:-Antilog(0.407879)==2.557876

33.To Find Harmonic Mean Of N Number.

Page 28: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>class gm{public:float a[10],b[10],c[10],d[10],e[10],n[10],i,m,s,z,z1,z2;void input (){cout<<"Enter the No. of Elements";cin>>m;cout<<"Enter the Value of X series and f";for (i=0;i<m;i++){cin>>a[i]>>b[i]>>d[i];}}void output (){s=0;z=0;cout<<"X f m f/m";for (i=0;i<m;i++){s=s+d[i];c[i]=((a[i]+b[i]))/2;e[i]=d[i]/c[i];z=z+e[i];cout<<endl<<a[i]<<"-"<<b[i]<<" "<<" "<<d[i]<<" "<<c[i]<<" "<<e[i];}z1=s/z;cout<<endl<<"The Value of N is="<<s;cout<<endl<<"The Sum Of f/m is="<<z;cout<<endl<<"Harmonic Mean is="<<z1;}};void main (){clrscr ();gm g;g.input();g.output ();getch ();}

OutPut Is:-

Page 29: C++ ProGrams By Sukhdeep Singh

Enter the No. of Elements5Enter the Value of X series and f

10 20 220 30 530 40 440 50 650 60 7X f m f/m10-20 2 15 0.13333320-30 5 25 0.230-40 4 35 0.11428640-50 6 45 0.13333350-60 7 55 0.127273The Value of N is=24The Sum Of f/m is=0.708225Harmonic Mean is=33.887531

34.To Find Median Of a list Of Elements.

Page 30: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>class sukha{public:int a[20],b[20],c[20],e[10],i,n,d,g,h,z,m;void input (){cout<<"Enter the Value Of N";cin>>n;cout<<"Enter the Value of x and f";for (i=0;i<n;i++){cin>>a[i]>>b[i]>>c[i];}}void output (){d=0;e[0]=c[0];cout<<" X f c.f";for (i=0;i<n;i++){d=d+c[i];e[i]=e[i-1]+c[i];cout<<endl<<a[i]<<"-"<<b[i]<<" "<<c[i]<<" "<<e[i];}g=d/2;cout<<endl<<"Value of N/2 is= "<<g;z=0;for(i=0;e[i]<g;i++){z++;}cout<<endl<<"Model Class Is="<<a[z]<<"-"<<b[z];cout<<endl<<"The Value Of l is="<<a[z];cout<<endl<<"Value Of c.f is="<<e[z-1];cout<<endl<<"Value Of f is="<<c[z];m=a[z]+(((g-e[z-1])*(b[z]-a[z]))/c[z]);cout<<endl<<"Median Is=="<<m;}};void main (){clrscr ();

Page 31: C++ ProGrams By Sukhdeep Singh

sukha s;s.input ();s.output ();getch ();}

OutPut Is:-

Enter the Value Of N5Enter the Value of x and f10 20 120 30 230 40 340 50 450 60 5 X f c.f10-20 1 120-30 2 330-40 3 640-50 4 1050-60 5 15Value of N/2 is= 7Model Class Is=40-50The Value Of l is=40Value Of c.f is=6Value Of f is=4Median Is==42

35. To Find Mode Of a list Of Elements.

Page 32: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>class sukha{public:float a[20],b[20],c[20],e[10],i,n,d,g,h,z,z1,m,h1;void input (){cout<<"Enter the Value Of N";cin>>n;cout<<"Enter the Value of x and f";for (i=0;i<n;i++){cin>>a[i]>>b[i]>>c[i];}}void output (){d=0;cout<<" X f";for (i=0;i<n;i++){cout<<endl<<a[i]<<"-"<<b[i]<<" "<<c[i]<<" ";}g=n/2;cout<<endl<<"Value of N/2 is= "<<g;cout<<endl<<"Model Class Is="<<a[g]<<"-"<<b[g];cout<<endl<<"The Value Of l is="<<a[g];cout<<endl<<"Value Of f0 is="<<c[g-1];cout<<endl<<"Value Of f1 is="<<c[g];cout<<endl<<"Value Of f2 is="<<c[g+1];z=(c[g]-c[g-1])+(c[g]-c[g+1]);m=((c[g]-c[g-1])*(b[0]-a[0]));h=m/z;h1=a[g]+h;cout<<endl<<"Mode Is=="<<h1;}};void main (){clrscr ();sukha s;s.input ();s.output ();

Page 33: C++ ProGrams By Sukhdeep Singh

getch ();}

OutPut Is:-

Enter the Value of x and f0 10 510 20 1520 30 3030 40 5540 50 8550 60 6060 70 4570 80 25 X f 0-10 510-20 1520-30 3030-40 5540-50 8550-60 6060-70 4570-80 25Value of N/2 is= 4Model Class Is=40-50The Value Of l is=40Value Of f0 is=55Value Of f1 is=85Value Of f2 is=60Mode Is==45.454544

36. To Find Mean Deviation.

Page 34: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>class md{public:int a[10],b[10],n,i,d,f,sum;float s,s1;void input (){cout<<"Enter the Value of Series";cin>>n;cout<<"Enter the Elements of Series";for (i=0;i<n;i++){cin>>a[i];}}void output (){sum=0;for (i=0;i<n;i++){sum=sum+a[i];}d=sum/n;cout<<endl<<"Mean "<<d;cout<<endl<<"Deviations Taken From Mean";for (i=0;i<n;i++){b[i]=d-a[i];if (b[i]<0)b[i]=-b[i];s1=s1+b[i];cout<<endl<<b[i];}cout<<endl<<"Sum Of Deviations "<<s1;s=s1/n;cout<<endl<<"Mean Deviation=="<<s;}};void main (){clrscr ();md k;

Page 35: C++ ProGrams By Sukhdeep Singh

k.input ();k.output ();getch ();}

OutPut Is:-

Enter the Value of Series5Enter the Elements of Series13456

Mean 3Deviations Taken From Mean20123Sum Of Deviations 8Mean Deviation==1.6

37. To Find Standard Deviation.

Page 36: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>#include<math.h>class md{public:float a[10],b[10],n,i,d,f,sum;float s,s1;void input (){cout<<"Enter the Value of Series";cin>>n;cout<<"Enter the Elements of Series";for (i=0;i<n;i++){cin>>a[i];}}void output (){sum=0;for (i=0;i<n;i++){sum=sum+a[i];}d=sum/n;cout<<endl<<"Mean="<<d;cout<<endl<<"Deviations Taken From Mean";for (i=0;i<n;i++){b[i]=pow(a[i]-d,2);s1=s1+b[i];cout<<endl<<b[i];}cout<<endl<<"Sum Of Deviations "<<s1;s=sqrt(s1/n);cout<<endl<<"Mean Deviation=="<<s;}};void main (){clrscr ();md k;k.input ();

Page 37: C++ ProGrams By Sukhdeep Singh

k.output ();getch ();}

OutPut Is:-

Enter the Value of Series5Enter the Elements of Series12365

Mean=3.4Deviations Taken From Mean5.761.960.166.7599992.56Sum Of Deviations 17.199999Mean Deviation==1.854724

38.To Find Coefficient Of Correlation.

Page 38: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>#include<math.h>class cor{public:float a[10],b[10],g[10],h[10],j[10],n,m,i,i1,c,c1;float d,d1,s,s1,e[10],f[10],y,z,r;void input (){cout<<"Enter the Value For X series and Y Series";cin>>n;cout<<"Enter the Elements of X And Y Series";for (i=0;i<n;i++){cin>>a[i]>>b[i];}}void output (){s=0;for (i=0;i<n;i++){s=s+a[i];}d=s/n;s1=0;for (i=0;i<n;i++){s1=s1+b[i];}d1=s1/n;cout<<"X Y x(square)("<<d<<")"<<" "<<"y(square)("<<d1<<")"<<" "<<"xy"<<endl;i1=0;c1=0,m=0;for (i=0;i<n;i++){g[i]=a[i]-d;e[i]=pow(a[i]-d,2);i1=i1+e[i];h[i]=b[i]-d1;f[i]=pow(b[i]-d1,2);c1=c1+f[i];j[i]=g[i]*h[i];m=m+j[i];cout<<a[i]<<" "<<" "<<b[i]<<" "<<e[i]<<" "<<f[i]<<" "<<j[i]<<endl;

Page 39: C++ ProGrams By Sukhdeep Singh

}y=sqrt(i1);z=sqrt(c1);r=m/(y*z);cout<<endl<<"Sum Of X Series="<<s;cout<<endl<<"Sum Of Y Series="<<s1;cout<<endl<<"Sum Of x(square) Series="<<i1;cout<<endl<<"Sum Of y(square) Series="<<c1;cout<<endl<<"Coefficient of Correlation=="<<r;}};void main (){clrscr ();cor f;f.input ();f.output ();getch ();}

OutPut Is:-

Enter the Value For X series and Y Series5Enter the Elements of X And Y Series10 2030 1220 1342 1232 12X Y x(square)(26.799999) y(square)(13.8) xy10 20 282.23996 38.439999 -104.15998830 12 10.240005 3.240001 -5.76000220 13 46.23999 0.64 5.44000142 12 231.040024 3.240001 -27.36000432 12 27.040009 3.240001 -9.360003

Sum Of X Series=134Sum Of Y Series=69Sum Of x(square) Series=596.799927Sum Of y(square) Series=48.800003Coefficient of Correlation==-0.82739

39.To illustrate concept Of Polymorphism

Page 40: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>class sukha{public:int a,b;void input (){cout<<"Enter the Value Of A and B";cin>>a>>b;}virtual void output (){cout<<endl<<"A="<<a<<endl<<"B="<<b;}};class sukha1:public sukha{public:int c;void output (){cout<<endl<<"In Derived Class"<<endl;}void sum (){c=a+b;cout<<endl<<"Sum Is="<<c;}};void main (){clrscr ();sukha *s,s1;sukha1 k;s=&s1;s->input();s->output();k.output();((sukha1 *)s)->sum();getch ();}OutPut Is:-

Page 41: C++ ProGrams By Sukhdeep Singh

Enter the Value Of A and B12

A=1B=2In Derived Class

Sum Is=3

40.To find Solution Of Simultaneous equations using Guass Elimination Method.

Page 42: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>#include<process.h>class sukha{public:float n,m,i,j,k,a[10][10],x[10],s,t,e,b,u;void input (){cout<<"Enter the No. of Equation";cin>>n;for (i=0;i<n;i++){ for (j=0;j<n+1;j++) {cin>>a[i][j]; }}}void output (){for (k=0;k<n-1;k++){if(a[k][k]==0){cout<<endl<<"Division by Zero";exit (0);}for (i=k+1;i<n;i++){u=a[i][k]/a[k][k];for (j=0;j<n+1;j++) a[i][j]=a[i][j]-(u*a[k][j]);}}for (i=n-1;i>=0;i--){s=0;for (j=i+1;j<n;j++)s=s+a[i][j]*x[j];x[i]=(a[i][n]-s)/a[i][i];}cout<<endl<<"Solution Of System Of Linear Equation";for (i=0;i<n;i++)

Page 43: C++ ProGrams By Sukhdeep Singh

{cout<<endl<<"x["<<i+1<<"]="<<x[i];}} };void main (){clrscr ();sukha g;g.input ();g.output ();getch ();}

OutPut Is:-

Enter the No. of Equation32 -3 1 -11 4 5 253 -4 1 2

Solution Of System Of Linear Equationx[1]=8.699999x[2]=5.7x[3]=-1.3

41.To find Solution Of Simultaneous equations using Guass Jorden Method.

Page 44: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>#include<process.h>class sukha{public:float n,m,i,j,k,a[10][10],x[10],s,t,e,b,u;void input (){cout<<"Enter the No. of Equation";cin>>n;for (i=0;i<n;i++){ for (j=0;j<n+1;j++) {cin>>a[i][j]; }}}void output (){for (k=0;k<n;k++){if(a[k][k]==0){cout<<endl<<"Division by Zero";exit (0);}t=a[k][k];for (j=k;j<n+1;j++)a[k][j]=a[k][j]/t;for (i=0;i<n;i++){if (i!=k) {u=a[i][k];for (j=k;j<n+1;j++) a[i][j]=a[i][j]-(u*a[k][j]);}}}cout<<endl<<"Solution Of System Of Linear Equation";for (i=0;i<n;i++){

Page 45: C++ ProGrams By Sukhdeep Singh

x[i]=a[i][n];cout<<endl<<"x["<<i+1<<"]="<<x[i];}} };void main (){clrscr ();sukha g;g.input ();g.output ();getch ();}

OutPut Is:-Enter the No. of Equation32 -3 1 -11 4 5 253 -4 1 2

Solution Of System Of Linear Equationx[1]=8.7x[2]=5.7x[3]=-1.3

42. To find Solution Of Simultaneous equations using Guass Seidel Method.

Page 46: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>#include<process.h>class sukha{public:float n,m,i,j,k,a[10][10],x[10],s,t,e,b;void input (){cout<<"Enter the No. of Equation";cin>>n;for (i=0;i<n;i++){ for (j=0;j<n+1;j++) {cin>>a[i][j]; }}cout<<endl<<"Enter the Max. No Iterations";cin>>m;cout<<endl<<"Enter the Very Small No. Of epsilon:";cin>>e;}void output (){for (i=0;i<n;i++)x[i]=0;for (k=1;k<=m;k++) {b=0;for (i=0;i<n;i++) {s=0;for (j=0;j<n;j++) {if (i!=j)s=s+a[i][j]*x[j]; }t=(a[i][n]-s)/a[i][i];e=((t-x[i])/t);if (e>b)b=e;x[i]=t;}

Page 47: C++ ProGrams By Sukhdeep Singh

if (b<=e){cout<<endl<<"Solution Converge After "<<k<<" iteration";for (i=0;i<n;i++)cout<<endl<<"x["<<i+1<<"]="<<x[i];}}} };void main (){clrscr ();sukha g;g.input ();g.output ();getch ();}

OutPut Is:-Enter the No. of Equation35 2 1 121 4 2 151 2 5 20Enter the Max. No Iterations15Enter the Very Small No. Of epsilon0.01

Solution Converge After 1 iterationx[1]=2.4x[2]=3.15x[3]=2.26Solution Converge After 2 iterationx[1]=0.688x[2]=2.448x[3]=2.8832Solution Converge After 11 iterationx[1]=1x[2]=2x[3]=3Solution Converge After 12 iterationx[1]=1x[2]=2x[3]=3

Page 48: C++ ProGrams By Sukhdeep Singh

Solution Converge After 14 iterationx[1]=1x[2]=2x[3]=3Solution Converge After 15 iterationx[1]=1x[2]=2x[3]=3

43.To Fit A Polynomial Curve.

Page 49: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>class sukha{public:float i,n,x[10],y[10],s,s1,s2,s3,d,d1,d2,a,b;void input (){cout<<"Enter the Value Of N";cin>>n;cout<<endl<<"Enter "<<n<<" Pairs Of x y";s=s1=s2=s3=0;for (i=0;i<n;i++){cin>>x[i]>>y[i];}}void output (){for (i=0;i<n;i++){s=s+x[i];s1=s1+y[i];s2=s2+(x[i]*x[i]);s3=s3+(x[i]*y[i]);}d=(n*s2)-(s*s);d1=(s1*s2)-(s3*s);d2=(n*s3)-(s*s1);a=d1/d;b=d2/d;cout<<endl<<"A= "<<a<<endl<<"B= "<<b;cout<<endl<<"Straight Line Is Of Form"<<endl<<"Y= "<<a<<"+"<<b<<" X";}};void main (){clrscr ();sukha h;h.input();h.output();getch ();}OutPut Is:-

Page 50: C++ ProGrams By Sukhdeep Singh

Enter the Value Of N5

Enter 5 Pairs Of x y1 92 93 104 125 11

A= 8.1B= 0.7Straight Line Is Of FormY= 8.1+0.7 X

44.To Illustrate The Concept Of Friend Function.

Page 51: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>class pri{int a,b;public:void input (){cout<<"Enter the Value of A and B";cin>>a>>b;}friend int output (pri f);};int output (pri f){return int(f.a+f.b);}void main (){clrscr ();pri j;j.input();cout<<"Sum Is="<<output(j);getch ();}

OutPut Is:-

Enter the Value of A and B56Sum Is=11

45.To Insert And Delete Elements From Stack

Page 52: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>class stack{public:int a[10],i,n,m;char p;void input (){cout<<"Enter the Limit Of Array";cin>>n;cout<<"Enter the Elements";for (i=0;i<n;i++){cin>>a[i];}}void output (){cout<<"Choose The Action (I For PUSH and D For POP)";cin>>p;if (p=='d'){cout<<"POP Action:-";for (i=0;i<n-1;i++){cout<<endl<<a[i];}}else if (p=='i'){cout<<"Enter the Element";cin>>m;cout<<"Push Action:-";for (i=0;i<n;i++){a[n]=m;n=n+1;}for (i=0;i<n-1;i++){cout<<endl<<a[i];}}

Page 53: C++ ProGrams By Sukhdeep Singh

}};void main (){clrscr ();stack s;s.input ();s.output ();getch ();}

OutPut Is:-

Enter the Limit Of Array5Enter the Elements13654Choose The Action (I For PUSH and D For POP)iEnter the Element7Push Action:-136547

Page 54: C++ ProGrams By Sukhdeep Singh

46.To Solve Numarical Differentiation

#include<iostream.h>#include<conio.h>#include<math.h>#include<iomanip.h>class sukha{public:float a,d[20][20],x[20],y[20],i,k,j,m,l,u,s,n,p;void input (){ cout<<"Enter the Value Of N"; cin>>n; cout<<endl<<"Enter "<<n<<" Pairs of (x,y)"; for (i=0;i<n;i++) { cin>>x[i]>>y[i]; } cout<<"Enter the Value Of X to interpollate the value of Y:"; cin>>a;}void output (){i=1;while (a<x[i]){ i++;}k=i-1;float h=x[1]-x[0];double z=0;for (j=0;j<n-1;j++){for (i=0;i<n-j;i++){ if (j==0) { d[i][j]=y[i+1]-y[i]; } else { d[i][j]=d[i+1][j-1]-d[i][j-1]; }

Page 55: C++ ProGrams By Sukhdeep Singh

}z+=pow(-1,j)*d[k][j];}

double dy;cout<<setprecision(3);dy=z/h;cout<<endl<<"Differentiation="<<dy;

}};void main (){sukha o;clrscr ();o.input ();o.output ();getch ();}

OutPut Is:-

Enter the Value Of N3

Enter 3 Pairs of (x,y)10 2015 2520 30Enter the Value Of X to interpollate the value of Y:15.5

Differentiation=1

47.To Show Different Error Messages By Using Error Handling Function in Files.

Page 56: C++ ProGrams By Sukhdeep Singh

#include<fstream.h>#include<stdlib.h>int main(){ void report(ofstream &); ofstream file; file.open("sample.exe",ios::noreplace); if(!file) { report (file); exit(1); } else { } file.close(); return 0;}void report(ofstream &file){ cout<<"Unalbe to open SAMPLE.EXE"<<endl; cout<<"good()="<<file.good()<<endl; cout<<"bad()="<<file.bad()<<endl; cout<<"fail()="<<file.fail()<<endl; cout<<"eof()="<<file.eof()<<endl;}

OutPut Is:-

F:\TC\BIN>26Unalbe to open SAMPLE.TXTgood()=0bad()=4fail()=4eof()=0

48.To Create and Implement various operations on a Linked List

Page 57: C++ ProGrams By Sukhdeep Singh

#include<iostream.h>#include<conio.h>class linklist{private:struct node{int info;node *link;}*l;public:linklist(){l=NULL;}void append(int num);void addatbeg(int num);void addafter(int c,int num);void del(int num);void display();int count();~linklist();};void linklist :: append(int num){node *q,*t;if (l==NULL){l=new node;l->info=num;l->link=NULL;}else{q=l;while(q->link!=NULL)q=q->link;t=new node;t->info=num;t->link=NULL;q->link;}}

Page 58: C++ ProGrams By Sukhdeep Singh

void linklist :: addatbeg(int num){node *q;q=new node;q->info=num;q->link=l;l=q;}void linklist :: addafter(int c,int num){node *q,*t;int i;for (i=0,q=l;i<c;i++){q=q->link;if (q==NULL){cout<<"There are Less than "<<c<<"Elements"<<endl;return;}}t=new node;t->info=num;t->link=q->link;q->link=t;}void linklist :: del(int num){node *q,*r;q=l;if (q->info==num){l=q->link;delete q;return;}r=q;while(q!=NULL){if(q->info==num){r->link=q->link;delete q;return;}r=q;

Page 59: C++ ProGrams By Sukhdeep Singh

q=q->link;}cout<<endl<<"Element"<<num<<"not found"<<endl;}void linklist :: display(){node *q;cout<<endl;for (q=l;q!=NULL;q=q->link)cout<<endl<<q->info;}int linklist :: count(){node *q;int c=0;for (q=l;q!=NULL;q=q->link)c++;return (c);}linklist :: ~linklist(){node *q;if (l==NULL)return;while(l!=NULL){q=l->link;delete l;l=q;}}void main(){linklist ll;clrscr();cout<<" Number of Element in Linked list::::::"<<ll.count();ll.append(1);ll.append(2);ll.append(3);ll.append(4);ll.append(5);ll.append(6);cout<<endl<<"Number of Element in linked list After Appending::::::"<<ll.count();ll.display();cout<<endl;ll.addatbeg(10);ll.addatbeg(20);

Page 60: C++ ProGrams By Sukhdeep Singh

ll.addatbeg(30);ll.addatbeg(40);cout<<endl<<"Number of Element in linked list after Adding at the Begining::::::"<<ll.count()<<endl;ll.display();ll.addafter(3,600);ll.addafter(6,600);cout<<endl<<"Number of Elements in linked list after Adding in the End:::::::"<<ll.count();ll.display();ll.del(30);ll.del(6);ll.del(0);cout<<endl<<"Number of Elements in linked list After Deletion::::::"<<ll.count();ll.display();getch();}

OutPut Is:-

Number of Element in Linked list::::::0Number of Element in linked list After Appending::::::1

1

Number of Element in linked list after Adding at the Begining::::::5403020101There are Less than 6Elements

Number of Elements in linked list after Adding in the End:::::::6403020106001Element6not found

Element0not found

Number of Elements in linked list After Deletion::::::5

Page 61: C++ ProGrams By Sukhdeep Singh

4020106001

49.To Illustrate the example of New And Delete Operators.

#include<iostream.h>

Page 62: C++ ProGrams By Sukhdeep Singh

#include<conio.h>class Base{private:int a,b,sum;public:void getdata();void putdata();void Sum();};void Base :: getdata(){cout<<"Enter value of A and b";cin>>a>>b;}void Base :: putdata(){cout<<"Sum=="<<sum;}void Base :: Sum(){sum=a+b;}void main(){Base *pt;clrscr();pt=new Base;pt->getdata();pt->Sum();pt->putdata();delete pt;getch();}

OutPut Is:-Enter value of A and b2320Sum==43

Page 63: C++ ProGrams By Sukhdeep Singh