SlideShare a Scribd company logo
1 of 23
Download to read offline
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
QUESTION 1 
Write a C++ program to evaluate∫ 
using Trapezoidal rule up 
to three significant figures. 
#include <cmath> 
#include <iostream> 
#include <iomanip> 
using namespace std; 
float f(float x) 
{ 
float func; 
func = exp(x); 
return func; 
} 
void main() 
{ 
float a,b,h,sum=0, Trap; 
int i, n; 
cout <<"Enter the lower limit a : "; 
cin >> a; 
cout << "Enter the upper limit b: "; 
cin >> b; 
cout << "Enter the number of subintervals n: "; 
cin >> n; 
h=(b-a)/n;
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
for(i=1;i<n;i++) 
/*or we can can write for(i=a+h;i<=b-h;i=i+h) { sum+=f(i);*/ 
{ 
sum= sum + f(a+i*h); 
} 
Trap= (h/2)*(f(a) + f(b) +2 *sum); 
cout <<"nThe value of the integral is: " << setprecision(3) << Trap <<endl ; 
} 
//Output: 
Enter the lower limit a : 0 
Enter the upper limit b: 1.2 
Enter the number of subintervals n: 8 
The value of the integral is: 2.32 
//Alternative way for question 1 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
double trapezoidal(double a,double b,int n) 
{
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
double h= (b-a)/n; 
double z[9],I; 
double x[9]= {0.0, 0.15, 0.3,0.45,0.6,0.75,0.9,1.05,1.2 }; 
for (int i=0; i<9 ; i++) 
z[i]= exp(x[i]); //the function is exp(x) 
I=h/2*( z[0]+z[8] + 2*(z[1]+z[2]+z[3]+z[4]+z[5]+z[6]+z[7]) ); 
return (I); 
} 
int main() 
{ 
double J; 
J=trapezoidal(0,1.2,8); 
cout<< "The integral value is " << setprecision(3) << J << "nn"; 
return 0; 
} 
//Output: 
The integral value is 2.32 
QUESTION 2 
Write a C++ function sub-program to evaluate ∫ 
using 
Trapezoidal rule correct to four decimal places. Compare the 
result with the exact solution 1.48766
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
#include <cmath> 
#include <iostream> 
#include <iomanip> 
using namespace std; 
float f(float x) 
{ 
float func; 
func = 1/(1+(x*x)); 
return func; 
} 
void main() 
{ 
double a,b,h,sum=0, Trap, exact=1.48766; 
// we can find the exact value by using the formula exact= atan(b) - atan(a); 
int i, n; 
cout <<"Enter the lower limit a : "; 
cin >> a; 
cout << "Enter the upper limit b: "; 
cin >> b; 
cout << "Enter the number of subintervals n: "; 
cin >> n; 
h=(b-a)/n; 
for(i=1;i<n;i++)
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
/*or we can can write for(i=a+h;i<=b-h;i=i+h) { sum+=f(i);*/ 
{ 
sum= sum + f(a+i*h); 
} 
Trap= (h/2)*(f(a) + f(b) +2 *sum); 
cout <<"nThe value of the integral is: " << setprecision(4) <<fixed << Trap <<endl ; 
cout<<"The difference in Trapezoidal method = "<<setprecision(4) << fixed <<fabs(exact- 
Trap)<<endl; 
} 
//Output: 
Output 1: 
Enter the lower limit a : 0 
Enter the upper limit b: 12 
Enter the number of subintervals n: 8 
The value of the integral is: 1.5358 
The difference in Trapezoidal method = 0.0482 
Output 2: 
Enter the lower limit a : 0 
Enter the upper limit b: 12 
Enter the number of subintervals n: 1000 
The value of the integral is: 1.4877 
The difference in Trapezoidal method = 0.0000
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
QUESTION 3 
Write a C++ program to evaluate ∫ √ 
using Simpson’s 
1/3 rule. 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
double f(double x) 
{ 
double function; 
function= pow(sin(x)+cos(x), 0.5); 
return function; 
} 
void main() 
{ 
double a,b, h,sum_even=0,sum_odd=0,Simpson_13; 
int i,n; 
cout << "Enter the lower limit a: "; 
cin >> a; 
cout << "Enter the upper limit b: "; 
cin >> b; 
cout << "Enter the number of subintervals: "; 
cin >> n; 
h=(b-a)/n; 
for(i=1;i<n;i=i++) 
{ 
if(i%2==0)
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
sum_even= sum_even+ f(a+i*h); 
if(i%2!=0) 
sum_odd= sum_odd+ f(a+i*h); 
} 
Simpson_13=(h/3)*(f(a)+f(b)+ 4*sum_odd +2*sum_even); 
cout <<"The value of the integral is " << setprecision(4) <<fixed << Simpson_13 << 
"nn"; 
} 
//Output: 
Enter the lower limit a: 0 
Enter the upper limit b: 1 
Enter the number of subintervals: 8 
The value of the integral is 1.1394 
//Alternative way for question 3 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
double simpson13(double a,double b,int n) 
{ 
double z[9],Simp,h=(b-a)/n; 
double x[9]={0,0.125,0.25,0.375,0.5,0.625,0.75,0.875,1.0}; 
for (int i=0; i<9 ; i++) 
z[i]= pow(sin(x[i])+cos(x[i]),0.5);
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
Simp=(h/3)*((z[0]+z[8]) + 2*(z[2]+z[4]+z[6]) + 4*(z[1]+z[3]+z[5]+z[7]) ); 
return Simp; 
} 
int main() 
{ 
double J; 
J=simpson13(0,1,8); 
cout<< "The integral value is: "<< setprecision(4) <<fixed << J<<endl; 
return 0; 
} 
//Output: 
The integral value is: 1.1394 
QUESTION 4 
Write a C++ function sub-program to evaluate∫ 
using 
Simpson’s 1/3 correct to four decimal places. 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
double f(double x) 
{ 
float function; 
function= 1/(1+pow(x,2)); 
return function; 
}
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
void main() 
{ 
double a,b, h,sum_even=0,sum_odd=0,Simpson_13; 
int i,n; 
cout << "Enter the lower limit a: "; 
cin >> a; 
cout << "Enter the upper limit b: "; 
cin >> b; 
cout << "Enter the number of subintervals: "; 
cin >> n; 
h=(b-a)/n; 
for(i=1;i<n;i=i++) 
{ 
if(i%2==0) 
sum_even= sum_even+ f(a+i*h); 
if(i%2!=0) 
sum_odd= sum_odd+ f(a+i*h); 
} 
Simpson_13=(h/3)*(f(a)+f(b)+ 4*sum_odd +2*sum_even); 
cout <<"The value of the integral is " << setprecision(4) <<fixed << Simpson_13 << "nn"; 
} 
//Output: 
Enter the lower limit a: 0 
Enter the upper limit b: 12 
Enter the number of subintervals: 6 
The value of the integral is 1.4020
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
//Alternative way for question 4 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
double simpson13(double a,double b,int n) 
{ 
double z[7],Simp,h=(b-a)/n; 
double x[7]={0,2,4,6,8,10,12}; 
for (int i=0; i<7 ; i++) 
z[i]= 1/(1+pow(x[i],2)); 
Simp=(h/3)*((z[0]+z[6]) + 2*(z[2]+z[4]) + 4*(z[1]+z[3]+z[5]) ); 
return Simp; 
} 
int main() 
{ 
double J; 
J=simpson13(0,12,6); 
cout<< "The integral value is: "<< setprecision(4) <<fixed << J<<endl; 
return 0; 
} 
//Output 
The integral value is: 1.4020
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
QUESTION 5 
Write a C++ program to evaluate∫ 
using Simpson’s 3/8 
rule. 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
double f(double x) 
{ 
double function; 
function=exp(sin(x)); 
return function; 
} 
void main() 
{ 
double a,b, h,sum_miss=0,sum_triple=0,Simpson_38; 
int i,n; 
cout << "Enter the lower limit a: "; 
cin >> a; 
cout << "Enter the upper limit b: "; 
cin >> b; 
cout << "Enter the number of subintervals: "; 
//should be taken as multiples of 3 
cin >> n; 
h=(b-a)/n;
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
for (i=1;i<n;i++) 
{ 
if ( i%3 != 0 ) 
sum_miss = sum_miss + f(a+i*h); 
if(i%3==0) 
sum_triple =sum_triple+ f(a+i*h); 
} 
Simpson_38= (3*h/8)*((f(a)+f(b))+ 3*sum_miss +2*sum_triple); 
cout <<"The value of the integral is " << setprecision(4) <<fixed << Simpson_38 << "nn"; 
} 
//Output: 
Enter the lower limit a: 0 
Enter the upper limit b: 1.570796 
Enter the number of subintervals: 8 
The value of the integral is 3.0381 
QUESTION 6 
Write a C++ program to evaluate∫ 
using Simpson’s 3/8 
rule. 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std;
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
double f(double x) 
{ 
double function; 
function=log10(x); 
return function; 
} 
void main() 
{ 
double a,b, h,sum_miss=0,sum_triple=0,Simpson_38; 
int i,n; 
cout << "Enter the lower limit a: "; 
cin >> a; 
cout << "Enter the upper limit b: "; 
cin >> b; 
cout << "Enter the number of subintervals: "; 
//should be taken as multiples of 3 
cin >> n; 
h=(b-a)/n; 
for (i=1;i<n;i++) 
{ 
if ( i%3 != 0 ) 
sum_miss = sum_miss + f(a+i*h); 
if(i%3==0) 
sum_triple =sum_triple+ f(a+i*h); 
} 
Simpson_38= (3*h/8)*((f(a)+f(b))+ 3*sum_miss +2*sum_triple);
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
cout <<"The value of the integral is " << setprecision(4) <<fixed << Simpson_38 << "nn"; 
} 
//Output: 
Enter the lower limit a: 2 
Enter the upper limit b: 6 
Enter the number of subintervals: 8 
The value of the integral is 2.2833 
QUESTION 7 
Write a C++ function program to evaluate∫ 
using 
Trapezoidal, Simpson’s 1/3 and Simpson’s 3/8 rules. Compare the 
results with the exact solution 2.32012 
#include <iostream> 
#include <iomanip> 
#include <cmath> 
using namespace std; 
double f(double x) 
{ 
return exp(x) ; 
} 
double simpson_38(double a,double b,double n) 
{ 
double h=(b-a)/n, sum_miss=0,sum_triple=0,Simp38; 
for (int i=1;i<n;i++) 
{ 
if ( i%3 != 0 ) 
sum_miss = sum_miss + f(a+i*h); 
if(i%3==0) 
sum_triple =sum_triple+ f(a+i*h); 
}
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
Simp38= (3*h/8)*((f(a)+f(b))+ 3*sum_miss +2*sum_triple); 
return Simp38; 
} 
double simpson_13(double a,double b,double n) 
{ 
double h=(b-a)/n, sum_odd=0,sum_even=0,Simp13; 
for(int i=1; i<n; i++) 
{ 
if(i%2==0) 
sum_even =sum_even + f(a+i*h); 
else 
sum_odd=sum_odd + f(a+i*h); 
} 
Simp13 = (h/3)*( (f(a) + f(b)) + 4*sum_odd + 2*sum_even ); 
return Simp13; 
} 
double trapezoidal(double a,double b,double n) 
{ 
double sum=0, Trap, h=(b-a)/n; 
for(int i=1;i<n;i++) 
sum = sum+f(a+i*h); 
Trap = (h/2)*( f(a) + f(b) + 2*sum); 
return Trap ; 
} 
int main() 
{ 
double a,b,n, exact= 2.32012; 
int ans; 
char choice; 
Pilihan: 
cout << "1. Trapezoidaln2. Simpson's 1/3 rulen3. Simpson's 3/8 rulenn"; 
cout << "Which number you wanna choose : "; 
cin >> ans; 
cout << endl;
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
switch(ans) 
{ 
case 1 : cout << "Please input a,b and n : "; 
cin >> a >> b >> n; 
cout<<"The Exact value = "<< setprecision(6) <<exact<<endl; 
cout<<"The result for Trapezoidal method = " 
<<setprecision(6)<<trapezoidal(a,b,n)<<endl; 
cout<<"The difference in Trapezoidal method = "<<setprecision(6)<< 
fabs(exact - trapezoidal(a,b,n))<<endl; 
break; 
case 2 : cout << "Please input a,b and n : "; 
cin >> a >> b >> n; 
cout<<"The Exact value = "<<setprecision(6)<<exact<<endl; 
cout<<"The result for Simpson's 1/3 method = "<<setprecision(6)<< 
simpson_13(a,b,n)<<endl; 
cout<<"The difference in Simpson's 1/3 
method="<<setprecision(6)<<fabs(exact- simpson_13(a,b,n))<<endl; 
break; 
case 3 : cout << "Please input a,b and n : "; 
cin >> a >> b >> n; 
cout<<"The Exact value = "<<setprecision(6)<<exact<<endl; 
cout<<"The result for Simpson's 3/8 method = "<< setprecision(6)<< 
simpson_38(a,b,n)<<endl; 
cout<<"The difference in Simpson's 3/8 method 
="<<setprecision(6)<<fabs(exact- simpson_38(a,b,n))<<endl; 
break; 
default: cout << "Invalid number. Try again : "; 
cin >> ans; 
goto Pilihan; 
break; 
} 
if(fabs(exact-simpson_38(a,b,n)) <fabs(exact-simpson_13(a,b,n)) && 
fabs(exact-simpson_38(a,b,n))<fabs(exact-trapezoidal(a,b,n)))
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
cout<<"Simpson's 3/8 is better"<<endl; 
else if(fabs(exact-simpson_13(a,b,n))<fabs(exact-simpson_38(a,b,n)) && 
fabs(exact-simpson_13(a,b,n))<fabs(exact-trapezoidal(a,b,n))) 
cout<<"Simpson's 1/3 is better"<<endl; 
else 
cout<<"Trapezoidal rule is better"<<endl; 
check: 
cout << "Do u want to try again (Y/N): "; 
cin >> choice; 
if ( choice == 'N' || choice == 'n') 
cout << "Thank You." << endl; 
else if ( choice == 'Y' || choice == 'y') 
{ 
goto Pilihan; 
} 
else goto check; 
return 0; 
} 
//Output: 
1. Trapezoidal 
2. Simpson's 1/3 rule 
3. Simpson's 3/8 rule 
Which number you wanna choose : 1 
Please input a,b and n : 0 1.2 8 
The Exact value = 2.32012 
The result for Trapezoidal method = 2.32447 
The difference in Trapezoidal method = 0.00434551 
Simpson's 1/3 is better 
Do u want to try again (Y/N): y 
1. Trapezoidal 
2. Simpson's 1/3 rule 
3. Simpson's 3/8 rule 
Which number you wanna choose : 2 
Please input a,b and n : 0 1.2 8
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
The Exact value = 2.32012 
The result for Simpson's 1/3 method = 2.32012 
The difference in Simpson's 1/3 method=3.43063e-006 
Simpson's 1/3 is better 
Do u want to try again (Y/N): y 
1. Trapezoidal 
2. Simpson's 1/3 rule 
3. Simpson's 3/8 rule 
Which number you wanna choose : 3 
Please input a,b and n : 0 1.2 8 
The Exact value = 2.32012 
The result for Simpson's 3/8 method = 2.26695 
The difference in Simpson's 3/8 method =0.0531698 
Simpson's 1/3 is better 
Do u want to try again (Y/N): n 
Thank You. 
//Alternative for question 7 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
#define f(x) exp(x) 
double trap(double a,double b,double h,double n) 
{ 
int i; 
double x1,sumA=0,resultA; 
x1=a; 
for(i=1;i<n;i++) 
{ 
x1=x1+h; 
sumA=sumA+f(x1); 
} 
resultA=(h/2)*(f(a)+2*sumA+f(b)); 
return resultA; 
} 
double simp13(double a,double b,double h,double n) 
{ 
int j; 
double x2,sumB1=0,sumB2=0,resultB; 
x2=a; 
for(j=1;j<n;j++)
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
{ 
x2=x2+h; 
if(j%2==0) 
sumB1=sumB1+2*f(x2); 
else 
sumB2=sumB2+4*f(x2); 
} 
resultB=(h/3)*(f(a)+sumB1+sumB2+f(b)); 
return resultB; 
} 
double simp38(double a,double b,double h,double n) 
{ 
double sum_miss=0,sum_triple=0,Simp38; 
for (int i=1;i<n;i++) 
{ 
if ( i%3 != 0 ) 
sum_miss = sum_miss + f(a+i*h); 
if(i%3==0) 
sum_triple =sum_triple+ f(a+i*h); 
} 
Simp38= (3*h/8)*(f(a)+f(b)+ 3*sum_miss +2*sum_triple); 
return Simp38; 
} 
int main() 
{ 
int n; 
double a=0,b=1.2,h,exact= 2.32012; 
cout<<"Enter the value of n: "; 
cin>>n; 
h=(b-a)/n; 
cout<<"The Exact value = "<<setprecision(6)<<exact<<endl; 
cout<<"The result for Trapezoidal method = "<<setprecision(6)<<trap(a,b,h,n)<<endl; 
cout<<"The result for Simpson 1/3 method = "<<setprecision(6)<<simp13(a,b,h,n)<<endl; 
cout<<"The result for Simpson 3/8 method = "<<setprecision(6)<<simp38(a,b,h,n)<<endl; 
cout<<"The difference in Trapezoidal method = "<<setprecision(6) <<fabs(exact-trap( 
a,b,h,n))<<endl; 
cout<<"The difference in Simpson 1/3 method = "<<setprecision(6)<<fabs(exact-simp13( 
a,b,h,n))<<endl;
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
cout<<"The difference in Simpson 3/8 method = "<<setprecision(6)<<fabs(exact-simp38( 
a,b,h,n))<<endl; 
if(fabs(exact-simp38(a,b,h,n)) <fabs(exact-simp13(a,b,h,n)) && 
fabs(exact-simp38(a,b,h,n))<fabs(exact-trap(a,b,h,n))) 
cout<<"Simpson's 3/8 is better"<<endl; 
else if(fabs(exact-simp13(a,b,h,n))<fabs(exact-simp38(a,b,h,n)) && 
fabs(exact-simp13(a,b,h,n))<fabs(exact-trap(a,b,h,n))) 
cout<<"Simpson's 1/3 is better"<<endl; 
else 
cout<<"Trapezoidal rule is better"<<endl; 
return 0; 
} 
//Output: 
Enter the value of n: 8 
The Exact value = 2.32012 
The result for Trapezoidal method = 2.32447 
The result for Simpson 1/3 method = 2.32012 
The result for Simpson 3/8 method = 2.26695 
The difference in Trapezoidal method = 0.00434551 
The difference in Simpson 1/3 method = 3.43063e-006 
The difference in Simpson 3/8 method = 0.0531698 
Simpson's 1/3 is better 
//Alternative for question 7 
#include<iostream> 
#include<cmath> 
using namespace std; 
double f(double a) 
{ 
return (double) exp(a); 
} 
int main()
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
{ 
double a=0,b=1.2,n,h,Exact,sum3=0,sum=0,sumE=0,sumO=0; 
double sumT=0,simps3per8,simp1per3,trapzoid; 
cout<<"Enter the number of subintervals,n: "; 
cin>>n; 
h=(b-a)/n; 
Exact=exp(b)-exp(a); 
cout<<"Exact value="<<Exact<<endl; 
for(int i=1; i<n; i++) //Simpson 3/8 
{ 
if(i%3==0) 
sum3=sum3 + f(a+i*h); 
else 
sum=sum + f(a+i*h);} 
simps3per8=((3*h)/8)*(f(a)+f(b)+3*sum+2*sum3); 
cout<<"Simpson's 3/8 ="<<simps3per8<<endl; 
for(i=1; i<n; i++) //Simpson's 1/3 
{ 
if(i%2==0) 
sumE=sumE + f(a+i*h); 
else
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
sumO=sumO + f(a+i*h);} 
simp1per3=(h/3)*(f(a)+f(b)+4*sumO+2*sumE); 
cout<<"Simpson's 1/3 ="<<simp1per3<<endl; 
for(i=1;i<n;i++) //Trapezoidal 
sumT=sumT+f(a+i*h); 
trapzoid=(h/2)*(f(a)+f(b)+2*sumT); 
cout<<"Trapezoidal= "<<trapzoid<<endl; 
if(fabs(Exact-simps3per8)<fabs(Exact-simp1per3) && 
fabs(Exact-simps3per8)<fabs(Exact-trapzoid)) 
cout<<"Simpson's 3/8 is better"<<endl; 
else if(fabs(Exact-simp1per3)<fabs(Exact-simps3per8) && 
fabs(Exact-simp1per3)<fabs(Exact-trapzoid)) 
cout<<"Simpson's 1/3 is better"<<endl; 
else 
cout<<"Trapezoidal rule is better"<<endl; 
return 0; 
}
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 
//Output: 
Enter the number of subintervals,n: 8 
Exact value=2.32012 
Simpson's 3/8 =2.26695 
Simpson's 1/3 =2.32012 
Trapezoidal= 2.32447 
Simpson's 1/3 is better

More Related Content

What's hot

Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequenceKiran K
 
Greedy Algorithm - Huffman coding
Greedy Algorithm - Huffman codingGreedy Algorithm - Huffman coding
Greedy Algorithm - Huffman codingMd Monirul Alom
 
block ciphers
block ciphersblock ciphers
block ciphersAsad Ali
 
CHAIN RULE AND IMPLICIT FUNCTION
CHAIN RULE AND IMPLICIT FUNCTIONCHAIN RULE AND IMPLICIT FUNCTION
CHAIN RULE AND IMPLICIT FUNCTIONNikhil Pandit
 
Coin Change Problem
Coin Change ProblemCoin Change Problem
Coin Change ProblemDEVTYPE
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsKandarp Tiwari
 
Managing Large Flask Applications On Google App Engine (GAE)
Managing Large Flask Applications On Google App Engine (GAE)Managing Large Flask Applications On Google App Engine (GAE)
Managing Large Flask Applications On Google App Engine (GAE)Emmanuel Olowosulu
 
Ch6 formal relational query languages
Ch6 formal relational query languages Ch6 formal relational query languages
Ch6 formal relational query languages uoitc
 
Encryption And Decryption Using AES Algorithm
Encryption And Decryption Using AES AlgorithmEncryption And Decryption Using AES Algorithm
Encryption And Decryption Using AES AlgorithmAhmed Raza Shaikh
 
Double DES & Triple DES
Double DES & Triple DESDouble DES & Triple DES
Double DES & Triple DESHemant Sharma
 
Graphs for Ai and ML
Graphs for Ai and MLGraphs for Ai and ML
Graphs for Ai and MLNeo4j
 

What's hot (20)

Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
 
Greedy Algorithm - Huffman coding
Greedy Algorithm - Huffman codingGreedy Algorithm - Huffman coding
Greedy Algorithm - Huffman coding
 
Data structure
Data structureData structure
Data structure
 
block ciphers
block ciphersblock ciphers
block ciphers
 
CHAIN RULE AND IMPLICIT FUNCTION
CHAIN RULE AND IMPLICIT FUNCTIONCHAIN RULE AND IMPLICIT FUNCTION
CHAIN RULE AND IMPLICIT FUNCTION
 
Coin Change Problem
Coin Change ProblemCoin Change Problem
Coin Change Problem
 
Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
 
Fuzzy Set
Fuzzy SetFuzzy Set
Fuzzy Set
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
 
Managing Large Flask Applications On Google App Engine (GAE)
Managing Large Flask Applications On Google App Engine (GAE)Managing Large Flask Applications On Google App Engine (GAE)
Managing Large Flask Applications On Google App Engine (GAE)
 
Ch6 formal relational query languages
Ch6 formal relational query languages Ch6 formal relational query languages
Ch6 formal relational query languages
 
Encryption And Decryption Using AES Algorithm
Encryption And Decryption Using AES AlgorithmEncryption And Decryption Using AES Algorithm
Encryption And Decryption Using AES Algorithm
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Double DES & Triple DES
Double DES & Triple DESDouble DES & Triple DES
Double DES & Triple DES
 
Chapter 22 Finite Field
Chapter 22 Finite FieldChapter 22 Finite Field
Chapter 22 Finite Field
 
C_Arrays.pptx
C_Arrays.pptxC_Arrays.pptx
C_Arrays.pptx
 
Graphs for Ai and ML
Graphs for Ai and MLGraphs for Ai and ML
Graphs for Ai and ML
 
N queen problem
N queen problemN queen problem
N queen problem
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
 
Galois field
Galois fieldGalois field
Galois field
 

Viewers also liked

Functional Leap of Faith (Keynote at JDay Lviv 2014)
Functional Leap of Faith (Keynote at JDay Lviv 2014)Functional Leap of Faith (Keynote at JDay Lviv 2014)
Functional Leap of Faith (Keynote at JDay Lviv 2014)Tomer Gabel
 
Multiple sagement trapezoidal rule
Multiple sagement trapezoidal ruleMultiple sagement trapezoidal rule
Multiple sagement trapezoidal ruleTanmoy Debnath
 
25285 mws gen_int_ppt_trapcontinuous
25285 mws gen_int_ppt_trapcontinuous25285 mws gen_int_ppt_trapcontinuous
25285 mws gen_int_ppt_trapcontinuousJyoti Parange
 
Covering (Rules-based) Algorithm
Covering (Rules-based) AlgorithmCovering (Rules-based) Algorithm
Covering (Rules-based) AlgorithmZHAO Sam
 
2.4 rule based classification
2.4 rule based classification2.4 rule based classification
2.4 rule based classificationKrish_ver2
 
C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)Olve Maudal
 
Gaussian quadratures
Gaussian quadraturesGaussian quadratures
Gaussian quadraturesTarun Gehlot
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
Data Mining: Concepts and techniques classification _chapter 9 :advanced methods
Data Mining: Concepts and techniques classification _chapter 9 :advanced methodsData Mining: Concepts and techniques classification _chapter 9 :advanced methods
Data Mining: Concepts and techniques classification _chapter 9 :advanced methodsSalah Amean
 
Chapter 4 Classification
Chapter 4 ClassificationChapter 4 Classification
Chapter 4 ClassificationKhalid Elshafie
 
Numerical integration
Numerical integrationNumerical integration
Numerical integrationMohammed_AQ
 
Newton cotes integration method
Newton cotes integration  methodNewton cotes integration  method
Newton cotes integration methodshashikant pabari
 

Viewers also liked (20)

Integration
IntegrationIntegration
Integration
 
Data mining
Data miningData mining
Data mining
 
Functional Leap of Faith (Keynote at JDay Lviv 2014)
Functional Leap of Faith (Keynote at JDay Lviv 2014)Functional Leap of Faith (Keynote at JDay Lviv 2014)
Functional Leap of Faith (Keynote at JDay Lviv 2014)
 
Multiple sagement trapezoidal rule
Multiple sagement trapezoidal ruleMultiple sagement trapezoidal rule
Multiple sagement trapezoidal rule
 
25285 mws gen_int_ppt_trapcontinuous
25285 mws gen_int_ppt_trapcontinuous25285 mws gen_int_ppt_trapcontinuous
25285 mws gen_int_ppt_trapcontinuous
 
Calc 4.6
Calc 4.6Calc 4.6
Calc 4.6
 
Covering (Rules-based) Algorithm
Covering (Rules-based) AlgorithmCovering (Rules-based) Algorithm
Covering (Rules-based) Algorithm
 
Data mining notes
Data mining notesData mining notes
Data mining notes
 
2.4 rule based classification
2.4 rule based classification2.4 rule based classification
2.4 rule based classification
 
05 classification 1 decision tree and rule based classification
05 classification 1 decision tree and rule based classification05 classification 1 decision tree and rule based classification
05 classification 1 decision tree and rule based classification
 
Trapezoidal rule
Trapezoidal ruleTrapezoidal rule
Trapezoidal rule
 
C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)
 
Gaussian quadratures
Gaussian quadraturesGaussian quadratures
Gaussian quadratures
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Es272 ch6
Es272 ch6Es272 ch6
Es272 ch6
 
Data Mining: Concepts and techniques classification _chapter 9 :advanced methods
Data Mining: Concepts and techniques classification _chapter 9 :advanced methodsData Mining: Concepts and techniques classification _chapter 9 :advanced methods
Data Mining: Concepts and techniques classification _chapter 9 :advanced methods
 
Chapter 4 Classification
Chapter 4 ClassificationChapter 4 Classification
Chapter 4 Classification
 
Numerical integration
Numerical integrationNumerical integration
Numerical integration
 
Naive bayes
Naive bayesNaive bayes
Naive bayes
 
Newton cotes integration method
Newton cotes integration  methodNewton cotes integration  method
Newton cotes integration method
 

Similar to C++ TUTORIAL 8

Similar to C++ TUTORIAL 8 (20)

C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
C++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 
901131 examples
901131 examples901131 examples
901131 examples
 
Bijender (1)
Bijender (1)Bijender (1)
Bijender (1)
 
C++ file
C++ fileC++ file
C++ file
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
 
Lab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practiceLab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practice
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
Final DAA_prints.pdf
Final DAA_prints.pdfFinal DAA_prints.pdf
Final DAA_prints.pdf
 
7720
77207720
7720
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
 

More from Farhan Ab Rahman (11)

C++ TUTORIAL 10
C++ TUTORIAL 10C++ TUTORIAL 10
C++ TUTORIAL 10
 
C++ TUTORIAL 9
C++ TUTORIAL 9C++ TUTORIAL 9
C++ TUTORIAL 9
 
C++ TUTORIAL 7
C++ TUTORIAL 7C++ TUTORIAL 7
C++ TUTORIAL 7
 
C++ TUTORIAL 6
C++ TUTORIAL 6C++ TUTORIAL 6
C++ TUTORIAL 6
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
 
VIBRATIONS AND WAVES TUTORIAL#2
VIBRATIONS AND WAVES TUTORIAL#2VIBRATIONS AND WAVES TUTORIAL#2
VIBRATIONS AND WAVES TUTORIAL#2
 
Notis Surau
Notis SurauNotis Surau
Notis Surau
 
Kitab Bakurah.amani
Kitab Bakurah.amaniKitab Bakurah.amani
Kitab Bakurah.amani
 

Recently uploaded

Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Recently uploaded (20)

Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 

C++ TUTORIAL 8

  • 1. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 QUESTION 1 Write a C++ program to evaluate∫ using Trapezoidal rule up to three significant figures. #include <cmath> #include <iostream> #include <iomanip> using namespace std; float f(float x) { float func; func = exp(x); return func; } void main() { float a,b,h,sum=0, Trap; int i, n; cout <<"Enter the lower limit a : "; cin >> a; cout << "Enter the upper limit b: "; cin >> b; cout << "Enter the number of subintervals n: "; cin >> n; h=(b-a)/n;
  • 2. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 for(i=1;i<n;i++) /*or we can can write for(i=a+h;i<=b-h;i=i+h) { sum+=f(i);*/ { sum= sum + f(a+i*h); } Trap= (h/2)*(f(a) + f(b) +2 *sum); cout <<"nThe value of the integral is: " << setprecision(3) << Trap <<endl ; } //Output: Enter the lower limit a : 0 Enter the upper limit b: 1.2 Enter the number of subintervals n: 8 The value of the integral is: 2.32 //Alternative way for question 1 #include<iostream> #include<cmath> #include<iomanip> using namespace std; double trapezoidal(double a,double b,int n) {
  • 3. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 double h= (b-a)/n; double z[9],I; double x[9]= {0.0, 0.15, 0.3,0.45,0.6,0.75,0.9,1.05,1.2 }; for (int i=0; i<9 ; i++) z[i]= exp(x[i]); //the function is exp(x) I=h/2*( z[0]+z[8] + 2*(z[1]+z[2]+z[3]+z[4]+z[5]+z[6]+z[7]) ); return (I); } int main() { double J; J=trapezoidal(0,1.2,8); cout<< "The integral value is " << setprecision(3) << J << "nn"; return 0; } //Output: The integral value is 2.32 QUESTION 2 Write a C++ function sub-program to evaluate ∫ using Trapezoidal rule correct to four decimal places. Compare the result with the exact solution 1.48766
  • 4. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 #include <cmath> #include <iostream> #include <iomanip> using namespace std; float f(float x) { float func; func = 1/(1+(x*x)); return func; } void main() { double a,b,h,sum=0, Trap, exact=1.48766; // we can find the exact value by using the formula exact= atan(b) - atan(a); int i, n; cout <<"Enter the lower limit a : "; cin >> a; cout << "Enter the upper limit b: "; cin >> b; cout << "Enter the number of subintervals n: "; cin >> n; h=(b-a)/n; for(i=1;i<n;i++)
  • 5. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 /*or we can can write for(i=a+h;i<=b-h;i=i+h) { sum+=f(i);*/ { sum= sum + f(a+i*h); } Trap= (h/2)*(f(a) + f(b) +2 *sum); cout <<"nThe value of the integral is: " << setprecision(4) <<fixed << Trap <<endl ; cout<<"The difference in Trapezoidal method = "<<setprecision(4) << fixed <<fabs(exact- Trap)<<endl; } //Output: Output 1: Enter the lower limit a : 0 Enter the upper limit b: 12 Enter the number of subintervals n: 8 The value of the integral is: 1.5358 The difference in Trapezoidal method = 0.0482 Output 2: Enter the lower limit a : 0 Enter the upper limit b: 12 Enter the number of subintervals n: 1000 The value of the integral is: 1.4877 The difference in Trapezoidal method = 0.0000
  • 6. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 QUESTION 3 Write a C++ program to evaluate ∫ √ using Simpson’s 1/3 rule. #include<iostream> #include<cmath> #include<iomanip> using namespace std; double f(double x) { double function; function= pow(sin(x)+cos(x), 0.5); return function; } void main() { double a,b, h,sum_even=0,sum_odd=0,Simpson_13; int i,n; cout << "Enter the lower limit a: "; cin >> a; cout << "Enter the upper limit b: "; cin >> b; cout << "Enter the number of subintervals: "; cin >> n; h=(b-a)/n; for(i=1;i<n;i=i++) { if(i%2==0)
  • 7. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 sum_even= sum_even+ f(a+i*h); if(i%2!=0) sum_odd= sum_odd+ f(a+i*h); } Simpson_13=(h/3)*(f(a)+f(b)+ 4*sum_odd +2*sum_even); cout <<"The value of the integral is " << setprecision(4) <<fixed << Simpson_13 << "nn"; } //Output: Enter the lower limit a: 0 Enter the upper limit b: 1 Enter the number of subintervals: 8 The value of the integral is 1.1394 //Alternative way for question 3 #include<iostream> #include<cmath> #include<iomanip> using namespace std; double simpson13(double a,double b,int n) { double z[9],Simp,h=(b-a)/n; double x[9]={0,0.125,0.25,0.375,0.5,0.625,0.75,0.875,1.0}; for (int i=0; i<9 ; i++) z[i]= pow(sin(x[i])+cos(x[i]),0.5);
  • 8. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 Simp=(h/3)*((z[0]+z[8]) + 2*(z[2]+z[4]+z[6]) + 4*(z[1]+z[3]+z[5]+z[7]) ); return Simp; } int main() { double J; J=simpson13(0,1,8); cout<< "The integral value is: "<< setprecision(4) <<fixed << J<<endl; return 0; } //Output: The integral value is: 1.1394 QUESTION 4 Write a C++ function sub-program to evaluate∫ using Simpson’s 1/3 correct to four decimal places. #include<iostream> #include<cmath> #include<iomanip> using namespace std; double f(double x) { float function; function= 1/(1+pow(x,2)); return function; }
  • 9. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 void main() { double a,b, h,sum_even=0,sum_odd=0,Simpson_13; int i,n; cout << "Enter the lower limit a: "; cin >> a; cout << "Enter the upper limit b: "; cin >> b; cout << "Enter the number of subintervals: "; cin >> n; h=(b-a)/n; for(i=1;i<n;i=i++) { if(i%2==0) sum_even= sum_even+ f(a+i*h); if(i%2!=0) sum_odd= sum_odd+ f(a+i*h); } Simpson_13=(h/3)*(f(a)+f(b)+ 4*sum_odd +2*sum_even); cout <<"The value of the integral is " << setprecision(4) <<fixed << Simpson_13 << "nn"; } //Output: Enter the lower limit a: 0 Enter the upper limit b: 12 Enter the number of subintervals: 6 The value of the integral is 1.4020
  • 10. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 //Alternative way for question 4 #include<iostream> #include<cmath> #include<iomanip> using namespace std; double simpson13(double a,double b,int n) { double z[7],Simp,h=(b-a)/n; double x[7]={0,2,4,6,8,10,12}; for (int i=0; i<7 ; i++) z[i]= 1/(1+pow(x[i],2)); Simp=(h/3)*((z[0]+z[6]) + 2*(z[2]+z[4]) + 4*(z[1]+z[3]+z[5]) ); return Simp; } int main() { double J; J=simpson13(0,12,6); cout<< "The integral value is: "<< setprecision(4) <<fixed << J<<endl; return 0; } //Output The integral value is: 1.4020
  • 11. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 QUESTION 5 Write a C++ program to evaluate∫ using Simpson’s 3/8 rule. #include<iostream> #include<cmath> #include<iomanip> using namespace std; double f(double x) { double function; function=exp(sin(x)); return function; } void main() { double a,b, h,sum_miss=0,sum_triple=0,Simpson_38; int i,n; cout << "Enter the lower limit a: "; cin >> a; cout << "Enter the upper limit b: "; cin >> b; cout << "Enter the number of subintervals: "; //should be taken as multiples of 3 cin >> n; h=(b-a)/n;
  • 12. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 for (i=1;i<n;i++) { if ( i%3 != 0 ) sum_miss = sum_miss + f(a+i*h); if(i%3==0) sum_triple =sum_triple+ f(a+i*h); } Simpson_38= (3*h/8)*((f(a)+f(b))+ 3*sum_miss +2*sum_triple); cout <<"The value of the integral is " << setprecision(4) <<fixed << Simpson_38 << "nn"; } //Output: Enter the lower limit a: 0 Enter the upper limit b: 1.570796 Enter the number of subintervals: 8 The value of the integral is 3.0381 QUESTION 6 Write a C++ program to evaluate∫ using Simpson’s 3/8 rule. #include<iostream> #include<cmath> #include<iomanip> using namespace std;
  • 13. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 double f(double x) { double function; function=log10(x); return function; } void main() { double a,b, h,sum_miss=0,sum_triple=0,Simpson_38; int i,n; cout << "Enter the lower limit a: "; cin >> a; cout << "Enter the upper limit b: "; cin >> b; cout << "Enter the number of subintervals: "; //should be taken as multiples of 3 cin >> n; h=(b-a)/n; for (i=1;i<n;i++) { if ( i%3 != 0 ) sum_miss = sum_miss + f(a+i*h); if(i%3==0) sum_triple =sum_triple+ f(a+i*h); } Simpson_38= (3*h/8)*((f(a)+f(b))+ 3*sum_miss +2*sum_triple);
  • 14. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 cout <<"The value of the integral is " << setprecision(4) <<fixed << Simpson_38 << "nn"; } //Output: Enter the lower limit a: 2 Enter the upper limit b: 6 Enter the number of subintervals: 8 The value of the integral is 2.2833 QUESTION 7 Write a C++ function program to evaluate∫ using Trapezoidal, Simpson’s 1/3 and Simpson’s 3/8 rules. Compare the results with the exact solution 2.32012 #include <iostream> #include <iomanip> #include <cmath> using namespace std; double f(double x) { return exp(x) ; } double simpson_38(double a,double b,double n) { double h=(b-a)/n, sum_miss=0,sum_triple=0,Simp38; for (int i=1;i<n;i++) { if ( i%3 != 0 ) sum_miss = sum_miss + f(a+i*h); if(i%3==0) sum_triple =sum_triple+ f(a+i*h); }
  • 15. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 Simp38= (3*h/8)*((f(a)+f(b))+ 3*sum_miss +2*sum_triple); return Simp38; } double simpson_13(double a,double b,double n) { double h=(b-a)/n, sum_odd=0,sum_even=0,Simp13; for(int i=1; i<n; i++) { if(i%2==0) sum_even =sum_even + f(a+i*h); else sum_odd=sum_odd + f(a+i*h); } Simp13 = (h/3)*( (f(a) + f(b)) + 4*sum_odd + 2*sum_even ); return Simp13; } double trapezoidal(double a,double b,double n) { double sum=0, Trap, h=(b-a)/n; for(int i=1;i<n;i++) sum = sum+f(a+i*h); Trap = (h/2)*( f(a) + f(b) + 2*sum); return Trap ; } int main() { double a,b,n, exact= 2.32012; int ans; char choice; Pilihan: cout << "1. Trapezoidaln2. Simpson's 1/3 rulen3. Simpson's 3/8 rulenn"; cout << "Which number you wanna choose : "; cin >> ans; cout << endl;
  • 16. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 switch(ans) { case 1 : cout << "Please input a,b and n : "; cin >> a >> b >> n; cout<<"The Exact value = "<< setprecision(6) <<exact<<endl; cout<<"The result for Trapezoidal method = " <<setprecision(6)<<trapezoidal(a,b,n)<<endl; cout<<"The difference in Trapezoidal method = "<<setprecision(6)<< fabs(exact - trapezoidal(a,b,n))<<endl; break; case 2 : cout << "Please input a,b and n : "; cin >> a >> b >> n; cout<<"The Exact value = "<<setprecision(6)<<exact<<endl; cout<<"The result for Simpson's 1/3 method = "<<setprecision(6)<< simpson_13(a,b,n)<<endl; cout<<"The difference in Simpson's 1/3 method="<<setprecision(6)<<fabs(exact- simpson_13(a,b,n))<<endl; break; case 3 : cout << "Please input a,b and n : "; cin >> a >> b >> n; cout<<"The Exact value = "<<setprecision(6)<<exact<<endl; cout<<"The result for Simpson's 3/8 method = "<< setprecision(6)<< simpson_38(a,b,n)<<endl; cout<<"The difference in Simpson's 3/8 method ="<<setprecision(6)<<fabs(exact- simpson_38(a,b,n))<<endl; break; default: cout << "Invalid number. Try again : "; cin >> ans; goto Pilihan; break; } if(fabs(exact-simpson_38(a,b,n)) <fabs(exact-simpson_13(a,b,n)) && fabs(exact-simpson_38(a,b,n))<fabs(exact-trapezoidal(a,b,n)))
  • 17. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 cout<<"Simpson's 3/8 is better"<<endl; else if(fabs(exact-simpson_13(a,b,n))<fabs(exact-simpson_38(a,b,n)) && fabs(exact-simpson_13(a,b,n))<fabs(exact-trapezoidal(a,b,n))) cout<<"Simpson's 1/3 is better"<<endl; else cout<<"Trapezoidal rule is better"<<endl; check: cout << "Do u want to try again (Y/N): "; cin >> choice; if ( choice == 'N' || choice == 'n') cout << "Thank You." << endl; else if ( choice == 'Y' || choice == 'y') { goto Pilihan; } else goto check; return 0; } //Output: 1. Trapezoidal 2. Simpson's 1/3 rule 3. Simpson's 3/8 rule Which number you wanna choose : 1 Please input a,b and n : 0 1.2 8 The Exact value = 2.32012 The result for Trapezoidal method = 2.32447 The difference in Trapezoidal method = 0.00434551 Simpson's 1/3 is better Do u want to try again (Y/N): y 1. Trapezoidal 2. Simpson's 1/3 rule 3. Simpson's 3/8 rule Which number you wanna choose : 2 Please input a,b and n : 0 1.2 8
  • 18. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 The Exact value = 2.32012 The result for Simpson's 1/3 method = 2.32012 The difference in Simpson's 1/3 method=3.43063e-006 Simpson's 1/3 is better Do u want to try again (Y/N): y 1. Trapezoidal 2. Simpson's 1/3 rule 3. Simpson's 3/8 rule Which number you wanna choose : 3 Please input a,b and n : 0 1.2 8 The Exact value = 2.32012 The result for Simpson's 3/8 method = 2.26695 The difference in Simpson's 3/8 method =0.0531698 Simpson's 1/3 is better Do u want to try again (Y/N): n Thank You. //Alternative for question 7 #include<iostream> #include<cmath> #include<iomanip> using namespace std; #define f(x) exp(x) double trap(double a,double b,double h,double n) { int i; double x1,sumA=0,resultA; x1=a; for(i=1;i<n;i++) { x1=x1+h; sumA=sumA+f(x1); } resultA=(h/2)*(f(a)+2*sumA+f(b)); return resultA; } double simp13(double a,double b,double h,double n) { int j; double x2,sumB1=0,sumB2=0,resultB; x2=a; for(j=1;j<n;j++)
  • 19. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 { x2=x2+h; if(j%2==0) sumB1=sumB1+2*f(x2); else sumB2=sumB2+4*f(x2); } resultB=(h/3)*(f(a)+sumB1+sumB2+f(b)); return resultB; } double simp38(double a,double b,double h,double n) { double sum_miss=0,sum_triple=0,Simp38; for (int i=1;i<n;i++) { if ( i%3 != 0 ) sum_miss = sum_miss + f(a+i*h); if(i%3==0) sum_triple =sum_triple+ f(a+i*h); } Simp38= (3*h/8)*(f(a)+f(b)+ 3*sum_miss +2*sum_triple); return Simp38; } int main() { int n; double a=0,b=1.2,h,exact= 2.32012; cout<<"Enter the value of n: "; cin>>n; h=(b-a)/n; cout<<"The Exact value = "<<setprecision(6)<<exact<<endl; cout<<"The result for Trapezoidal method = "<<setprecision(6)<<trap(a,b,h,n)<<endl; cout<<"The result for Simpson 1/3 method = "<<setprecision(6)<<simp13(a,b,h,n)<<endl; cout<<"The result for Simpson 3/8 method = "<<setprecision(6)<<simp38(a,b,h,n)<<endl; cout<<"The difference in Trapezoidal method = "<<setprecision(6) <<fabs(exact-trap( a,b,h,n))<<endl; cout<<"The difference in Simpson 1/3 method = "<<setprecision(6)<<fabs(exact-simp13( a,b,h,n))<<endl;
  • 20. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 cout<<"The difference in Simpson 3/8 method = "<<setprecision(6)<<fabs(exact-simp38( a,b,h,n))<<endl; if(fabs(exact-simp38(a,b,h,n)) <fabs(exact-simp13(a,b,h,n)) && fabs(exact-simp38(a,b,h,n))<fabs(exact-trap(a,b,h,n))) cout<<"Simpson's 3/8 is better"<<endl; else if(fabs(exact-simp13(a,b,h,n))<fabs(exact-simp38(a,b,h,n)) && fabs(exact-simp13(a,b,h,n))<fabs(exact-trap(a,b,h,n))) cout<<"Simpson's 1/3 is better"<<endl; else cout<<"Trapezoidal rule is better"<<endl; return 0; } //Output: Enter the value of n: 8 The Exact value = 2.32012 The result for Trapezoidal method = 2.32447 The result for Simpson 1/3 method = 2.32012 The result for Simpson 3/8 method = 2.26695 The difference in Trapezoidal method = 0.00434551 The difference in Simpson 1/3 method = 3.43063e-006 The difference in Simpson 3/8 method = 0.0531698 Simpson's 1/3 is better //Alternative for question 7 #include<iostream> #include<cmath> using namespace std; double f(double a) { return (double) exp(a); } int main()
  • 21. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 { double a=0,b=1.2,n,h,Exact,sum3=0,sum=0,sumE=0,sumO=0; double sumT=0,simps3per8,simp1per3,trapzoid; cout<<"Enter the number of subintervals,n: "; cin>>n; h=(b-a)/n; Exact=exp(b)-exp(a); cout<<"Exact value="<<Exact<<endl; for(int i=1; i<n; i++) //Simpson 3/8 { if(i%3==0) sum3=sum3 + f(a+i*h); else sum=sum + f(a+i*h);} simps3per8=((3*h)/8)*(f(a)+f(b)+3*sum+2*sum3); cout<<"Simpson's 3/8 ="<<simps3per8<<endl; for(i=1; i<n; i++) //Simpson's 1/3 { if(i%2==0) sumE=sumE + f(a+i*h); else
  • 22. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 sumO=sumO + f(a+i*h);} simp1per3=(h/3)*(f(a)+f(b)+4*sumO+2*sumE); cout<<"Simpson's 1/3 ="<<simp1per3<<endl; for(i=1;i<n;i++) //Trapezoidal sumT=sumT+f(a+i*h); trapzoid=(h/2)*(f(a)+f(b)+2*sumT); cout<<"Trapezoidal= "<<trapzoid<<endl; if(fabs(Exact-simps3per8)<fabs(Exact-simp1per3) && fabs(Exact-simps3per8)<fabs(Exact-trapzoid)) cout<<"Simpson's 3/8 is better"<<endl; else if(fabs(Exact-simp1per3)<fabs(Exact-simps3per8) && fabs(Exact-simp1per3)<fabs(Exact-trapzoid)) cout<<"Simpson's 1/3 is better"<<endl; else cout<<"Trapezoidal rule is better"<<endl; return 0; }
  • 23. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 //Output: Enter the number of subintervals,n: 8 Exact value=2.32012 Simpson's 3/8 =2.26695 Simpson's 1/3 =2.32012 Trapezoidal= 2.32447 Simpson's 1/3 is better