Hi all C++ expert, I'm new in this field. I've written a program but I need to write it using class. I'm not able to understand where I'll define
a = new double[numElement];
etc.... and
delete [] a;
etc... I'm confused where and why I'll define 'new' & 'delete' using class, constructor etc... I'm not confident about 'vector' also. So please help me how to solve it I'll really thankful to you.
#include<iostream>
#include<cmath>
/*---------------------------------------
-----------------------------------------
This is the Fibonacci Search Algorithm which gives optimal interval reduction for a given number of function evaluations.
This program Operation On A Given Function :
----------------------------------------
The Function is ' F(x)=e^(-x)+x^2 '";
----------------------------------------
----------------------------------------
Inputs of this program are :
******************************
(i)(a1 & b1) = Initial Interval
(ii) r = Required interval reduction
(iii) m = Number of iteration
(iv) z = The perturbation applied to rsolve the ambiguity over the final interval
Output of this program :
*************************
The program shows the values of each iteration to achieve the given prescribed interval reduction
*/
double R_Fibo(int m);
void Iteration(double *a,double *b,double *c,double *d,double *Fc,double *Fd,double z,int m);
double F(double x);
int main(int argc, char* argv[]) // put the othe arguments for main (int argc, char* argv[])
{
double z,r; //r=required interval reduction & z=small perturbation for final interval
double *a,*b,*c,*d,*Fc,*Fd,I;
int m,numElement =100; //m=number of iteration
a = new double[numElement];
b = new double[numElement];
c = new double[numElement];
d = new double[numElement];
Fc= new double[numElement];
Fd= new double[numElement];
//Result Of A Fibonacci_Search Algorithm Operation On A Given Function :
std::cout <<"\nThe Function is ' F(x)=e^(-x)+x^2 '";
std::cout <<"\n";
//User Specify The Initial Interval :
std::cout << "\nGive The Initian Point :" <<"\na1 = ";
std::cin >> a[1];
std::cout << "\nGive The Final Point :" <<"\nb1 = ";
std::cin >> b[1];
std::cout <<"\n";
//Distance Between The Starting Interval :
I=(b[1]-a[1]);
std::cout << "As You Have Entered" << "\na1 = " <<a[1] <<"\tAnd" <<"\nb1 = " << b[1];
std::cout << "\nInterval Reduction At The Initial Iteration :"<< "\nI(1) = " << I;
std::cout <<"\n";
std::cin.get();
//Required interval reduction (r):
std::cout <<"\nNeeded The Prescribe Interval Reduction :" <<"\nr = ";
std::cin >> r;
std::cout <<"\n";
//Give the Number of iteration (m):
//By Fibonacci Series
std::cout <<"\nAccording To The Interval Reduction";
std::cout <<"\nHow Many Intervals You Need :" << "\nm = ";
std::cin >> m;
std::cout <<"\n";
//The perturbation applied to rsolve the ambiguity over the final interval (z)
std::cout <<"\nFor Accuracy At The Final Interval, Need To Take A Small Perturbation Say, z :";
std::cout <<"\nEnter The Value Of z = ";
std::cin >> z;
std::cout <<"\n";
//To Calculate The Ratio of two consecutive Fibo_Num (F(m-1)/Fm) :
//Function (F(m-1)/Fm) Declaration :
std::cout <<"\nBefore The Start Of Interval Reduction";
std::cout << "\nThe Ratio Of Two Consecutive Fibonacci_Number :";
std::cout <<"\nRF = " << R_Fibo(m); //call the R_Fibo()
std::cout <<"\n";
std::cin.get();
//We Introduce Two Another Points For Getting Two New Interval Of Uncertainty
//First Point 'c1' And Second Point 'd1' :
c[1]=b[1]-(R_Fibo(m)*I);
std::cout << "\nPlaced A Point c1 Within The Initial Interval :"<< c[1];
d[1]=a[1]+(R_Fibo(m)*I);
std::cout <<"\nPlaced Another Point d1 Within The Initial Interval :"<<d[1] << "\n";
std::cout <<"\n";
std::cin.get();
//Showing The Starting Reduction :
//----------------
//----------------
std::cout <<"At The 1 Iteration :\n";
std::cout <<"The Value Of a1=" << a[1] << "\n";
std::cout <<"The Value Of b1=" << b[1] << "\n";
std::cout <<"The Value Of c1=" << c[1] << "\n";
std::cout <<"The Value Of d1=" << d[1] ;
//--------------------
//Function 'Fc1' at point 'c1' And Function 'Fd1' at point 'd1':
Fc[1]=F(c[1]);
std::cout << "\nAt c1 The Function Value Fc1=" << Fc[1];
Fd[1]=F(d[1]);
std::cout << "\nAt d1 The Function Value Fd1=" << Fd[1];
std::cout <<"\n";
//---------------------
//---------------------
// The Iteration Start from Here
// now call the function Iteration()
Iteration(a,b,c,d,Fc,Fd,z,m);
//Give The Prescribe Interval Reduction :
std::cout <<"\nNeeded The Prescribe Interval Reduction :" <<"\nr = " << r;
std::cout <<"\n";
//Calculate The Required Interval Reduction
double In=b[m]-a[m];
std::cout <<"\nThe Interval Reduction At The Final Iteration :" <<"\nI(n)= " << In;
std::cout<<"\n";
delete [] a;
delete [] b;
delete [] c;
delete [] d;
delete [] Fc;
delete [] Fd;
std::cin.get();
return 0;
}
double F(double x)
{
return exp(-x) + x*x;
}
//Ratio of two successive terms of Fibonacci Sequence is obtained using Binet's Formula
//Function (F(m-1)/Fm) Defination :
double R_Fibo(int m)
{
double n1=1.0-(sqrt (5.0)); // *** casting is not c++ compliant. also, use 5.0 instead of 5
double n2=1.0+(sqrt(5.0));
double s=(n1/n2);
double s1=(sqrt(5.0)-1.0)/2.0; // *** do not mix integer and double computation. use 2.0 instead of 2 etc.
double RF=s1*((1.0-pow(s,m))/(1.0-pow(s,(m+1))));
return RF;
}
// pass values into Iteratio() function
void Iteration(double *a, double *b, double *c, double *d, double *Fc, double *Fd,double z,int m)
{
for(int k=1;k<=(m-2);k++)
{
std::cin.get();
std::cout <<"\n";
std::cout <<"At The "<<k+1<<" Iteration :\n";
if(Fc[k]<Fd[k])
{
a[k+1]=a[k];
std::cout <<"The Value Of a" << k+1 << "=" << a[k+1] << "\n";
b[k+1]=d[k];
std::cout <<"The Value Of b" << k+1 << "=" << b[k+1] << "\n";
c[k+1]=b[k+1]-R_Fibo(m-k)*(b[k+1]-a[k+1]);
if(k==(m-2)) //condition for getting the value of point c
{
c[k+1]=c[k+1]+z; //The perturbation is applied
std::cout <<"The Value Of c" << k+1 << "=" << c[k+1] << "\n";
}
else
{
std::cout <<"The Value Of c" << k+1 << "=" << c[k+1] << "\n";
}
d[k+1]=c[k];
std::cout <<"The Value Of d" << k+1 << "=" << d[k+1] << "\n";
Fc[k+1]=F(c[k+1]);
std::cout <<"The Value Of Fc" << k+1 << "=" << Fc[k+1] << "\n";
Fd[k+1]=Fc[k];
std::cout <<"The Value Of Fd" << k+1 << "=" << Fd[k+1] << "\n";
}
else
{
a[k+1]=c[k];
std::cout <<"The Value Of a" << k+1 << "=" << a[k+1] << "\n";
b[k+1]=b[k];
std::cout <<"The Value Of b" << k+1 << "=" << b[k+1] << "\n";
c[k+1]=d[k];
std::cout <<"The Value Of c" << k+1 << "=" << c[k+1] << "\n";
d[k+1]=a[k+1]+R_Fibo(m-k)*(b[k+1]-a[k+1]);
if(k==(m-2)) //condition for getting the value of point d
{
d[k+1]=d[k+1]+z; //The perturbation is applied
std::cout <<"The Value Of d" << k+1 << "=" << d[k+1] << "\n";
}
else
{
std::cout <<"The Value Of d" << k+1 << "=" << d[k+1] << "\n";
}
Fc[k+1]=Fd[k];
std::cout <<"The Value Of Fc" << k+1 << "=" << Fc[k+1] << "\n";
Fd[k+1]=F(d[k+1]);
std::cout <<"The Value Of Fd" << k+1 << "=" << Fd[k+1] << "\n";
}
}
//Another 'if' Condition Start outside The 'for' Loop
if(Fc[m-1]<Fd[m-1])
{
std::cout <<"\n";
std::cout <<"\nAt Final Iteration :\n";
a[m]=a[m-1];
b[m]=d[m-1];
std::cout <<"\n";
std::cout <<"The Value Of a" << m << "= "<< a[m] << "\n";
std::cout <<"The Value Of b" << m << "= "<< b[m] << "\n";
}
else
{
a[m]=c[m-1];
b[m]=b[m-1];
std::cout <<"\n";
std::cout <<"The Value Of a" << m << "= "<< a[m] << "\n";
std::cout <<"The Value Of b" << m << "= "<< b[m] << "\n";
}
}
Thanks