SlideShare a Scribd company logo
1 of 23
Download to read offline
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
QUESTION 1
Write a C++ program to find a root of the equation x3 – 4x – 8.95 = 0 accurate to
three decimal places using the Bisection method in the interval (2, 3).
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
void main()
{
double x1=2, x2=3, x3, fx1, fx2,fx3;
cout << "x1=t" << "|x2=t"<<"|x3=t" << "|fx1=tt" << "|fx2=tt" << "|fx3=t" << endl;
do
{
x3 = (x1 + x2)/2;
fx1= pow(x1,3) - 4*x1 - 8.95;
fx2= pow(x2,3) - 4*x2 - 8.95;
fx3= pow(x3,3) - 4*x3 - 8.95;
cout << x1 << "t" <<x2 << "t" <<x3 <<"t" << fx1 <<"tt" << fx2 << "tt" <<fx3 <<endl;
if(fx1*fx3 <0)
{
x2=x3;
}
else
{
x1=x3;
}
} while ( fabs(x1 - x2) > 0.000001);
cout << "nThe root is= " << setprecision(3) <<fixed << x3 <<endl;
}
// Output:
x1= |x2= |x3= |fx1= |fx2= |fx3=
2 3 2.5 -8.95 6.05 -3.325
2.5 3 2.75 -3.325 6.05 0.846875
2.5 2.75 2.625 -3.325 0.846875 -1.36211
2.625 2.75 2.6875 -1.36211 0.846875 -0.289111
2.6875 2.75 2.71875 -0.289111 0.846875 0.270917
2.6875 2.71875 2.70313 -0.289111 0.270917 -0.0110771
2.70313 2.71875 2.71094 -0.0110771 0.270917 0.129423
2.70313 2.71094 2.70703 -0.0110771 0.129423 0.0590492
2.70313 2.70703 2.70508 -0.0110771 0.0590492 0.0239551
2.70313 2.70508 2.7041 -0.0110771 0.0239551 0.00643126
2.70313 2.7041 2.70361 -0.0110771 0.00643126 -0.00232486
2.70361 2.7041 2.70386 -0.00232486 0.00643126 0.00205271
2.70361 2.70386 2.70374 -0.00232486 0.00205271 -0.000136197
2.70374 2.70386 2.7038 -0.000136197 0.00205271 0.000958227
2.70374 2.7038 2.70377 -0.000136197 0.000958227 0.000411007
2.70374 2.70377 2.70375 -0.000136197 0.000411007 0.000137403
2.70374 2.70375 2.70374 -0.000136197 0.000137403 6.02383e-007
2.70374 2.70374 2.70374 -0.000136197 6.02383e-007 -6.77976e-005
2.70374 2.70374 2.70374 -6.77976e-005 6.02383e-007 -3.35976e-005
2.70374 2.70374 2.70374 -3.35976e-005 6.02383e-007 -1.64976e-005
The root is= 2.704 (3 decimal places)
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
QUESTION 2
Write a C++ program to find a root of the equation ex – 3x = 0 using the Bisection method.
#include <iostream>
#include<cmath>
using namespace std;
void main()
{
int n, count=1;
double x0,x1,x2,fx0,fx1,fx2;
cout <<"nSolution by BISECTION method n";
cout <<"nThe equation is: exp(x) - 3*x= 0nn";
cout << "Enter the number of iterations: ";
cin >> n;
/*Finding an Approximate ROOT of Given Equation, Having +ve Value*/
for(x2=1;;x2++)
{
fx2= exp(x2) - 3*x2;
if (fx2 > 0)
{
break;
}
}
/*Finding an Approximate ROOT of Given Equation, Having -ve Value*/
for(x1=x2-1; ; x2--)
{
fx1= exp(x1) - 3*x1;
if( fx1 <0)
{
break;
}
}
//Printing Result
cout << "tt-----------------------------------------";
cout <<"ntt ITERATIONStt ROOTSn";
cout <<"tt-----------------------------------------";
for(;count<=n;count++)
{
x0=(x1+x2)/2;
fx0 = exp(x0) - 3*x0;
if( fx0 ==0)
{
cout << x0;
}
else if(fx0*fx1<0)
{
x2=x0;
}
else
{
x1=x0;
fx1=fx0;
}
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
cout <<"nttITERATION " << count;
cout <<":tt" << x0;
if(fabs((x1-x2)) < 0.000001)
{
cout <<"ntt---------------------------------";
cout <<"ntRoot = "<< x0;
cout << "ntIterations = " << count <<endl;
cout << "tt---------------------------------";
}
}
cout << "ntt----------------------------------------";
cout << "nttRoot = " << x0;
cout << "nttIterations = " << count-1<<endl;
cout << "tt----------------------------------------" << endl;
}
//Output:
Solution by BISECTION method
The equation is: exp(x) - 3*x= 0
Enter the number of iterations: 19
-----------------------------------------
ITERATIONS ROOTS
-----------------------------------------
ITERATION 1: 1.5
ITERATION 2: 1.75
ITERATION 3: 1.625
ITERATION 4: 1.5625
ITERATION 5: 1.53125
ITERATION 6: 1.51563
ITERATION 7: 1.50781
ITERATION 8: 1.51172
ITERATION 9: 1.51367
ITERATION 10: 1.5127
ITERATION 11: 1.51221
ITERATION 12: 1.51196
ITERATION 13: 1.51208
ITERATION 14: 1.51215
ITERATION 15: 1.51212
ITERATION 16: 1.51213
ITERATION 17: 1.51214
ITERATION 18: 1.51213
ITERATION 19: 1.51213
----------------------------------------
Root = 1.51213
Iterations = 19
----------------------------------------
QUESTION 3
Write a C++ function program to find the roof of f(x) = x6 – x – 1 = 0 accurate to within ξ = 0.001.
Use the Bisection method.
#include <iostream>
#include<cmath>
using namespace std;
#define EPS 0.001
#define f(x) pow(x,6) - x - 1
//global variables
int count=1,n;
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
void Bisect()
{
double x0,x1,x2,a,b,c;
/*Finding an Approximate ROOT of Given Equation, having +ve Value*/
for(x2=1;;x2++)
{
c=f(x2);
if (c > 0)
{
break;
}
}
/*Finding an Approximate ROOT of Given Equation, Having -ve Value*/
for(x1=x2-1;;x2--)
{
b=f(x1);
if(b <0)
{
break;
}
}
//Printing Result
cout << "tt-----------------------------------------";
cout <<"ntt ITERATIONStt ROOTSn";
cout <<"tt-----------------------------------------";
for( ; count<=n; count++)
{
x0=(x1+x2)/2;
a =f(x0);
if(a ==0)
{
x0;
}
else if(a*b<0)
{
x2=x0;
}
else
{
x1=x0;
b=a;
}
cout <<"nttITERATION " << count;
cout <<":tt" << x0;
if(fabs((x1-x2)) < EPS)
{
cout <<"ntt----------------------------------";
cout <<"ntRoot = "<< x0;
cout << "ntIterations = " << count <<endl;
cout << "tt----------------------------------";
}
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
}
cout << "ntt----------------------------------------";
cout << "nttRoot = " << x0;
cout << "nttIterations = " << count-1<<endl;
cout << "tt----------------------------------------" << endl;
}
void main()
{
cout <<"nSolution by BISECTION method n";
cout <<"nThe equation is: x^6- x -1= 0nn";
cout << "Enter the number of iterations: ";
cin >> n;
Bisect();
}
//Output:
Solution by BISECTION method
The equation is: x^6- x -1= 0
Enter the number of iterations: 20
-----------------------------------------
ITERATIONS ROOTS
-----------------------------------------
ITERATION 1: 1.5
ITERATION 2: 1.25
ITERATION 3: 1.125
ITERATION 4: 1.1875
ITERATION 5: 1.15625
ITERATION 6: 1.14063
ITERATION 7: 1.13281
ITERATION 8: 1.13672
ITERATION 9: 1.13477
ITERATION 10: 1.13379
----------------------------------
Root = 1.13379
Iterations = 10
----------------------------------
ITERATION 11: 1.13428
----------------------------------
Root = 1.13428
Iterations = 11
----------------------------------
ITERATION 12: 1.13452
----------------------------------
Root = 1.13452
Iterations = 12
----------------------------------
ITERATION 13: 1.13464
----------------------------------
Root = 1.13464
Iterations = 13
----------------------------------
ITERATION 14: 1.1347
----------------------------------
Root = 1.1347
Iterations = 14
----------------------------------
ITERATION 15: 1.13474
----------------------------------
Root = 1.13474
Iterations = 15
----------------------------------
ITERATION 16: 1.13472
----------------------------------
Root = 1.13472
Iterations = 16
----------------------------------
ITERATION 17: 1.13473
----------------------------------
Root = 1.13473
Iterations = 17
----------------------------------
ITERATION 18: 1.13472
----------------------------------
Root = 1.13472
Iterations = 18
----------------------------------
ITERATION 19: 1.13473
----------------------------------
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
Root = 1.13473
Iterations = 19
----------------------------------
ITERATION 20: 1.13472
----------------------------------
Root = 1.13472
Iterations = 20
----------------------------------
----------------------------------------
Root = 1.13472
Iterations = 20
----------------------------------------
QUESTION 4
Write a C++ Function program to find the smallest positive root of the cubic
equation ax3+bx2+cx+d=0 using Bisection method. You can take error tolerance
of 10-6
#include <iostream>
#include <cmath>
using namespace std;
#define f(x) (a*pow(x,3)+b*pow(x,2)+c*x+d)
double Bisection(double a, double b, double c, double d)
{
double x1, x2;
double mid_x;
x1 = 0;
x2 = 1; // We need this (in other case x2 is random value)
mid_x = 0.5; // initialize all variables before use
while( f(x1) * f(x2) > 0)
{
x1 = x2;
x2 = x1 + 1;
if (x2 > 1000)
return -1.0; // to prevent overflow
}
for(int i = 1; i < 100; i++)
{
mid_x = (x1 + x2) / 2.0;
if(f(x1) * f(mid_x) < 0)
{
x2 = mid_x;
}
else
{
x1 = mid_x;
}
if(fabs(x1 - x2) < 0.0000001)
{
return (x1 + x2) / 2; // will return immediately
}
}
return mid_x; // return anything. Code can to bounce between two numbers
}
int main()
{
double a, b, c, d, h;
ULANG:
cout << "Enter the value of constants for the cubic equation: " << endl;
cin >> a>> b>> c>> d;
if(a==0)
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
{
cout << "The value for constant a cannot equal to 0.Try again:n" << endl;
goto ULANG;
}
h = Bisection(a, b, c, d);
if (h >= 0)
cout << "The smallest +ve root of the cubic equation using Bisection method is: "
<< h << endl;
else
cout << "The root is not found in range from 0 to 1000" << endl;
return 0;
}
//Output:
Enter the value of constants for the cubic equation:
0 1 6 -1
The value for constant a cannot equal to 0.Try again:
Enter the value of constants for the cubic equation:
6 2 0 -1
The smallest +ve root of the cubic equation using Bisection method is: 0.45872
/******ALTERNATIVE METHOD FOR QUESTION 4******/
Done by FAIZ MATHS STUDENT, UM (trust me, he is so genius)
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
void bisec(double a, double b);
void choosepositive(int& x, int& y);
void choosenegative(int& x, int& y);
void zero(int z);
double f(double x);
double mid(double x,double y);
double p,q,r,s;
int main()
{
int z,l=0,n=1,m=0,o=-1,e,g,t=0,u=0,h;
cout << "BISECTION METHOD [CUBIC EQUATION]nnEnter ax^3+bx^2+c+dn" << endl;
cin >> p >> q >> r >> s;
do
{
do
{
zero(m);
cout << "nChoose:n" << "1. +ve rootn" << "2. -ve rootn" << endl;
cin >> z;
if (z==1)
{
choosepositive(l,n);e=n;g=n+1;
if (f(l)*f(n) == 0 && f(n)!=0) {cout << "NO MORE REAL +VE ROOT" <<
endl;break;}
if (n==100){cout<<"NO MORE REAL +VE ROOTn";break;}
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
bisec(l,n);break;
}
else if (z==2)
{
choosenegative(m,o);t=o;u=o-1;
if (f(m)*f(o) == 0 && f(o)!=0) {cout << "NO MORE REAL -VE ROOT" <<
endl;break;}
if (o==-100){cout<<"NO MORE REAL -VE ROOTn";break;}
bisec(m,o);break;
}
}
while (z!=1 || z!=2);
cout << "Want Another root?n1. YESn2. NO (EXIT)n" << endl;
cin >> h;l=e;n=g;m=t;o=u;
}
while (h==1);
return 0;
}
void bisec(double a, double b)
{
double n=1,r;
if (f(b)==0) {cout << "nThe root is " << b <<endl;}
else
{
r=mid(a,b);
cout << "n attbttxttb-rttf(x)" <<endl;
do
{
cout << setprecision(0) << n << " "<< setprecision(6) << fixed << a << "t"
<< b << "t" << r << "t" << b-r << "t" << f(r) << endl;
if (f(a)*f(r) < 0) b=r; else if (f(r)*f(b) < 0) a=r; else if (f(r)==0) break;
r=mid(a,b);n++;
}
while (abs(b-r) > 0.000001 );
cout << "nThe root is " << r <<endl;
}
}
void choosepositive(int& x, int& y) { while (f(x)*f(y) > 0 && y<100) {x=y;y++;} }
void choosenegative(int& x, int& y) { while (f(x)*f(y) > 0 && y>-100) {x=y;y--;} }
void zero(int z) {if (f(0)==0) {cout << "nThe root is 0"<< endl;}}
double f(double x) { return (double) p*pow(x,3)+q*pow(x,2)+r*x+s;}
double mid(double x,double y) {return (double)(x+y)/2;}
//Output:
BISECTION METHOD [CUBIC EQUATION]
Enter ax^3+bx^2+c+d
1 2 3 4
Choose:
1. +ve root
2. -ve root
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
1
NO MORE REAL +VE ROOT
Want Another root?
1. YES
2. NO (EXIT)
1
Choose:
1. +ve root
2. -ve root
2
n a b x b-r f(x)
1 -1.000000 -2.000000 -1.500000 -0.500000 0.625000
The root is -1.750000
Want Another root?
1. YES
2. NO (EXIT)
2
QUESTION 5
Write a C++ program to find a root of the function f (x) = ex – 3x2 to an accuracy
of 5 digits in the interval 0.5 and 1.0 using the false position method
#include<iostream>
#include<cmath>
using namespace std;
void main()
{
double x1=0.5,x2=1.0, x3, fx1, fx2,fx3;
cout << "x1=t" << "|x2=tt"<<"|x3=tt" << "|fx1=tt" << "|fx2=tt" << "|fx3=t" << endl;
do
{
fx1= exp(x1) - 3*pow(x1,2);
fx2= exp(x2) - 3*pow(x2,2);
x3 = (x1*fx2 -x2*fx1)/(fx2-fx1);
fx3= exp(x3) - 3*pow(x3,2);
cout << x1 << "t" <<x2 << "t" <<x3 <<"t" << fx1 <<"tt" << fx2 << "tt" <<fx3 <<endl;
if(fx1*fx3 <0)
{
x2=x3;
}
else
{
x1=x3;
}
} while ( fabs(x1 - x2) > 0.000001);
cout << "nThe root is= " << x3 <<endl;
}
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
//Output:
x1= |x2= |x3= |fx1= |fx2= |fx3=
0.5 1 0.880672 0.898721 -0.281718 0.0857699
0.880672 1 0.908523 0.0857699 -0.281718 0.0044143
0.908523 1 0.909934 0.0044143 -0.281718 0.000218671
0.909934 1 0.910004 0.000218671 -0.281718 1.08115e-005
0.910004 1 0.910007 1.08115e-005 -0.281718 5.34494e-007
0.910007 1 0.910008 5.34494e-007 -0.281718 2.64238e-008
0.910008 1 0.910008 2.64238e-008 -0.281718 1.30632e-009
0.910008 1 0.910008 1.30632e-009 -0.281718 6.45808e-011
0.910008 1 0.910008 6.45808e-011 -0.281718 3.19211e-012
0.910008 1 0.910008 3.19211e-012 -0.281718 1.58096e-013
0.910008 1 0.910008 1.58096e-013 -0.281718 7.10543e-015
0.910008 1 0.910008 7.10543e-015 -0.281718 1.33227e-015
0.910008 1 0.910008 1.33227e-015 -0.281718 -4.44089e-016
The root is= 0.910008
QUESTION 6
Write a C++ function program to find a root of cos x – 3x + 5 = 0 using the false
Position method.
#include<iostream>
#include<cmath>
using namespace std;
double f(double x)
{
double fx;
fx= cos(x) - 3*x +5;;
return fx;
}
void main()
{
double x1, x2,x3;
cout << "Enter x1: ";
cin >> x1;
cout << "Enter x2: ";
cin >> x2;
cout << "x1=t" << "|x2=tt"<<"|x3=tt" << "|fx1=tt" << "|fx2=tt" << "|fx3=t" << endl;
do
{
x3 = (x1*f(x2) -x2*f(x1))/(f(x2)-f(x1));
cout << x1 << "t" <<x2 << "t" <<x3 <<"t" << f(x1) <<"tt" << f(x2) << "tt" <<f(x3) <<endl;
if(f(x1)*f(x3) <0)
{
x2=x3;
}
else
{
x1=x3;
}
} while ( fabs(x1 - x2) > 0.000001); //the epsilon value can be changed
/***The function is like a straight line that regular falsi can extrapolate to a root even from far
away, such as from (10, 100). But it is not a straight line, so it cannot converge with that much
precision. To avoid this, we can try dropping our precision requirements by order of magnitude
and it will work ****/
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
cout << "nThe root is= " << x3 <<endl;
}
//Output:
Output 1:
Enter x1: 1
Enter x2: 2
x1= |x2= |x3= |fx1= |fx2= |fx3=
1 2 1.64207 2.5403 -1.41615 0.00259181
1.64207 2 1.64272 0.00259181 -1.41615 -2.20673e-005
1.64207 1.64272 1.64271 0.00259181 -2.20673e-005 -1.28211e-010
1.64207 1.64271 1.64271 0.00259181 -1.28211e-010 0
The root is= 1.64271
Output 2:
Enter x1: -10000.234
Enter x2: 100000.3452
x1= |x2= |x3= |fx1= |fx2= |fx3=
-10000.2 100000 1.37861 30004.8 -299997 1.05518
1.37861 100000 1.73033 1.05518 -299997 -0.349866
1.37861 1.73033 1.64275 1.05518 -0.349866 -0.000151465
1.37861 1.64275 1.64271 1.05518 -0.000151465 8.03515e-008
1.64271 1.64275 1.64271 8.03515e-008 -0.00015146 -2.75335e-014
The root is= 1.64271
QUESTION 7
Find a real root of the equation x4 – 11x + 8 = 0 accurate to four decimal places
using regular falsi method.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
#define f(x) (pow(x,4)- 11*x+8)
double FALSI(double a, double b, double c, double d,double e)
{
double x1, x2;
double mid_x;
x1 = 0;
x2 = 1; // need this!! in other case x2 is random value!
mid_x = 0.5; // initialize all variables before use
while( f(x1) * f(x2) > 0)
{
x1 = x2;
x2 = x1 + 1;
if (x2 > 1000)
return -1.0; // to prevent overflow
}
for(int i = 1; i < 100; i++)
{
mid_x = (x1*f(x2) -x2*f(x1))/(f(x2)-f(x1));
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
if(f(x1) * f(mid_x) < 0)
{
x2 = mid_x;
}
else
{
x1 = mid_x;
}
if(fabs(x1 - x2) < 0.0000001)
{
return (x1 + x2) / 2; // will return immediately
}
}
return mid_x; // return anything. Code can to bounce between two numbers
}
int main()
{
double h;
h = FALSI(1, 0, 0,-11,8);
cout << "The real root of the equation by using Falsi method is: " << setprecision(4)
<<fixed<< h << endl;
return 0;
}
//Output:
The real root of the equation by using Falsi method is: 0.7571 (4 decimal places)

More Related Content

What's hot

Ee 3122 numerical methods and statistics sessional credit
Ee 3122 numerical methods and statistics sessional  creditEe 3122 numerical methods and statistics sessional  credit
Ee 3122 numerical methods and statistics sessional creditRaihan Bin-Mofidul
 
Numerical Methods with Computer Programming
Numerical Methods with Computer ProgrammingNumerical Methods with Computer Programming
Numerical Methods with Computer ProgrammingUtsav Patel
 
Travel management
Travel managementTravel management
Travel management1Parimal2
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++Bharat Kalia
 
C++ Programming - 14th Study
C++ Programming - 14th StudyC++ Programming - 14th Study
C++ Programming - 14th StudyChris Ohk
 
Implementing Software Machines in C and Go
Implementing Software Machines in C and GoImplementing Software Machines in C and Go
Implementing Software Machines in C and GoEleanor McHugh
 
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Alex Penso Romero
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th StudyChris Ohk
 
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-ee01083101premrings
 
Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd StudyChris Ohk
 

What's hot (20)

C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
Ee 3122 numerical methods and statistics sessional credit
Ee 3122 numerical methods and statistics sessional  creditEe 3122 numerical methods and statistics sessional  credit
Ee 3122 numerical methods and statistics sessional credit
 
New presentation oop
New presentation oopNew presentation oop
New presentation oop
 
Numerical Methods with Computer Programming
Numerical Methods with Computer ProgrammingNumerical Methods with Computer Programming
Numerical Methods with Computer Programming
 
Travel management
Travel managementTravel management
Travel management
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
Oop1
Oop1Oop1
Oop1
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
C++ Programming - 14th Study
C++ Programming - 14th StudyC++ Programming - 14th Study
C++ Programming - 14th Study
 
Implementing Software Machines in C and Go
Implementing Software Machines in C and GoImplementing Software Machines in C and Go
Implementing Software Machines in C and Go
 
Go a crash course
Go   a crash courseGo   a crash course
Go a crash course
 
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
C++ file
C++ fileC++ file
C++ file
 
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
 
C++ programs
C++ programsC++ programs
C++ programs
 
Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd Study
 
Opp compile
Opp compileOpp compile
Opp compile
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 

Viewers also liked (8)

C++:Lab 2
 C++:Lab 2 C++:Lab 2
C++:Lab 2
 
C++
C++C++
C++
 
C++ lab -4
C++ lab -4C++ lab -4
C++ lab -4
 
BS M'sia
BS M'siaBS M'sia
BS M'sia
 
Mpam 3 basic steps
Mpam 3 basic stepsMpam 3 basic steps
Mpam 3 basic steps
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Defective Construction Work
Defective Construction WorkDefective Construction Work
Defective Construction Work
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 

Similar to C++ TUTORIAL 6

Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods kinan keshkeh
 
Modificacion del programa
Modificacion del programaModificacion del programa
Modificacion del programaMario José
 
Laporan teknik komputas Konversi Bilangan
Laporan teknik komputas Konversi BilanganLaporan teknik komputas Konversi Bilangan
Laporan teknik komputas Konversi BilanganMuhammad Hafizh Annur
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたAkira Maruoka
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1kkkseld
 
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.pptxssuser3cbb4c
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptkinan keshkeh
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptkinan keshkeh
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
 
Deep learning with C++ - an introduction to tiny-dnn
Deep learning with C++  - an introduction to tiny-dnnDeep learning with C++  - an introduction to tiny-dnn
Deep learning with C++ - an introduction to tiny-dnnTaiga Nomi
 

Similar to C++ TUTORIAL 6 (20)

Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods
 
Modificacion del programa
Modificacion del programaModificacion del programa
Modificacion del programa
 
Laporan teknik komputas Konversi Bilangan
Laporan teknik komputas Konversi BilanganLaporan teknik komputas Konversi Bilangan
Laporan teknik komputas Konversi Bilangan
 
C programs
C programsC programs
C programs
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみた
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
 
C++ file
C++ fileC++ file
C++ file
 
Include
IncludeInclude
Include
 
Seminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mmeSeminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mme
 
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
 
CppTutorial.ppt
CppTutorial.pptCppTutorial.ppt
CppTutorial.ppt
 
901131 examples
901131 examples901131 examples
901131 examples
 
week-2x
week-2xweek-2x
week-2x
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
oop Lecture 4
oop Lecture 4oop Lecture 4
oop Lecture 4
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
Deep learning with C++ - an introduction to tiny-dnn
Deep learning with C++  - an introduction to tiny-dnnDeep learning with C++  - an introduction to tiny-dnn
Deep learning with C++ - an introduction to tiny-dnn
 

Recently uploaded

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 

Recently uploaded (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

C++ TUTORIAL 6

  • 1. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6 QUESTION 1 Write a C++ program to find a root of the equation x3 – 4x – 8.95 = 0 accurate to three decimal places using the Bisection method in the interval (2, 3). #include<iostream> #include<cmath> #include<iomanip> using namespace std; void main() { double x1=2, x2=3, x3, fx1, fx2,fx3; cout << "x1=t" << "|x2=t"<<"|x3=t" << "|fx1=tt" << "|fx2=tt" << "|fx3=t" << endl; do { x3 = (x1 + x2)/2; fx1= pow(x1,3) - 4*x1 - 8.95; fx2= pow(x2,3) - 4*x2 - 8.95; fx3= pow(x3,3) - 4*x3 - 8.95; cout << x1 << "t" <<x2 << "t" <<x3 <<"t" << fx1 <<"tt" << fx2 << "tt" <<fx3 <<endl; if(fx1*fx3 <0) { x2=x3; } else { x1=x3;
  • 2. } } while ( fabs(x1 - x2) > 0.000001); cout << "nThe root is= " << setprecision(3) <<fixed << x3 <<endl; } // Output: x1= |x2= |x3= |fx1= |fx2= |fx3= 2 3 2.5 -8.95 6.05 -3.325 2.5 3 2.75 -3.325 6.05 0.846875 2.5 2.75 2.625 -3.325 0.846875 -1.36211 2.625 2.75 2.6875 -1.36211 0.846875 -0.289111 2.6875 2.75 2.71875 -0.289111 0.846875 0.270917 2.6875 2.71875 2.70313 -0.289111 0.270917 -0.0110771 2.70313 2.71875 2.71094 -0.0110771 0.270917 0.129423 2.70313 2.71094 2.70703 -0.0110771 0.129423 0.0590492 2.70313 2.70703 2.70508 -0.0110771 0.0590492 0.0239551 2.70313 2.70508 2.7041 -0.0110771 0.0239551 0.00643126 2.70313 2.7041 2.70361 -0.0110771 0.00643126 -0.00232486 2.70361 2.7041 2.70386 -0.00232486 0.00643126 0.00205271 2.70361 2.70386 2.70374 -0.00232486 0.00205271 -0.000136197 2.70374 2.70386 2.7038 -0.000136197 0.00205271 0.000958227 2.70374 2.7038 2.70377 -0.000136197 0.000958227 0.000411007 2.70374 2.70377 2.70375 -0.000136197 0.000411007 0.000137403 2.70374 2.70375 2.70374 -0.000136197 0.000137403 6.02383e-007 2.70374 2.70374 2.70374 -0.000136197 6.02383e-007 -6.77976e-005 2.70374 2.70374 2.70374 -6.77976e-005 6.02383e-007 -3.35976e-005 2.70374 2.70374 2.70374 -3.35976e-005 6.02383e-007 -1.64976e-005 The root is= 2.704 (3 decimal places)
  • 3. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6 QUESTION 2 Write a C++ program to find a root of the equation ex – 3x = 0 using the Bisection method. #include <iostream> #include<cmath> using namespace std; void main() { int n, count=1; double x0,x1,x2,fx0,fx1,fx2; cout <<"nSolution by BISECTION method n"; cout <<"nThe equation is: exp(x) - 3*x= 0nn"; cout << "Enter the number of iterations: "; cin >> n; /*Finding an Approximate ROOT of Given Equation, Having +ve Value*/ for(x2=1;;x2++) { fx2= exp(x2) - 3*x2; if (fx2 > 0) { break; } } /*Finding an Approximate ROOT of Given Equation, Having -ve Value*/ for(x1=x2-1; ; x2--) { fx1= exp(x1) - 3*x1;
  • 4. if( fx1 <0) { break; } } //Printing Result cout << "tt-----------------------------------------"; cout <<"ntt ITERATIONStt ROOTSn"; cout <<"tt-----------------------------------------"; for(;count<=n;count++) { x0=(x1+x2)/2; fx0 = exp(x0) - 3*x0; if( fx0 ==0) { cout << x0; } else if(fx0*fx1<0) { x2=x0; } else { x1=x0; fx1=fx0; }
  • 5. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6 cout <<"nttITERATION " << count; cout <<":tt" << x0; if(fabs((x1-x2)) < 0.000001) { cout <<"ntt---------------------------------"; cout <<"ntRoot = "<< x0; cout << "ntIterations = " << count <<endl; cout << "tt---------------------------------"; } } cout << "ntt----------------------------------------"; cout << "nttRoot = " << x0; cout << "nttIterations = " << count-1<<endl; cout << "tt----------------------------------------" << endl; } //Output: Solution by BISECTION method The equation is: exp(x) - 3*x= 0 Enter the number of iterations: 19 ----------------------------------------- ITERATIONS ROOTS ----------------------------------------- ITERATION 1: 1.5 ITERATION 2: 1.75 ITERATION 3: 1.625 ITERATION 4: 1.5625 ITERATION 5: 1.53125
  • 6. ITERATION 6: 1.51563 ITERATION 7: 1.50781 ITERATION 8: 1.51172 ITERATION 9: 1.51367 ITERATION 10: 1.5127 ITERATION 11: 1.51221 ITERATION 12: 1.51196 ITERATION 13: 1.51208 ITERATION 14: 1.51215 ITERATION 15: 1.51212 ITERATION 16: 1.51213 ITERATION 17: 1.51214 ITERATION 18: 1.51213 ITERATION 19: 1.51213 ---------------------------------------- Root = 1.51213 Iterations = 19 ---------------------------------------- QUESTION 3 Write a C++ function program to find the roof of f(x) = x6 – x – 1 = 0 accurate to within ξ = 0.001. Use the Bisection method. #include <iostream> #include<cmath> using namespace std; #define EPS 0.001 #define f(x) pow(x,6) - x - 1 //global variables int count=1,n;
  • 7. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6 void Bisect() { double x0,x1,x2,a,b,c; /*Finding an Approximate ROOT of Given Equation, having +ve Value*/ for(x2=1;;x2++) { c=f(x2); if (c > 0) { break; } } /*Finding an Approximate ROOT of Given Equation, Having -ve Value*/ for(x1=x2-1;;x2--) { b=f(x1); if(b <0) { break; } } //Printing Result cout << "tt-----------------------------------------"; cout <<"ntt ITERATIONStt ROOTSn"; cout <<"tt-----------------------------------------"; for( ; count<=n; count++)
  • 8. { x0=(x1+x2)/2; a =f(x0); if(a ==0) { x0; } else if(a*b<0) { x2=x0; } else { x1=x0; b=a; } cout <<"nttITERATION " << count; cout <<":tt" << x0; if(fabs((x1-x2)) < EPS) { cout <<"ntt----------------------------------"; cout <<"ntRoot = "<< x0; cout << "ntIterations = " << count <<endl; cout << "tt----------------------------------"; }
  • 9. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6 } cout << "ntt----------------------------------------"; cout << "nttRoot = " << x0; cout << "nttIterations = " << count-1<<endl; cout << "tt----------------------------------------" << endl; } void main() { cout <<"nSolution by BISECTION method n"; cout <<"nThe equation is: x^6- x -1= 0nn"; cout << "Enter the number of iterations: "; cin >> n; Bisect(); } //Output: Solution by BISECTION method The equation is: x^6- x -1= 0 Enter the number of iterations: 20 ----------------------------------------- ITERATIONS ROOTS ----------------------------------------- ITERATION 1: 1.5 ITERATION 2: 1.25 ITERATION 3: 1.125 ITERATION 4: 1.1875 ITERATION 5: 1.15625
  • 10. ITERATION 6: 1.14063 ITERATION 7: 1.13281 ITERATION 8: 1.13672 ITERATION 9: 1.13477 ITERATION 10: 1.13379 ---------------------------------- Root = 1.13379 Iterations = 10 ---------------------------------- ITERATION 11: 1.13428 ---------------------------------- Root = 1.13428 Iterations = 11 ---------------------------------- ITERATION 12: 1.13452 ---------------------------------- Root = 1.13452 Iterations = 12 ---------------------------------- ITERATION 13: 1.13464 ---------------------------------- Root = 1.13464 Iterations = 13 ---------------------------------- ITERATION 14: 1.1347 ---------------------------------- Root = 1.1347 Iterations = 14 ---------------------------------- ITERATION 15: 1.13474 ---------------------------------- Root = 1.13474 Iterations = 15 ---------------------------------- ITERATION 16: 1.13472 ---------------------------------- Root = 1.13472 Iterations = 16 ---------------------------------- ITERATION 17: 1.13473 ---------------------------------- Root = 1.13473 Iterations = 17 ---------------------------------- ITERATION 18: 1.13472 ---------------------------------- Root = 1.13472 Iterations = 18 ---------------------------------- ITERATION 19: 1.13473 ----------------------------------
  • 11. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6 Root = 1.13473 Iterations = 19 ---------------------------------- ITERATION 20: 1.13472 ---------------------------------- Root = 1.13472 Iterations = 20 ---------------------------------- ---------------------------------------- Root = 1.13472 Iterations = 20 ---------------------------------------- QUESTION 4 Write a C++ Function program to find the smallest positive root of the cubic equation ax3+bx2+cx+d=0 using Bisection method. You can take error tolerance of 10-6 #include <iostream> #include <cmath> using namespace std; #define f(x) (a*pow(x,3)+b*pow(x,2)+c*x+d) double Bisection(double a, double b, double c, double d) { double x1, x2; double mid_x; x1 = 0; x2 = 1; // We need this (in other case x2 is random value) mid_x = 0.5; // initialize all variables before use while( f(x1) * f(x2) > 0) { x1 = x2; x2 = x1 + 1; if (x2 > 1000) return -1.0; // to prevent overflow
  • 12. } for(int i = 1; i < 100; i++) { mid_x = (x1 + x2) / 2.0; if(f(x1) * f(mid_x) < 0) { x2 = mid_x; } else { x1 = mid_x; } if(fabs(x1 - x2) < 0.0000001) { return (x1 + x2) / 2; // will return immediately } } return mid_x; // return anything. Code can to bounce between two numbers } int main() { double a, b, c, d, h; ULANG: cout << "Enter the value of constants for the cubic equation: " << endl; cin >> a>> b>> c>> d; if(a==0)
  • 13. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6 { cout << "The value for constant a cannot equal to 0.Try again:n" << endl; goto ULANG; } h = Bisection(a, b, c, d); if (h >= 0) cout << "The smallest +ve root of the cubic equation using Bisection method is: " << h << endl; else cout << "The root is not found in range from 0 to 1000" << endl; return 0; } //Output: Enter the value of constants for the cubic equation: 0 1 6 -1 The value for constant a cannot equal to 0.Try again: Enter the value of constants for the cubic equation: 6 2 0 -1 The smallest +ve root of the cubic equation using Bisection method is: 0.45872 /******ALTERNATIVE METHOD FOR QUESTION 4******/ Done by FAIZ MATHS STUDENT, UM (trust me, he is so genius) #include <cmath> #include <iostream> #include <iomanip>
  • 14. using namespace std; void bisec(double a, double b); void choosepositive(int& x, int& y); void choosenegative(int& x, int& y); void zero(int z); double f(double x); double mid(double x,double y); double p,q,r,s; int main() { int z,l=0,n=1,m=0,o=-1,e,g,t=0,u=0,h; cout << "BISECTION METHOD [CUBIC EQUATION]nnEnter ax^3+bx^2+c+dn" << endl; cin >> p >> q >> r >> s; do { do { zero(m); cout << "nChoose:n" << "1. +ve rootn" << "2. -ve rootn" << endl; cin >> z; if (z==1) { choosepositive(l,n);e=n;g=n+1; if (f(l)*f(n) == 0 && f(n)!=0) {cout << "NO MORE REAL +VE ROOT" << endl;break;} if (n==100){cout<<"NO MORE REAL +VE ROOTn";break;}
  • 15. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6 bisec(l,n);break; } else if (z==2) { choosenegative(m,o);t=o;u=o-1; if (f(m)*f(o) == 0 && f(o)!=0) {cout << "NO MORE REAL -VE ROOT" << endl;break;} if (o==-100){cout<<"NO MORE REAL -VE ROOTn";break;} bisec(m,o);break; } } while (z!=1 || z!=2); cout << "Want Another root?n1. YESn2. NO (EXIT)n" << endl; cin >> h;l=e;n=g;m=t;o=u; } while (h==1); return 0; } void bisec(double a, double b) { double n=1,r; if (f(b)==0) {cout << "nThe root is " << b <<endl;} else { r=mid(a,b); cout << "n attbttxttb-rttf(x)" <<endl;
  • 16. do { cout << setprecision(0) << n << " "<< setprecision(6) << fixed << a << "t" << b << "t" << r << "t" << b-r << "t" << f(r) << endl; if (f(a)*f(r) < 0) b=r; else if (f(r)*f(b) < 0) a=r; else if (f(r)==0) break; r=mid(a,b);n++; } while (abs(b-r) > 0.000001 ); cout << "nThe root is " << r <<endl; } } void choosepositive(int& x, int& y) { while (f(x)*f(y) > 0 && y<100) {x=y;y++;} } void choosenegative(int& x, int& y) { while (f(x)*f(y) > 0 && y>-100) {x=y;y--;} } void zero(int z) {if (f(0)==0) {cout << "nThe root is 0"<< endl;}} double f(double x) { return (double) p*pow(x,3)+q*pow(x,2)+r*x+s;} double mid(double x,double y) {return (double)(x+y)/2;} //Output: BISECTION METHOD [CUBIC EQUATION] Enter ax^3+bx^2+c+d 1 2 3 4 Choose: 1. +ve root 2. -ve root
  • 17. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6 1 NO MORE REAL +VE ROOT Want Another root? 1. YES 2. NO (EXIT) 1 Choose: 1. +ve root 2. -ve root 2 n a b x b-r f(x) 1 -1.000000 -2.000000 -1.500000 -0.500000 0.625000 The root is -1.750000 Want Another root? 1. YES 2. NO (EXIT) 2 QUESTION 5 Write a C++ program to find a root of the function f (x) = ex – 3x2 to an accuracy of 5 digits in the interval 0.5 and 1.0 using the false position method #include<iostream> #include<cmath> using namespace std;
  • 18. void main() { double x1=0.5,x2=1.0, x3, fx1, fx2,fx3; cout << "x1=t" << "|x2=tt"<<"|x3=tt" << "|fx1=tt" << "|fx2=tt" << "|fx3=t" << endl; do { fx1= exp(x1) - 3*pow(x1,2); fx2= exp(x2) - 3*pow(x2,2); x3 = (x1*fx2 -x2*fx1)/(fx2-fx1); fx3= exp(x3) - 3*pow(x3,2); cout << x1 << "t" <<x2 << "t" <<x3 <<"t" << fx1 <<"tt" << fx2 << "tt" <<fx3 <<endl; if(fx1*fx3 <0) { x2=x3; } else { x1=x3; } } while ( fabs(x1 - x2) > 0.000001); cout << "nThe root is= " << x3 <<endl; }
  • 19. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6 //Output: x1= |x2= |x3= |fx1= |fx2= |fx3= 0.5 1 0.880672 0.898721 -0.281718 0.0857699 0.880672 1 0.908523 0.0857699 -0.281718 0.0044143 0.908523 1 0.909934 0.0044143 -0.281718 0.000218671 0.909934 1 0.910004 0.000218671 -0.281718 1.08115e-005 0.910004 1 0.910007 1.08115e-005 -0.281718 5.34494e-007 0.910007 1 0.910008 5.34494e-007 -0.281718 2.64238e-008 0.910008 1 0.910008 2.64238e-008 -0.281718 1.30632e-009 0.910008 1 0.910008 1.30632e-009 -0.281718 6.45808e-011 0.910008 1 0.910008 6.45808e-011 -0.281718 3.19211e-012 0.910008 1 0.910008 3.19211e-012 -0.281718 1.58096e-013 0.910008 1 0.910008 1.58096e-013 -0.281718 7.10543e-015 0.910008 1 0.910008 7.10543e-015 -0.281718 1.33227e-015 0.910008 1 0.910008 1.33227e-015 -0.281718 -4.44089e-016 The root is= 0.910008 QUESTION 6 Write a C++ function program to find a root of cos x – 3x + 5 = 0 using the false Position method. #include<iostream> #include<cmath> using namespace std; double f(double x) { double fx;
  • 20. fx= cos(x) - 3*x +5;; return fx; } void main() { double x1, x2,x3; cout << "Enter x1: "; cin >> x1; cout << "Enter x2: "; cin >> x2; cout << "x1=t" << "|x2=tt"<<"|x3=tt" << "|fx1=tt" << "|fx2=tt" << "|fx3=t" << endl; do { x3 = (x1*f(x2) -x2*f(x1))/(f(x2)-f(x1)); cout << x1 << "t" <<x2 << "t" <<x3 <<"t" << f(x1) <<"tt" << f(x2) << "tt" <<f(x3) <<endl; if(f(x1)*f(x3) <0) { x2=x3; } else { x1=x3; } } while ( fabs(x1 - x2) > 0.000001); //the epsilon value can be changed /***The function is like a straight line that regular falsi can extrapolate to a root even from far away, such as from (10, 100). But it is not a straight line, so it cannot converge with that much precision. To avoid this, we can try dropping our precision requirements by order of magnitude and it will work ****/
  • 21. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6 cout << "nThe root is= " << x3 <<endl; } //Output: Output 1: Enter x1: 1 Enter x2: 2 x1= |x2= |x3= |fx1= |fx2= |fx3= 1 2 1.64207 2.5403 -1.41615 0.00259181 1.64207 2 1.64272 0.00259181 -1.41615 -2.20673e-005 1.64207 1.64272 1.64271 0.00259181 -2.20673e-005 -1.28211e-010 1.64207 1.64271 1.64271 0.00259181 -1.28211e-010 0 The root is= 1.64271 Output 2: Enter x1: -10000.234 Enter x2: 100000.3452 x1= |x2= |x3= |fx1= |fx2= |fx3= -10000.2 100000 1.37861 30004.8 -299997 1.05518 1.37861 100000 1.73033 1.05518 -299997 -0.349866 1.37861 1.73033 1.64275 1.05518 -0.349866 -0.000151465 1.37861 1.64275 1.64271 1.05518 -0.000151465 8.03515e-008 1.64271 1.64275 1.64271 8.03515e-008 -0.00015146 -2.75335e-014 The root is= 1.64271
  • 22. QUESTION 7 Find a real root of the equation x4 – 11x + 8 = 0 accurate to four decimal places using regular falsi method. #include <iostream> #include <cmath> #include <iomanip> using namespace std; #define f(x) (pow(x,4)- 11*x+8) double FALSI(double a, double b, double c, double d,double e) { double x1, x2; double mid_x; x1 = 0; x2 = 1; // need this!! in other case x2 is random value! mid_x = 0.5; // initialize all variables before use while( f(x1) * f(x2) > 0) { x1 = x2; x2 = x1 + 1; if (x2 > 1000) return -1.0; // to prevent overflow } for(int i = 1; i < 100; i++) { mid_x = (x1*f(x2) -x2*f(x1))/(f(x2)-f(x1));
  • 23. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6 if(f(x1) * f(mid_x) < 0) { x2 = mid_x; } else { x1 = mid_x; } if(fabs(x1 - x2) < 0.0000001) { return (x1 + x2) / 2; // will return immediately } } return mid_x; // return anything. Code can to bounce between two numbers } int main() { double h; h = FALSI(1, 0, 0,-11,8); cout << "The real root of the equation by using Falsi method is: " << setprecision(4) <<fixed<< h << endl; return 0; } //Output: The real root of the equation by using Falsi method is: 0.7571 (4 decimal places)