Note.....

Dear readers I Have collected these Programs from various sources of internet...I don't know what the header file is Please mail me if any error found hi2rashid@yahoo.co.in

Sponsored....

Tuesday, April 13, 2010

C++ program for newton backward interpolation formula

//program for newton backward interpolation formula
#include
#include
#include
#define maxn 100
#define order 4
void main()
{
float ax[maxn+1],ay[maxn+1],diff[maxn+1][order+1],nr=1,dr=1,x,p,h,yp;
int n,i,j,k;
clrscr();
cout<<"enter the value of n\n";
cin>>n;
cout<<"enter the values in form x,y\n";
for(i=1;i<=n;i++)
{cin>>ax[i]>>ay[i];}
cout<<"enter the value of x for which value of y is wanted\n";
cin>>x;
h=ax[2]-ax[1];
for(i=n;i>=1;i--)
{diff[i][1]=ay[i]-ay[i-1];}
for(j=2;j<=order;j++)
{
for(i=n;i>j;i--)
{diff[i][j]=diff[i][j-1]-diff[i-1][j-1];}
}
i=n;
p=(x-ax[i])/h;
yp=ay[i];
for(k=1;k<=order;k++)
{
nr*=p+k-1;
dr*=k;
yp+=(nr/dr)*diff[i][k];
}
cout<<"when x = "<<<" y = "<
getch();
}

output:
enter the value of n
5
enter the values in form x,y
80 5026
85 5674
90 6362
95 7088
100 7854
enter the value of x for which value of y is wanted
105
when x = 105 y = 8666

C++ Program to solve linear equation (3 variable) by gauss elimination method

//program to solve linear equation (3 variable) by gauss elimination method
#include
#include
#include
#include
#include
#define r 3
#define c 4
void main()
{
clrscr();
float a[r][c];
int i,j;
textcolor(GREEN);
cprintf("PROGRAM TO SOLVE LINEAR EQUATIONS BY GAUSS ELIMINATION
METHOD");
cout<<"\n";
textcolor(BLUE);
cprintf("enter the values in given format");
cout<<"\n";
cprintf("a[0][0] x + a[0][1] y + a[0][2] z = a[0][3]");
cout<<"\n";
for( i=0;i
{
for( j=0;j
{
cout<<"enter a["<<<"]["<<<"] : ";
cin>>a[i][j];
cout<<"\n";
}
}
delay(10);
step:
clrscr();
cout<<"the values entered are :\n";
for( i=0;i
{
for(j=0;j
{
cout<
cout<<"\t";
}
cout<<"\n";
}

float temp1=a[0][0];
for( j=0;j
{

a[0][j]=a[0][j]/temp1;
}
cout<<"the values after step 1 :\n";
cprintf("on dividing R1 by ");cout<<<"\n";
for( i=0;i
{
for(int j=0;j
{
cout<
cout<<"\t";
}
cout<<"\n";
}

float temp2=a[1][0];
for( j=0;j
{a[1][j]=a[1][j]-temp2*a[0][j]; }
cout<<"the values after step 2 :\n";
cprintf("on R2 - ");cout<<<"*R1\n";
for( i=0;i
{
for(int j=0;j
{
cout<
cout<<"\t";
}
cout<<"\n";
}

float temp3=a[2][0];
for( j=0;j
{a[2][j]=a[2][j]-temp3*a[0][j];}
cout<<"the values after step 3 :\n";
textcolor(GREEN);
cprintf("on R3 - ");cout<<<"*R1\n";
for( i=0;i
{
for(int j=0;j
{
cout<
cout<<"\t";
}
cout<<"\n";
}

float temp4=a[2][1];
for(j=0;j
a[2][j]=a[2][j]-temp4/a[1][1]*a[1][j];
cout<<"the values after step 4 :\n";
cprintf("on R3 - ");cout<<<"*R2\n";
for( i=0;i
{
for(int j=0;j
{
cout<
cout<<"\t";
}
cout<<"\n";
}
cout<<"Please wait while system calculates the result..............\n";
float z=a[r-1][c-1]/a[r-1][c-2];
float y=(a[r-2][c-1]-a[r-2][c-2]*z )/a[r-2][c-3];
float x=(a[r-3][c-1]-a[r-3][c-2]*z-a[r-3][c-3]*y)/a[r-3][c-4];
delay(10000);
clrscr();
textcolor(GREEN);
cprintf("SOLUTION OF GIVEN SYSTEM OF EQUATIONS");
cout<<"\n";
cout<<"x = "<
cout<<"\ny = "<
cout<<"\nz = "<
float ch;
cout<<"\n";
cprintf("Do you want to view steps again");cout<<"\n";
cprintf("press '1' else Press '2'");cout<<" ";cin>>ch;
if(ch==1)
goto step;
else
exit(0);
getch();
}

output:

enter the values in given format
a[0][0] x + a[0][1] y + a[0][2] z = a[0][3]
enter a[0][0] : 2

enter a[0][1] : 1

enter a[0][2] : 1

enter a[0][3] : 10

enter a[1][0] : 3

enter a[1][1] : 2

enter a[1][2] : 3

enter a[1][3] : 18

enter a[2][0] : 1

enter a[2][1] : 4

enter a[2][2] : 9

enter a[2][3] : 16

2 1 1 10
3 2 3 18
1 4 9 16
the values after step 1 :
on dividing R1 by 1
1 0.5 0.5 5
3 2 3 18
1 4 9 16
the values after step 2 :
on R2 - 3*R1
1 0.5 0.5 5
0 0.5 1.5 3
1 4 9 16
the values after step 3 :
on R3 - 1*R1
1 0.5 0.5 5
0 0.5 1.5 3
0 3.5 8.5 11
the values after step 4 :
on R3 - 7*R2
1 0.5 0.5 5
0 0.5 1.5 3
0 0 -2 -10
Please wait while system calculates the result..............

SOLUTION OF GIVEN SYSTEM OF EQUATIONS
x = 7
y = -9
z = 5
Do you want to view steps again
press '1' else Press '2'

C++ PROGRAM FOR INTEGRATION USING NUMERICAL METHODS

//PROGRAM FOR INTEGRATION USING NUMERICAL METHODS

#include
#include
#define f(x) sqrt(cos(x))
#define inv 10
#include
void main()
{
clrscr();
int choice;
float l,j=0,u,h,arr[inv+1],sum1=0,sum2=0,sum3=0,res=0;
textcolor(GREEN);
cprintf("INTEGRATION USING THE TRAPEZOIDAL RULE & SIMPSON'S 1/3rd
RULE");
cout<<"\n";
textcolor(BLUE);
cprintf("1.TRAPEZOIDAL RULE");
cout<<"\n";
cprintf("2.SIMPSON'S 1/3rd RULE");
cout<<"\nenter your choice :";
cin>>choice;

cout<<"enter the lower and upper limit respectively\n";
cin>>l>>u;
j=l;
h=(u-l)/inv;
for(int i=0;i<=inv;i++)
{
arr[i]=f(j);//cout<<"\n"<
j=j+h;
}

sum1=arr[0]+arr[inv];

switch(choice)
{
case 1:
cout<<"\n================================================================================\n";
for(i=1;i
{sum2+=arr[i];}
res=h/2*(sum1+2*sum2);
cout<<"\nthe integration is :"<
break;
case 2:
cout<<"\n================================================================================\n";
for(i=1;i
{ sum2+=arr[i];}
for(i=2;i
{sum3+=arr[i];}
res=h/3*(sum1+4*sum2+2*sum3);
cout<<"\nthe integration is :"<
break;
default:cprintf("WRONG CHOICE ");
}
cout<<"\n";
cprintf("PROGRAM DESIGNED BY:ROHIT KUMAR");
cout<<"\n";
textcolor(RED);
cprintf("BRANCH : ECE / 3rd semester / roll 249/06 ");

getch();
}

output:

INTEGRATION USING THE TRAPEZOIDAL RULE & SIMPSON'S 1/3rd RULE
1.TRAPEZOIDAL RULE
2.SIMPSON'S 1/3rd RULE
enter your choice :1
enter the lower and upper limit respectively
0
1.57

================================================================================


the integration is :1.187191
PROGRAM DESIGNED BY:ROHIT KUMAR
BRANCH : ECE / 3rd semester / roll 249/06

INTEGRATION USING THE TRAPEZOIDAL RULE & SIMPSON'S 1/3rd RULE
1.TRAPEZOIDAL RULE
2.SIMPSON'S 1/3rd RULE
enter your choice :2
enter the lower and upper limit respectively
0
1.57

================================================================================


the integration is :1.19437

C++ Program for newton forward interpolation formula

//Program for newton forward interpolation formula

#include
#include
#include
#include
#define MAXN 100
#define ORDER 4
void main()
{
float ax[MAXN],ay[MAXN],diff[MAXN+1][ORDER+1];
float nr=1.0,dr=1.0,x,p,h,yp;
int i,n,j,k;
textcolor(GREEN);
clrscr();
cout<<"enter the value of n : ";
cin>>n;
cout<<"\nenter the values in form x,y\n";
for(i=0;i<=n;i++)
cin>>ax[i]>>ay[i];
cout<<"\nenter the value of x for which y is wanted : ";
cin>>x;
h=ax[1]-ax[0];
for(i=0;i<=n-1;i++)
diff[i][1]=ay[i+1]-ay[i];
for(j=2;j<=ORDER;j++)
for(i=0;i<=n;i++)
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
i=0;
while(!(ax[i]>x))
i++;
i--;
p=(x-ax[i])/h;
yp=ay[i];
for(k=1;k<=ORDER;k++)
{
nr*=p-k+1;
dr*=k;
yp+=(nr/dr)*diff[i][k];
}
cout<<"\n when x = "<<<", y = "<<<<
delay(200);
cout<<"\a\a";
textcolor(YELLOW);
cout<<"\n\n\a";

cout<<"\n\n";


getch();
}

output:
enter the value of n : 4
enter the values in form x,y
80 5026
85 5674
90 6362
95 7088
100 7854
enter the value of x for which y is wanted : 83
when x = 83, y = 5.41e+03

C++ Program for probability- 12

//program no. 12 (probability)

#include
#include
#include
#include
void main()
{
textcolor(BROWN);
clrscr();
int w1,w2,b1,b2;
float p1,p2,p3;
cout<<"enter the number of white & black balls for urn 1 "; cin>>w1>>b1;
cout<<"enter the number of white & black balls for urn 2 "; cin>>w2>>b2;
cout<<"\ncase 1 : both ball drawn from urn1 are black "; p1=(nCr(b1,2)/nCr(b1+w1,2))*(nCr(w2,1)/nCr(w2+b2+2,1)); cout<<"\n\ncase 2: when both balls drawn from urn 1 are white"; p2=(nCr(w1,2)/nCr(w1+b1,2))*(nCr(w2+2,1)/nCr(w2+b2+2,1)); cout<<"\n\ncase 3 : when one ball is white & other is black"; p3=((nCr(w1,1)*nCr(b1,1))/nCr(w1+b1,2))*(nCr(w2+1,1)/nCr(w2+b2+2,1)); cout<<"\nprobability in case 1 = "< 2 = "< cout<<" 3 = "< cout< textcolor(RED); cprintf(" p3 =" %f" 1 =" 0.011538" 2 =" 0.288462" 3 =" 0.153846" p3 =" 0.453846">
main ( )
{
int a = 5;
printf("value of a = %d\n",a);
printf("address of a = %u\n",&a);
}

C++ Program for probability-11

//program no. 11 (probability)
#include
#include
#include
#include
#define max 5
void main()
{
textcolor(LIGHTGREEN);
clrscr();
float urn[max],w[max],b[max],p[max],sum=0;
int num,nw;
cout<<"enter the number of urns : ";
cin>>num;
cout<<"enter the number of white balls to be drawn :";
cin>>nw;
for(int i=0;i
{
cout<<"for urn "<<<" enter its probability : ";
cin>>urn[i];
cout<<"\n enter the number of white & black balls resp. : ";
cin>>w[i]>>b[i];
p[i]=nCr(w[i],nw)/nCr(w[i]+b[i],nw);
sum+=p[i]*urn[i];
}
cout<<"\nthe required probability is "<
delay(100);
textcolor(RED);
cout<<"\n\n\a";
cprintf("PROGRAM DESIGNED BY:ASHISH KUMAR ");
cout<<"\n\n";
cprintf("/ roll 249/06 ");
getch();
}

output:
enter the number of urns : 3
enter the number of white balls to be drawn :2
for urn 1 enter its probability : 0.2

enter the number of white & black balls resp. : 7 3
for urn 2 enter its probability : 0.6

enter the number of white & black balls resp. : 4 6
for urn 3 enter its probability : 0.2

enter the number of white & black balls resp. : 2 8

the required probability is 0.177778

C++ Program for probability- 10

//program no. 10 (probability)

#include
#include
#include
#define max 5
void main()
{
float num,a[max],s[max],p[max],sum=0;
textcolor(LIGHTGREEN); int m;
clrscr();
cout<<"enter the number of plants = ";
cin>>num;
cout<<"enter the plant which scooter is likely to be found ";
cin>>m;
for(int i=1;i<=num;i++)
{
cout<<"enter the % of sccoters manufactured at plant "<<<" = ";
cin>>a[i];
cout<<"enter the % of scooters of standard quality manufactured at
plant "<<<" = ";
cin>>s[i];
sum+=a[i]*s[i];
}
p[m]=a[m]*s[m]/sum;
cout<<"\nthe chance that it comes from plant "<<<" = "<
delay(100);
textcolor(RED);
cout<<"\n\n\a";
cprintf("PROGRAM DESIGNED BY:ASHISH KUMAR");
cout<<"\n\n";
cprintf("/ roll 249/06 ");
getch();
}

output:
enter the number of plants = 2
enter the plant which scooter is likely to be found 2
enter the % of sccoters manufactured at plant 1 = 0.7
enter the % of scooters of standard quality manufactured at plant 1 =
0.8
enter the % of sccoters manufactured at plant 2 = 0.3
enter the % of scooters of standard quality manufactured at plant 2 =
0.9

the chance that it comes from plant 2 = 0.325301