Welcome To Uday Satya Blog

91-100 PROGRAMS



/**************************************************************
         91. Program to implement command line arguments
**************************************************************/
#include
#include
#include
#include
int main(int argc,char * argv[])
{
int number[9]={11,22,33,44,55,66,77,88,99};
if(argc !=3)
{
cout<<"argc="<
cout<<"error in arguments \n";
exit(1);
}
ofstream fout1,fout2;
fout1.open(argv[1]);
if(fout1.fail())
{
cout<<"couldnt open the file"
    <
exit(1);
}
fout2.open(argv[2]);
if(fout2.fail())
{
cout<<"couldnt open the file"
    <
exit(1);
}
for(int i=0;i<9;i++)
{
if(number[i]%2==0)
fout2<
else
fout1<
}
fout1.close();
fout2.close();
ifstream fin;
char ch;
for(i=1;i
{
fin.open(argv[i]);
cout<<"contents of"<
do
{
fin.get(ch);
cout<
}
while(fin);
cout<<"\n \n";
fin.close();
}
return 0;
}

Output:

contents of odd
11 33 55 77  99
Contents of even
22 44 66 88













/*******************************************************************
    92. To count no of lines,words and characters in a given string
*******************************************************************/
#include
#include
#include
#include
#include
void main()
{
int n=0,c=-1,w=0;
char a;
clrscr();
ofstream f1;
f1.open("file.text");
cout<<"enter the contents of file:\n";
while(cin)
{
cin.get(a);
f1.put(a);
}
f1.close();
ifstream if1;
if1.open("file.text");
while(!if1.eof())
{
if1.get(a);
if(a=='\n')
{
n++;
w++;
}
if(a==32)
{
w++;
}
if((a!='\n')&&(a!=32))
{
c++;
}
}
cout<<"no of lines:"<
cout<<"no of words:"<
cout<<"no of characters:"<
getch();
}





Output:

enter the contents of file

Loyola academy degree and pg college
computer block
computer science

no.of lines:3
no of words:10
no of characters:60
















/*****************************************************
            93.  Program for bisection method
*****************************************************/

#include
#include
#include
#include

float func(float);
void main()
{
float a,b,x1,y0,y1,y2;
cout<<"enter the value of a & b\n";
cin>>a>>b;
y0=func(a);
y1=func(b);
int i=0;
if((y0*y1)>0)
{
cout<<"values are not suitable\n";
exit(1);
}

do
{
x1=(a+b)/2;
y2= func(x1);
i++;
if((y0*y2)>0)
a=x1;
else
b=x1;
}
while(i<5);

cout<<"the roots are "<
getch();
}
float func(float n)
{
n=(n*n*n)-(3*n*n)+1;
return n;
}



OUTPUT:
Enter the value of a & b
-1
3
The roots are 2.875


























/*************************************************************
     94. Program for lagranges interpolation formula
**************************************************************/
#include
#include

void main()
{
float x[20],y[20],x1,prod1,prod2;
float prod, sum=0;
int i,j,n;
cout<<"enter the limit\n";
cin>>n;
cout<<"enter the  value of x1\n";
cin>>x1;
cout<<"enter the values of x table\n";
for(i=0;i
{
cin>>x[i];
}
cout<<"enter the values of y table\n";
for(i=0;i
{
cin>>y[i];
}
for(i=0;i
{
prod1=1;
prod2=1;
for(j=0;j
{
if(j!=i)
{
prod1=prod1*(x1-x[j]);
prod2=prod2*(x[i]-x[j]);
prod=prod1/prod2;
}                   }
sum=sum+(y[i]*prod);
}
cout<<"the intigral value is :"<
getch();
}


OUTPUT
Enter the limit
4
Enter the value of  x1
10
Enter the values of x table
5
6
9
11
Enter the values of y table
12
13
14
16
The integrals value  is :14.6667


















/***************************************************************
     95.  Program to implement newton-raphson method
***************************************************************/
#include
#include

float func(float);
float dfunc(float);
void main()
{
float x0,x1,n;
cout<<"enter initial approximation x0 \n";
cin>>x0;
cout<<"eter no. of iteration ";
cin>>n;

for(int i=0;i
{

x1=x0-(func(x0)/dfunc(x0));
x0=x1;
}

cout<<"the roots are\t"<
getch();
}

float func(float x)

{
x=(x*x)-(5*x)+2;
return x;
}
float dfunc(float x)
{
return x;
}




OUTPUT:
Enter initial approximation x0
1
Enter no. of iteration 2The roots are 4.3333

































/******************************************************************
96.//program for Newton’s backward interpolation formula
*******************************************************************/
#include
#include

void funct(int);
float product(int);

float b[10],g[10],f[10],x[10],c[10],sum=0,prod=1,x1,p,prodc=1;
int i,j,n;

void main()
{
clrscr();
cout<<"enter n value\n";
cin>>n;
cout<<"enter x and f(x)\n";
for(i=0;i
cin>>x[i]>>f[i];
cout<<"enter values of x to determine f(x)\n";
cin>>x1;

p=(x1-x[n-1]/x[1]-x[0]);
cout<<"p="<

for(j=0;j
b[j]=f[j];
int k=n;
for(i=0;i
{
funct(k);
c[i]=g[k-2];
}
for(j=0;j
{
b[j]=g[j];
k--;
}
int m=i;
for(i=0;i
{
prod=product(i)*c[i];
sum+=prod;
}
sum=sum+f[n-1];
cout<<"result is "<
getch();
}
void funct(int k)
{
for(j=0;j
{
g[j]=b[j+1]-b[j];
}}
float product(int i)
{
prodc=1;
for(j=0;j
prodc=((p+j)/(j+1))*prodc;
return (prodc);
}



OUTPUT:

Enter n value
2
Enter x and f(x)
30    0.5
35 0.567
Enter values of x to determine f(x)
32
P=1
Result is 0.634


/*****************************************************************
      97.  Program for Newton’s forward interpolation formula
******************************************************************/
#include
#include

void funct(int);
float product(int);

float b[10],g[10],f[10],x[10],prod=1,c[10],sum=0,x1,p,prodc=1;
int i,j,n;

void main()
{
cout<<"enter n value\n";
cin>>n;
cout<<"enter x and f(x)\n";
for(i=0;i
cin>>x[i]>>f[i];
cout<<"enter values of x to determine f(x)\n";
cin>>x1;

p=(x1-x[0]/x[1]-x[0]);
cout<<"p="<

for(j=0;j
b[j]=f[j];
int k=n;
for(i=0;i
{
funct(k);
for(j=0;j
{
b[i]=g[j];
c[i]=b[0];
k--;
}
}
int m=i;
for(i=0;i
{
prod=product(i)*c[i];
sum+=prod;
}
sum=sum+f[0];
cout<<"result is "<
getch();
}
void funct(int k)
{
for(j=0;j
{
g[j]=b[j+1]-b[j];
}}
float product(int i)
{
prodc=1;
for(i=0;j
prodc=((p-j)/(j+1))*prodc;
return (prodc);
}


OUTPUT:

Enter n value
4
Enter x and f(x)
12500   111.80
12510   111.84
12520   111.89
12530   111.97
Enter values of x to determine f(x)
12516
P=14.9984
Result is 177.153


/**********************************************************
             98.  Program for Simpson’s 1/3rd rule
***********************************************************/
#include
#include
#include

void main()
{

float f[20],sum1=0,sum2=0,sum,h,a,x,m,n;

int i;
cout<<"enter upper limit and lower limit\n";
cin>>m>>n;
cout<<"enter no. of intervals\n";
cin>>a;
h=(m-n)/a;

for(x=n,i=0;x<=m;i++)
{
f[i]=1/sqrt(x+2);

cout<
x=x+h;
}

for(i=1,x=n+h;x
{
if(i%2==0)
sum1=sum1+f[i];
else
sum2=sum2+f[i];
x=x+h;
}
sum=(h/3)*((f[0]+f[i]+(4*sum2)+(2*sum1));
cout<<"the intigral values are"<
getch();
}






OUTPUT:

Enter upper limit and lower limit
3
1

Enter no of intervals
2

1            0.57735
2            0.5
3            0.447214

The integral values are 1.00819



















/*****************************************************************
           99.  To implement successive approximation method
*****************************************************************/
#include
#include
#include
void main()
{
double a,b,y,h;
int n;
cout<<" enter the number of iterations \n";
cin>>n;
cout<<"enter A for -ve and B for +ve \n";
 cin>>a>>b;
 h= (a+b)/2;
 for(int i=0;i
 {
 y= (-sin(h)+exp(h))/3;
 cout<
 h=y;
}
cout<<"the approximation value of x is:"<
getch();
}

OUTPUT:

Enter the number of iteration
4
Enter A for –ve and B for +ve
-2
3
1           0.389765
2           0.365554
3           0.361282
4           0.360565



/*******************************************************
                100.  Program for trapezoidal rule
********************************************************/
#include
#include
#include

void main()
{
float f[20],sum=0,h,x;;
int i=0,m,n,a;

cout<<"enter upper limit and lower limit \n";
cin>>m>>n;

cout<<"enter no. of intervals\n";
cin>>a;
h=(m-n)/a;

for(x=n;x<=m;i++)
{
f[i]=1/(1+x);
cout<
x=x+h;
}
X=n+h;
for(i=1;x
{
sum+=f[i];
x=x+h;
}
sum=(h/2)*((f[0]+f[a]+(2*sum)));
cout<<"the intigral values are:"<
getch();
}






OUTPUT:

Enter upper limit and lower limit
3
0

Enter no.of intervals
3

0        11         0.52        0.333333       0.25

The integral values are : 1.45833

0 comments:

Post a Comment