practical list

54
Practical List Submitted on 1. Calculate the sum 1/1 + 1/2 + 1/3 + 1/4 + ----------+ 1/ N. 2. Read floating point numbers and computes two averages: the average of negative numbers and the average of the positive numbers. 3. Logical (i.e. Boolean) valued function which takes a single integer parameter and returns “True “ if and only if the integer is a prime number between 1 and 1000. 4. User defined function to find the absolute value of an integer. 5. Generate a random number between 0 and 99. 6. Function “print…pyramid(..)” which takes a single integer argument “height” and displays a “pyramid” of this height made up of “*” characters on the screen. 7. Using two dimensional arrays, write c ++ function (and a corresponding program to test it) which multiplies an m x n matrix of integers by an n x r matrix of integers. 8. To create employee data base using two dimensional arrays. 9. Enter 100 integers into an array and sort them in an ascending order. 10. Enter 10 integers into an array and then search for a particular integer in the array. 11. Read from a text file and write to a text file. 12. Demonstrate the use of enumeration. 13. (A). Root finding methods – Newton Method (B). Root finding methods – Secant Method 14. (A). LU decomposition (B). Cholesky methods 15. (A). Gauss-Jacobi (B). Gauss-Siedel method. 16. (A). Lagrange Interpolation method (B). Newton Interpolation method 17. (A). Gregory Newton Backward Interpolation method (B). Gregory Newton Backward and forward interpolation method

Upload: moniratna-bhuyan

Post on 27-Oct-2014

36 views

Category:

Documents


0 download

DESCRIPTION

Uploaded from Google Docs

TRANSCRIPT

Page 1: Practical List

Practical ListSubmitted on

1. Calculate the sum 1/1 + 1/2 + 1/3 + 1/4 + ----------+ 1/ N.2. Read floating point numbers and computes two averages: the average of negative

numbers and the average of the positive numbers.3. Logical (i.e. Boolean) valued function which takes a single integer parameter and

returns “True “ if and only if the integer is a prime number between 1 and 1000.4. User defined function to find the absolute value of an integer.5. Generate a random number between 0 and 99.6. Function “print…pyramid(..)” which takes a single integer argument “height” and

displays a “pyramid” of this height made up of “*” characters on the screen.7. Using two dimensional arrays, write c ++ function (and a corresponding program

to test it) which multiplies an m x n matrix of integers by an n x r matrix of integers.

8. To create employee data base using two dimensional arrays.9. Enter 100 integers into an array and sort them in an ascending order.10. Enter 10 integers into an array and then search for a particular integer in the array.11. Read from a text file and write to a text file.12. Demonstrate the use of enumeration.

13. (A). Root finding methods – Newton Method

(B). Root finding methods – Secant Method

14. (A). LU decomposition

(B). Cholesky methods

15. (A). Gauss-Jacobi

(B). Gauss-Siedel method.

16. (A). Lagrange Interpolation method

(B). Newton Interpolation method

17. (A). Gregory Newton Backward Interpolation method

(B). Gregory Newton Backward and forward interpolation method

18. Simpson’s rule

19. Euler’s method.

20. Find roots of a second degree polynomial

21. Enter 10 integers into an array and print the largest of them.

22. (A). Bisection Method

(B). False position Method

23. SOR iterative method

Page 2: Practical List

24. Trapezoidal rule

//Practical list 1

//moniratna bhuyan

//Program to calculate 1/1+1/2+...+1/N

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

float s=0,p;

int n,i;cout<<"\n\tEnter the value of n= ";cin>>n;for(i=1;i<=n;i++){p=1.0/i;s=s+p;}cout<<"\n\tThe sum= "<<s;getch();}/* Output:Enter the value of n= 5The sum= 2.283334*/

//Practical list 2//moniratna bhuyan

Page 3: Practical List

//Program to read floating point numbers and calculate the average of positive numbers and negative numbers//Array ‘A’ stores the value#include<iostream.h>#include<conio.h>int main(){double A[100], posavg, negavg, sumpos, sumneg;int n, npos, nneg, i;cout<<"Supply the number of numbers"<<endl;cin>>n;cout<<"Enter the "<<n<<" number"<<endl;for(i=0; i<n; i++)cin>>A[i];cout<<"Finding the average of positive number and negative number"<<endl;cout<<"The numbers are"<<endl;for(i=0; i<n; i++)cout<<A[i]<<" ";cout<<endl;sumpos=0;npos=0;sumneg=0;nneg=0;for(i=0; i<n; i++){if(A[i]>0){sumpos=sumpos+a[i];npos=npos+1;}elseif(A[i]<0){sumneg=sumneg+a[i];nneg=nneg+1;}}posavg=sumpos/npos;negavg=sumneg/nneg;cout<<"The avg of positive integer is "<<posavg<<endl;cout<<"The avg of negative integer is "<<negavg<<endl;getch();return 0;}/*Supply the number of numbers4Enter the 4 number1 2 -8 -9Finding the average of positive number and negative numberThe numbers are

Page 4: Practical List

1 2 -8 -9The avg of positive integer is 1.5The avg of negative integer is -8.5*/

//Practical list 3//moniratna bhuyan

//Program for Logical (i.e. Boolean) valued function which takes a single integer parameter //and returns “True “ if and only if the integer is a prime number between 1 and 1000

#include<iostream.h>#include<conio.h> void main(){clrscr();

int n, flag ,i;cout<<"\nEnter the number= ";cin>>n;if(n>=1 && n<=1000){

for(i=2;i<=n/2;i++){

if(n%i==0){

flag=1;break;

}}

}if (flag==0)

cout<<"\nTRUE";else

cout<<n<<" is not prime between 1 AND 1000"<<endl;getch();

}/*

Output:

Enter the number = 23

TRUE */

/* Enter the number= 25

Number is not prime between 1 AND 1000*/

Page 5: Practical List

//Practical list 4//moniratna bhuyan//Program to create a user defined function to find the absolute value of an integer #include<iostream.h> # include <conio.h> int abs(int); void main() { clrscr(); int num; cout<<"\n Enter the number= "; cin>>num; cout<<"\n The absolute value= "<<abs(num); getch(); } int abs(int n) { int no; if(n>0) no=n; else no= -n; return no; } /*

Output:

Enter the number= -3

The absolute value= 3

Enter the numbe= 3

The absolute value= 3

*/

//Practical list 5//moniratna bhuyan

//Program to generate a random number between 0 and 99 #include<iostream.h> #include<conio.h> #include<stdlib.h>

void main() { clrscr(); randomize(); int n;

Page 6: Practical List

n=random(100)+0; cout<<"\n The random number chosen from 0 to 99= "<<n; getch(); }

/* Output:

The random number chosen from 0 to 99= 11

*/

//Practical list 6//moniratna bhuyan

//Program to find the root of a second degree polynomial// Program to print a "pyramid" of a specified height made up of '*' on the screen #include <iostream.h>#include<conio.h>void print_pyramid(int height);int main(){ clrscr();

int pyramid_height;cout << "\nThe height of the pyramid= ";cin >> pyramid_height;print_pyramid(pyramid_height);

getch(); return 0;}void print_pyramid(int height){

int i,j,k;cout << "\n\n";

for (i = 1 ; i <= height ; i++){

for(j=0;j<=height-i;j++) cout<<" "; for(k=0;k<=2*i-2;k++) cout<<"*"; cout<<"\n"; }

cout << "\n\n"; }

/* Output:

The height of thr pyramid= 3

*

Page 7: Practical List

***

*****

*/

//Practical list 7//moniratna bhuyan//Program to multiply two matrices

#include<iostream.h>#include<conio.h>int main() { clrscr(); int i, j, k,p,d,l,m; double a[10][10], b[10][10], c[10][10]; cout<<"\nEnter the size of Matrix1: "<<endl; cin>>p>>d; cout<<"\nEnter the size of Matrix2: "<<endl; cin>>l>>m; if(d!=l) { cout<<"\nWrong Dimension."<<endl; } else { cout<<"\nEnter the first matrix: "<<endl; for(i=0; i<p; i++) for(j=0; j<d; j++) cin>>a[i][j]; cout<<endl; cout<<"\nEnter the second matrix: "<<endl; { for(i=0; i<l; i++) for(j=0; j<m; j++) cin>>b[i][j]; cout<<endl; } cout<<"\nThe first matrix: "<<endl; for(i=0; i<p; i++) { for(j=0; j<d; j++) cout<<a[i][j]<<" "; cout<<endl; } cout<<endl; cout<<"\nThe second matrix: "<<endl; for(i=0; i<l; i++) { for(j=0; j<m; j++)

Page 8: Practical List

cout<<b[i][j]<<" "; cout<<endl; } cout<<endl; cout<<"\nThe multiplication: "<<endl; for(i=0; i<p; i++) { for(j=0; j<m; j++) { c[i][j]=0; for(k=0; k<d; k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } for(i=0; i<p; i++) { for(j=0; j<m; j++) { cout<<c[i][j]<<" "; } cout<<endl; } } getch();

return 0; }

//OUTPUT:

/*

Enter the size of Matrix1:

2

4

Enter the size of Matrix2:

4

3

Enter the first matrix:

1

4

5

Page 9: Practical List

2

5

2

10

2

Enter the second matrix:

2

5

4

5

1

0

2

4

8

1

2

0

The first matrix:

1 4 5 2

5 2 10 2

The second matrix:

2 5 4

5 1 0

2 4 8

1 2 0

The multiplication:

34 33 44

Page 10: Practical List

42 71 100

*/

/*

Enter the size of Matrix1:

2

3

Enter the size of Matrix2:

4

2

Wrong Dimension.

*/

//Practical list 8//moniratna bhuyan

//Program to create employee data base using two dimensional arrays#include<iostream.h>#include<conio.h> void main() {

clrscr();int a[50][2],n,i;cout<<"Enter the no of employees= ";cin>>n;cout<<"\n Enter the details of the employee: \n";for(i=0;i<n;i++) {

cout<<"\n Enter the employee number= ";cin>>a[i][0];cout<<"\n Enter the employee salary= ";cin>>a[i][1];

}cout<<"\n The details of the employees are: \n";cout<<"\n Employee no Employee salary";for(i=0;i<n;i++){

cout<<"\n "<<a[i][0]<<"."<<"\t\t\t"<<a[i][1];}getch();

}

Page 11: Practical List

/*Output:

Enter the no of employees= 5

Enter the details of the employee:

Enter the employee no= 1

Enter the employee salary= 1000

Enter the employee no= 2

Enter the employee salary= 5000

Enter the employee no= 3

Enter the employee salary= 6253

Enter the employee no= 4

Enter the employee salary= 2350

Enter the employee no= 5

Enter the employee salary= 3214

The details of the employees are:

Employee no Employee salary

Page 12: Practical List

1. 1000

2. 5000

3. 6253

4. 2350

5. 3214

*/

//Practical list 9//moniratna bhuyan

//Program to enter hundred integers in an array and to sort them in an ascending order #include<iostream.h> #include<conio.h> void main() { clrscr(); int i,arr[100],j,temp,n; cout<<"\nEnter the size of the array= "; cin>>n; cout<<"\nEnter the array:\n"<<endl; for(i=0;i<n;i++) cin>>arr[i]; for(j=0;j<n;j++) { for(i=j+1;i<n;i++) {

if(arr[j]>arr[i]) {temp=arr[j]; arr[j]=arr[i]; arr[i]=temp; }}

} cout<<"\nThe array in ascending order: "<<endl; for(i=0;i<n;i++) cout<<"\n"<<arr[i]; getch(); }

/* OUTPUT:

Enter the size of the array= 100

Enter the array:

5 6 3 9 4 5 2 36 15 84 32 96 45 8 3 45 32 15 96 45 66 22 45 89 33 25 96 25 48 59

Page 13: Practical List

23 56 444 598 652 125 369 159 357 458 856 365

12 25 6 8 73 14 85 36 96 25 14 89 36 52 18 65 45 82 36 95 75 15 25 34 96 14 78 9

8 5 3 90 80 70 30 60 40 3 5 96 45 85 32 65 15 7

8 4 8 4 25 36 84 74 51 25 96 99 100

The array in ascending order:

2 3 3 3 3 4 4 4 5 5 5 5 6 6 7 8 8 8 8 9 12 14 14 14 15 15 15 15 18 22 23 25 25

25 25 25 25 25 30 32 32 32 33 34 36 36 36 36 36 40 45 45 45 45 45 45 48 51 52 56

59 60 65 65 66 70 73 74 75 78 80 82 84 84 85 85 89 89 90 95 96 96 96 96 96 96 9

6 98 99 100 125 159 357 365 369 444 458 598 652 856

*/

//Practical list 10//moniratna bhuyan

// Program to enter 10 integers into an array and then search for a particular integer in the //array #include<iostream.h> #include<conio.h> void main() { clrscr(); int i,num, arr[20]; cout<<"\nEnter ten integers:\n"<<endl; for(i=0;i<10;i++) cin>>arr[i]; cout<<"\nEnter the number to be searched= "; cin>>num; for(i=0;i<10;i++) { if(arr[i]==num) cout<<num<<"is at the position: "<<i+1<<endl; } getch(); }

/*Output;

Page 14: Practical List

Enter the ten integers:

3

4

5

8

0

7

1

2

6

9

Enter the number to be searched= 7

7 is at the position 6

*/

//Practical list 11//moniratna bhuyan

//Program to read from a text file and to write to a text file#include<fstream.h>#include<conio.h>void main(){

clrscr();int i;int marks1[10], marks2[10],marks3[10];double avg[100];int total[100];

ifstream indata;ofstream outdata;indata.open("nm1.dat");outdata.open("nm2.data");for(i=1;i<=5;i++) {

indata>>marks1[i]>>marks2[i]>>marks3[i];

Page 15: Practical List

total[i]=marks1[i]+marks2[i]+marks3[i];avg[i]=(total[i])/3;outdata<<marks1[i]<<endl<<marks2[i]<<endl<<marks3[i]<<endl<<endl;outdata<<"\nTotal of students "<<i<<"is "<<total[i];outdata<<"\nAverage os students "<<i<<"is: "<<avg[i];outdata<<endl<<endl;

}indata.close();outdata.close();

getch(); }/*

Output:

*/

//Practical list 12//moniratna bhuyan//Program to demonstrate the use of enumeration

#include<iostream.h>#include<conio.h>void main(){

clrscr();enum day{sunday,monday,tuesday,wednesday,thursday,friday,saturday};day theday;int a;cout<<"\nEnter the day of week(0 to 6) : ";cin>>a;theday=day(a);if((theday==sunday)||(theday==saturday))

cout<<"\nHurray it is a WEEKEND!!!";else

cout<<"\nIts a working day.";getch();

}

/*Output:

Enter the day of week (0 to 6) : 2

Its a working day.

Page 16: Practical List

*/

/*

Enter the day of week (0 to 6) : 6

Hurray it is a WEEKEND!!!

*/

//Practical list 13(A)//moniratna bhuyan

//Program for Newton Method#include<iostream.h>#include<conio.h>#include<math.h>#include<stdlib.h> void main() { clrscr(); int i,n; float xo,xn,fx,eR,dfx; cout<<"The function is f(x)=cos(x)-x*exp(x)"<<endl; cout<<"Enter the number of iterations= "; cin>>n; cout<<"Enter the error of tolarence= "; cin>>eR; cout<<"Enter the value of xOLD, xo= "; cin>>xo; for(i=1;i<=n;i++) { fx=cos(xo)-xo*exp(xo); dfx=-sin(xo)-exp(xo)-xo*exp(xo); xn=xo-fx/dfx; fx=cos(xn)-xn*exp(xn); if(fabs(fx)<=eR)

{ cout<<"The number of iterations is insufficient."<<endl;

cout<<"Iterations="<<i<<endl; cout<<"Root= "<<xn<<endl; cout<<"The function= "<<fx<<endl; getch(); exit(0); }

xo=xn; }

Page 17: Practical List

cout<<"Insufficient"<<endl; cout<<"Iterations="<<i<<endl; cout<<"Root= "<<xn<<endl; cout<<"The function= "<<fx<<endl; getch(); }

/*Output:The function is f(x)=cos(x)-x*exp(x)

Enter the number of iterations= 15

Enter the error of tolarence= 1.0000e-04

Enter the value of xOLD, xo= 1

The number of iterations is insufficient.

Iterations= 4

Root= 0.5177574

The function= 2.286344e-08

*/

//Practical list 13(B)//moniratna bhuyan

//Program for secant method#include<iostream.h>#include<conio.h>#include<math.h>#include<stdlib.h> void main() { clrscr(); int i,n; float a=0,b=1,r,fa,fb,fr,eR; cout<<"(a,b)=\t"<<"("<<a<<","<<b<<")"<<endl; cout<<"The function is f(x)=cos(x)-x*exp(x)"<<endl; cout<<"Enter the number of iterations= "; cin>>n; cout<<"Enter the error= "; cin>>eR; for(i=1;i<=n;i++) {

fa=cos(a)-a*exp(a);fb=cos(b)-b*exp(b);r=a-(a-b)*fa/(fa-fb);fr=cos(r)-r*exp(r);if(fr<=eR)

Page 18: Practical List

{ cout<<"The number of iterations is insufficient."<<endl; cout<<"Iterations="<<i<<endl; cout<<"Root= "<<r<<endl; cout<<"The function= "<<fr<<endl; getch(); exit(0); }

a=b; b=r; } }

/*Output:

(a,b)= (0,1)

The function is f(x)=cos(x)-x*exp(x)

Enter the number of iterations= 20

Enter the error of tolarence= 1.0000e-04

The number of iterations is insufficient.

Iterations= 3

Root= 0.531706

The function= -0.042931

*/

//Practical list 14(A)//moniratna bhuyan

//Program for LU decomposition#include<iostream.h>#include<conio.h> void main() { clrscr(); int n,i,j,k; float A[10][10],L[10][10],U[10][10],sum=0,sum1=0,sum2=0; cout<<"Enter the size of the matrix= "; cin>>n; cout<<"Enter the matrix:\n"<<endl; for(i=0;i<n;i++) for(j=0;j<n;j++) cin>>A[i][j]; cout<<"The matrix A is:\n"<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++)

Page 19: Practical List

{ cout<<A[i][j]<<"\t"; } cout<<"\n"; } cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++)

{L[j][i]=0;U[i][j]=0;}}

for(i=0;i<n;i++) { L[i][i]=1;} U[0][0]=A[0][0];

for(j=1;j<n;j++) { U[0][j]=A[0][j]; L[j][0]=A[j][0]/U[0][0]; } for(i=1;i<n;i++) { sum=0; for(k=0;k<=i-1;k++) sum=sum+L[i][k]*U[k][i]; U[i][i]=A[i][i]-sum; for(j=i+1;j<n;j++) { sum1=0; sum2=0; for(k=0;k<=i-1;k++)

{ sum1=sum1+L[i][k]*U[k][j];

sum2=sum2+L[j][k]*U[k][i]; U[i][j]=A[i][j]-sum1; L[j][i]=(A[j][i]-sum2)/U[i][i]; }

} } cout<<"The Lower Triangular matrix:\n"<<endl; for(i=0;i<n;i++)

{ for(j=0;j<n;j++)

cout<<L[i][j]<<"\t"; cout<<endl; }

cout<<endl; cout<<"The Upper Triangular matrix:\n"<<endl; for(i=0;i<n;i++) {

Page 20: Practical List

for(j=0;j<n;j++) cout<<U[i][j]<<"\t"; cout<<endl; } getch();

}

/*Output:

Enter the size of the matrix= 4

Enter the matrix:

2

1

1

0

4

3

3

1

8

7

9

5

6

7

9

8

The matrix A is:

2 1 1 0

Page 21: Practical List

4 3 3 1

8 7 9 5

6 7 9 8

The Lower Triangular matrix is:

1 0 0 0

2 1 0 0

4 3 1 0

3 4 1 1

The Upper Triangular matrix is:

2 1 1 0

0 1 1 1

0 0 2 2

0 0 0 2

*/

//Practical list 14(B)//moniratna bhuyan//Program for Cholesky Factorization

#include<iostream.h>#include<conio.h>#include<math.h> void main() { clrscr(); int n,i,j,k; double A[10][10],L[10][10],sum=0; cout<<"Enter the size of the matrix= "; cin>>n; cout<<"Enter the matrix\n"; for(i=0;i<n;i++) { for(j=0;j<n;j++)

Page 22: Practical List

{ cin>>A[i][j];} } cout<<"The matrix A is:"<<endl; for(i=0;i<n;i++) { cout<<endl; for(j=0;j<n;j++) { cout<<"\t"<<A[i][j]; } cout<<endl;} cout<<"A"<<"["<<n<<"]"<<"["<<n<<"] is SPD"<<endl; L[0][0]=(sqrt(A[0][0])); for(i=1;i<n;i++)

{ L[i][0]=A[i][0]/L[0][0];

} for(j=0;j<n;j++)

{ for(k=1;k<n;k++)

{ sum=0;

if(j==k) {

for(i=0;i<=k-1;i++) {

sum=sum+L[k][i]*L[k][i]; } L[k][k]=sqrt(A[k][k]-sum); } else if((k+1)<=j) {

for(i=0;i<=k-1;i++) {

sum=sum+L[j][i]*L[k][i]; } L[j][k]=(A[j][k]-sum)/L[k][k]; } else L[j][k]=0; }

} cout<<"\nCholesky Decomposition "<<endl; cout<<"Matrix L "<<endl; for(i=0;i<n;i++) {

cout<<endl; for(j=0;j<n;j++) {

cout<<"\t"<<L[i][j]; } cout<<endl;

}

Page 23: Practical List

cout<<"Matrix L Transpose "<<endl; for(i=0;i<n;i++) {

cout<<endl; for(j=0;j<n;j++) {

cout<<"\t"<<L[j][i]; }

} getch(); }/*

Output: Enter the size of the matrix= 3

Enter the matrix

4

2

-1

2

4

1

-1

1

4

The matrix a is:

4 2 -1

2 4 1

-1 1 4

A[3][3] is SPD.

Cholesky decomposition

Matrix L

2 0 0

1 1.732051 0

Page 24: Practical List

-0.5 0.866025 1.732051

Matrix L Transpose

2 1 -.05

0 1.732051 0

0 0 1.732051

*/

//Practical list 15(A)//moniratna bhuyan

//Program for Gauss-Jacobi Method#include<iostream.h>#include<conio.h>#include<iomanip.h>#include<stdlib.h>#include<math.h>void main(){

clrscr();float a[20][20],b[20],ox[20],nx[20],sum,ep;float re;int i,j,k,n,nmax;cout<<"\n Enter the number of equations = ";cin>>n;cout<<"\n Enter the co-efficient matrix A: "<<endl;for(i=0;i<n;i++){

for(j=0;j<n;j++){

cin>>a[i][j];}

}cout<<"\n Enter the co-efficient matrix B:"<<endl;for(i=0;i<n;i++){

cin>>b[i];}cout<<"\n Enter the maximum number of iterations permited= ";cin>>nmax;cout<<"\n Enter the acceptable precision= ";cin>>ep;for(i=0;i<n;i++){

ox[i]=0.0;}for(k=0;k<nmax;k++){

Page 25: Practical List

for(i=0;i<n;i++){

sum=0.0;for(j=0;j<n;j++){

if(i!=j){

sum+=(a[i][j]*ox[j]);}

}nx[i]=(b[i]-sum)/a[i][i];re=fabs((nx[i]-ox[i])/nx[i]);

}if (re<ep){

cout<<"\nThe solution converges in "<<k<<" iteration";for(i=0;i<n;i++)cout<<"\n"<<nx[i];getch();exit(0);

}for(i=0;i<n;i++){

ox[i]=nx[i];}

}cout<<"\n The solution does not converge.";getch();

}

/*Output:

Enter the number of equations= 3

Enter the co-efficient matrix A:

5

1

2

-3

9

4

1

Page 26: Practical List

2

-7

Enter the co-efficient matrix B:

10

-14

-33

15

Enter the acceptable precision= 0.0001

The solution converges in 12 iteration.

1.000408

-2.999738

3.999759

*/

//Practical list 15(B)//moniratna bhuyan

//Program for Gauss-Siedel method. #include<iostream.h>#include<conio.h>#include<iomanip.h>#include<math.h>#include<stdlib.h>void main(){

clrscr();float a[20][20],b[20],x[20],sum,ep,temp,re;int i,j,k,n,nmax;cout<<"\n Enter the number of equations = ";cin>>n;cout<<"\n Enter the co-efficient matrix A: "<<endl;for(i=0;i<n;i++){

for(j=0;j<n;j++){

cin>>a[i][j];}

}cout<<"\n Enter the co-efficient matrix B: "<<endl;for(i=0;i<n;i++)

Page 27: Practical List

{cin>>b[i];

}cout<<"\n Enter the maximum number of iterations permited= ";cin>>nmax;cout<<"\n Enter The acceptable precision= ";cin>>ep;for(i=0;i<n;i++){

x[i]=0.0;}for(k=0;k<nmax;k++){

for(i=0;i<n;i++){

sum=0.0;for(j=0;j<n;j++){

if(i!=j){

sum+=a[i][j]*x[j];}

}temp=(b[i]-sum)/a[i][i];re=fabs((temp-x[i])/temp);x[i]=temp;

}if(re<ep){

cout<<"\n The solution converges in "<<k<<" iterations "<<endl;for(i=0;i<n;i++)cout<<"\nSolution Number "<<i+1<<"\t"<<x[i];getch();exit(0);

}}cout<<"\n The colution doesnot converge. ";getch();

}

/*Output:

Enter the number of equations = 3

Enter the co-efficient matrix A:

5

1

Page 28: Practical List

2

-3

9

4

1

2

-7

Enter the co-efficient matrix B:

10

-14

-33

Enter the number of iterations permited= 10

Enter the acceptable precision= 0.0001

The solution converges in 9 iterations

Solution Number 1 0.99991

Solution Number 2 -3.000077

Solution Number 3 3.999965

*/

//Practical list 16(A)//moniratna bhuyan

//Program for Lagrange' interpolating polynomial#include<iostream.h>#include<conio.h> int main() { clrscr(); int n,i,j; float y; float x[10],fx[10],l[10]; cout<<"\nEnter the number of values to be entered= "; cin>>n; cout<<"\nEnter the "<<n<<" values: "<<endl; for(i=0;i<n;i++) {

Page 29: Practical List

cin>>x[i]; } cout<<"\nEnter the corresponding values of f(x): "<<endl; for(i=0;i<n;i++) {

cin>>fx[i]; } cout<<"\nEnter value at x is to be calculated= "; cin>>y; for(i=0;i<n;i++) {

l[i]=1; } for(i=0;i<n;i++) {

for(j=0;j<n;j++) { if(i!=j) {

l[i]*=(y-x[j])/(x[i]-x[j]); } }

} float p=0.0; for(i=0;i<n;i++) {

p+=(l[i]*fx[i]); } cout<<"\n\nThe polynomial value at "<<y<<" is "<<p<<endl; getch(); return 0; }

/*Output;

Enter the number of values to be entered= 3

Enter the 3 values:

0

1

3

Enter the corresponding values of f(x):

1

3

55

Page 30: Practical List

Enter value at x is to be calculated= 1

The polynomial value at 1 is 3

*/

//Practical list 16(B)//moniratna bhuyan

//Progrm for Newton Interpolation method#include<iostream.h>#include<conio.h>#include<iomanip.h>void main(){

clrscr();double x[10],f[10],f1[10],f2[10],f3[10],a,i,p;for(i=1;i<4;i++){

cout<<"\n Enter the value of x= ";cin>>x[i];cout<<"\n Enter the value of y= ";cin>>f[i];

}cout<<"\n Enter the interpolating value = ";cin>>a;for(i=0;i<3;i++){

f1[i]=(f[i+1]-f[i])/(x[i+1]-x[i]);}for(i=0;i<2;i++){

f2[i]=(f1[i+1]-f1[i])/(x[i+2]-x[i]);}for(i=0;i<1;i++){

f3[i]=(f2[i+1]-f2[i])/(x[i+3]-x[i]);}p=f[0]+(f1[0]*(a-x[0]))+(f2[0]*(a-x[0])*(a-x[1]))+(f3[0]*(a-x[0])*(a-x[1])*(a-x[2]));cout<<"\n\nThe Netwon Form = "<<p;getch();

}

/*Output:

Enter the value of x= 1

Enter the value of y= 0

Page 31: Practical List

Enter the value of x= 2

Enter the value of y= 0.69314718

Enter the value of x= 3

Enter the value of y= 1.098612289

Enter the interpolating value= 1.5

The Newton form= 0.321232

*/

//Practical list 17(A)//moniratna bhuyan

//Program for Gregory Newton Backward Interpolation method#include<iostream.h>#include<conio.h>#include<math.h>void main(){ clrscr();

double a,i,u,h,p,x[10],f[10],f1[10],f2[10],f3[10];cout<<"\nEnter the difference between two points= ";cin>>h;for(i=0; i<4; i++){ cout<<"\nEnter the value of x= ";

cin>>x[i];cout<<"\nEnter the value of y= ";cin>>f[i];

}cout<<"\nEnter the interpolating value= ";cin>>a;for(i=0; i<3; i++)

f1[i]=(f[i+1]-f[i]);for(i=0; i<2; i++)

f2[i]=(f1[i+1]-f1[i]);for(i=0; i<1; i++)

f3[i]=(f2[i+1]-f2[i]);cout<<"\n\nThe exact value of the function is= "<<exp(a);

Page 32: Practical List

u=(a-x[0])/h;p=f[0]+((f1[0]*u)+((f2[0]*(u*(u-1)))/2)+(f3[0]*(u)*(u-1)*(u-2))/6);cout<<"\n\nThe value of interpolated polynomial is= "<<p;cout<<"\n\nThe error is= "<<fabs(exp(a)-p);getch();

}

/*Output:

Enter the difference between two points= 0.5

Enter the value of x= 1

Enter the value of y= 2.7183

Enter the value of x= 1.5

Enter the value of y= 4.4817

Enter the value of x= 2.0

Enter the value of y= 7.3891

Enter the value of x= 2.5

Enter the value of y= 12.1825

Enter the interpolating value= 2.25

The exact value of the function is= 9.487736

Page 33: Practical List

The value of interpolated polynomial is= 9.503675

The error is= 0.015939

*/

//Practical list 17(B)//moniratna bhuyan

//Program to find the root of a second degree polynomial//Program for Gregory Newton Forward interpolation method#include<iostream.h>#include<conio.h>#include<math.h>void main(){

clrscr();double a,i,u,h,p,x[10],f[10],f1[10],f2[10],f3[10];cout<<"\nEnter the difference between two points= ";cin>>h;for(i=0; i<4; i++){

cout<<"\nEnter the value of x= ";cin>>x[i];cout<<"\nEnter the value of y= ";cin>>f[i];

}cout<<"\nEnter the interpolating value= ";cin>>a;for(i=0; i<3; i++)

f1[i]=(f[i+1]-f[i]);for(i=0; i<2; i++)

f2[i]=(f1[i+1]-f1[i]);for(i=0; i<1; i++)

f3[i]=(f2[i+1]-f2[i]);cout<<"\n\nThe exact value of the function is= "<<exp(a);u=(a-x[3])/h;p=f[3]+((f1[2]*u)+((f2[1]*(u*(u+1)))/2)+(f3[0]*(u)*(u+1)*(u+2))/6);cout<<"\n\nThe value of interpolated polynomial is= "<<p;cout<<"\n\nThe error is= "<<fabs(exp(a)-p);getch();

}

/*Output:

Enter the difference between two points= 0.5

Page 34: Practical List

Enter the value of x= 1

Enter the value of y= 2.7183

Enter the value of x= 1.5

Enter the value of y= 4.4817

Enter the value of x= 2.0

Enter the value of y= 7.3891

Enter the value of x= 2.5

Enter the value of y= 12.1825

Enter the interpolating value= 2.25

The exact value of the function is= 9.487736

The value of interpolated polynomial is= 9.503675

The error is= 0.015939

*/

//Practical list 18//moniratna bhuyan//Programe to find the integral by Simpson's Rule//To approximate the value of 1/(1+x) #include<iostream.h> #include<conio.h> double func(double(x)); int main()

Page 35: Practical List

{ clrscr(); double a,b, val; cout<<"\nEnter the lower limit and the upper limit of the integral:"<<endl; cin>>a>>b; val=(b-a)/6*(func(a)+4*(func((a+b)/2))+func(b)); cout<<"\nThe approximation value of the integral is= "<<val; getch(); return 0; } double func(double(x)) { return 1/(1+x); } /* Output; Enter the lower limit and the upper limit of the integral:

0

1

The approximation value of the integral is= 0.69444

*/

//Practical list 19//moniratna bhuyan

//Program for Euler's method#include<iostream.h>#include<conio.h>#include<math.h>#include<stdlib.h>double f(double x,double t1){

return 1+(x/t1);}void main(){

clrscr();double x,w,h,m,t,t2,p;cout<<"\n Enter the initial x = ";cin>>x;cout<<"\n Enter the initial w = ";cin>>w;cout<<"\n Enter the lower range of t = ";cin>>t;cout<<"\n Enter the upper range of t = ";cin>>t2;cout<<"\n Enter the step size h = ";

Page 36: Practical List

cin>>h;while(1){

if(t>t2){

getch();exit(0);

}x+=h*f(w,t);p=t*(1+log(t));cout<<"\n When t = "<<t;cout<<"\t w = "<<w<<"\nExact solution = "<<p<<"\tError = "<<p-w<<"\n";getch();w=x;t+=h;

}}

/*Output:

Enter the initial x = 1

Enter the initial w = 1

Enter the lower range of t = 1

Enter the upper range of t = 6

Enter the step size h = 0.5

When t = 1 w = 1

Exact solution = 1 Error = 0

When t = 1.5 w = 2

Exact solution = 2.108198 Error = 0.108198

When t = 2 w = 3.166667

Exact solution = 3.386294 Error = 0.219628

When t = 2.5 w = 4.458333

Exact solution = 4.790727 Error = 0.332393

Page 37: Practical List

When t = 3 w = 5.85

Exact solution = 6.295837 Error = 0.445837

When t = 3.5 w = 7.325

Exact solution = 7.88467 Error = 0.55967

When t = 4 w = 8.871429

Exact solution = 9.545177 Error = 0.673749

When t = 4.5 w = 10.480357

Exact solution = 11.268348 Error = 0.787991

When t = 5 w = 12.144841

Exact solution = 13.04719 Error = 0.902348

When t = 5.5 w = 13.859325

Exact solution = 14.876115 Error = 1.016789

When t = 6 w = 15.619264

Exact solution = 16.750557 Error = 1.131293

*/

//Practical list 20//moniratna bhuyan

//Program to find the root of a second degree polynomial #include<iostream.h> #include<conio.h> #include<math.h> #include<stdlib.h> void main() { clrscr();

Page 38: Practical List

float a,b,c,D,i,root1,root2; cout<<"\nEnter the values of a,b,c: "<<endl; cin>>a>>b>>c; D=pow(b,2)-4*a*c; if(a==0) { cout<<"The equation is not quadratic."<<endl; getch(); exit(0); } if(D==0) { root1= (-b)/(2*a); root2= (-b)/(2*a); cout<<"\nThe equation has real and equal roots."<<endl; cout<<"\nYhe roots are "<<root1<<"," <<root2<<endl; } else if(D>0) { root1= (-b+sqrt(D))/(2*a); root2= (-b-sqrt(D))/(2*a); cout<<"\nThe equation has real and unequal roots."<<endl; cout<<"\nThe roots are "<<root1<<"," <<root2<<endl; } else { root1=(-b)/(2*a); root2=(-b)/(2*a); i= (sqrt(abs(D)))/(2*a); cout<<"\nThe equation has imaginary roots."<<endl; cout<<"\nThe roots are "<<root1<<"+i"<<i<<" , " <<root2<<"-i"<<i<<endl; } getch(); } /*Output: Enter the values of a,b,c:

3

7

9

The equation has imaginary roots.

The roots are -1.16667+i1.28019 , -1.16667-i1.28019

*/

/*

Enter the values of a,b,c:

Page 39: Practical List

1

2

1

The equation has real and equal roots.

The roots are -1,-1

*/

/*

Enter the values of a,b,c:

1

-5

6

The equation has real and unequal roots.

The roots are 3,2

*/

//Practical list 21//moniratna bhuyan

//Program to enter ten integers in an array and to print the largest of them #include<iostream.h> #include<conio.h> int main() { clrscr(); int i, arr[11], max; cout<<"\nEnter the elements of the array: "<<endl; for(i=0;i<10;i++) cin>>arr[i]; max=arr[0]; for(i=0;i<10;i++) { if(max<arr[i])

max=arr[i]; } cout<<"\nThe largest element= "<<max<<endl; getch(); return 0; } /*Output: Enter the elements of the array:

Page 40: Practical List

4

1

2

9

0

3

5

8

7

6

The largest element= 9

*/

//Practical list 22(A)//moniratna bhuyan

//Program for Bisection Method#include<iostream.h>#include<conio.h>#include<math.h>#include<stdlib.h> void main() { clrscr(); int i,n; float a=0,b=1,r,fa,fb,fr,eR; cout<<"(a,b)=\t"<<"("<<a<<","<<b<<")"<<endl; cout<<"Enter the number of iterations= "; cin>>n; cout<<"Enter the error of tolarence= "; cin>>eR; cout<<"The function is f(x)=cos(x)-x*exp(x)"<<endl; r=(a+b)/2; for(i=1;i<=n;i++) { fa=cos(a)-a*exp(a); fr=cos(r)-r*exp(r); if((b-a)/pow(2,n)>=eR)

{ cout<<r<<endl; getch(); exit(0);

Page 41: Practical List

} if(fa*fr<0)

r=(a+r)/2;else {a=r; r=(r+b)/2; }

} cout<<"The number of iterations exceeded."<<endl; cout<<"Iterations="<<i<<endl; cout<<"Root= "<<r<<endl; cout<<"The function= "<<fr<<endl; getch(); }

/*Output:(a,b)= (0,1)

Enter the number of iterations= 40

Enter the error of tolarence= 1.0000e-04

The function is f(x)=cos(x)-x*exp(x)

The number of iterations exceeded.

Iterations=41

Root= 0.51776

The function= -3.043994e 05

*/

//Practical list 22(B)//moniratna bhuyan

//Program for False Position Method#include<iostream.h>#include<conio.h>#include<math.h>#include<stdlib.h> void main() { clrscr(); int i,n; float a=0,b=1,r,fa,fb,fr,eR; cout<<"(a,b)=\t"<<"("<<a<<","<<b<<")"<<endl; cout<<"The function is f(x)=cos(x)-x*exp(x)"<<endl; cout<<"Enter the number of iterations= "; cin>>n; cout<<"Enter the error= "; cin>>eR;

Page 42: Practical List

for(i=1;i<=n;i++) { fa=cos(a)-a*exp(a); fb=cos(b)-b*exp(b); r=a-(a-b)*fa/(fa-fb); fr=cos(r)-r*exp(r); if(fr<=eR) {

cout<<"The number of iterations is insufficient."<<endl; cout<<"Iterations="<<i<<endl; cout<<"Root= "<<r<<endl; cout<<"The function= "<<fr<<endl; getch(); exit(0); }

if(fa*fr<0) b=r; else a=r; } }/*Output:

(a,b)= (0,1)

The function is f(x)=cos(x)-x*exp(x)

Enter the number of iterations= 20

Enter the error of tolarence= 1.0000e-04

The number of iterations is insufficient.

Iterations= 9

Root= 0.517728

The function= 8.832585e- 05

*/

//Practical list 23//moniratna bhuyan

//Program for SOR iterative method#include<iostream.h>#include<conio.h>#include<stdlib.h>#include<math.h> void main() {

clrscr();double a[10][10],b[10],x[10],sum,ep,temp,w,re;

Page 43: Practical List

int i,j,k,n,nmax;cout<<"\n Enter the number of equations = ";cin>>n;cout<<"\n Enter the value of w = ";cin>>w;cout<<"\n Enter the coefficient matrix A";for(i=0;i<n;i++){

for(j=0;j<n;j++){

cout<<"\n Enter the a["<<i<<"]["<<j<<"] = ";cin>>a[i][j];

}}cout<<"\n Enter the coeficients of matrix B";for(i=0;i<n;i++){

cout<<"\n Enter the b["<<i<<"] = ";cin>>b[i];

}cout<<"\n Enter the maximum number of iterations permitted :\t";cin>>nmax;cout<<"\n Enter the acceptable precision :\t";cin>>ep;for(i=0;i<n;i++){

x[i]=0.0;}for(k=0;k<nmax;k++){

for(i=0;i<n;i++){

sum=0.0;for(j=0;j<n;j++){

if(i!=j){

sum+=(a[i][j]*x[j]);}

}temp=((1-w)*x[i])+(w*(b[i]-sum)/a[i][i]);re=fabs((temp-x[i])/temp);x[i]=temp;

}

if(re<ep){

cout<<"\n Solution converges in\t"<<k<<" iteration";for(i=0;i<n;i++)cout<<"\n Solution number "<<i+1<<"\t"<<x[i];

Page 44: Practical List

getch();exit(0);

}}cout<<"\n Solution does not converges";getch();

}

/*Output:

Enter the number of equations = 3

Enter the value of w = 0.9

Enter the coefficient matrix A

Enter the a[0][0] = 5

Enter the a[0][1] = 1

Enter the a[0][2] = 2

Enter the a[1][0] = -3

Enter the a[1][1] = 9

Enter the a[1][2] = 4

Enter the a[2][0] = 1

Enter the a[2][1] = 2

Page 45: Practical List

Enter the a[2][2] = -7

Enter the coeficients of matrix B

Enter the b[0] = 10

Enter the b[1] = -14

Enter the b[2] = -33

Enter the maximum number of iterations permitted : 6

Enter the acceptable precision : 0.00013

Solution converges in 4 iteration

Solution number 1 0.999546

Solution number 2 -2.999851

Solution number 3 3.999965

*/

//Practical list 24//moniratna bhuyan

//Program for Trapezoidal Rule #include<iostream.h> #include<conio.h> #include<math.h>

double func(int x) { return pow(x,4); } void main() {

clrscr();int a, b, n, i, m, p;double trap, fa, fb, h, sum=0;

Page 46: Practical List

cout<<"\nEnter the number of subintervals= ";cin>>n;cout<<"\nEnter the lower limit and the upper limit: "<<endl;cin>>a>>b;h=(b-a)/n;m=n-1;for(i=1;i<=m;i++) { p=a+i*h; sum=sum+func(p); }fa=func(a);fb=func(b);trap=h*(fa+2*sum+fb)/2;cout<<"\nIntegration of function= "<<trap;getch();

}

/*Output:

Enter the number of subintervals= 6

Enter the lower limit and the upper limt:

-3

3

Integration of the function= 115

*/