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

Wherever you need to set aside memory and you have no idea how much memory you'll need at compile time, you'll probably need a "new" command. Whenever you have a "new" command, you'll need a "delete" command somewhere. A decent rule of thumb, but definitely only a rule of thumb (lots of exceptions) is: "new" goes in the constructor, "delete" goes in the destructor.

It's impossible to tell you where to put these commands in your class since you have no class yet.

Regarding vector, the whole idea of a vector is to let C++ do all the memory management/array resizing for you. Thus you probably won't use the word "new" or "delete" when dealing with a vector.

Wherever you need to set aside memory and you have no idea how much memory you'll need at compile time, you'll probably need a "new" command. Whenever you have a "new" command, you'll need a "delete" command somewhere. A decent rule of thumb, but definitely only a rule of thumb (lots of exceptions) is: "new" goes in the constructor, "delete" goes in the destructor.

It's impossible to tell you where to put these commands in your class since you have no class yet.

Regarding vector, the whole idea of a vector is to let C++ do all the memory management/array resizing for you. Thus you probably won't use the word "new" or "delete" when dealing with a vector.

I've used constructor & destructor VernonDozier.But the variables are also declared in main() and etc.. As you said

It's impossible to tell you where to put these commands in your class since you have no class yet.

I'm till confused to find exact position to utilize 'new' & 'delete'.So, I'm trying to solve it using with both structure & class. But if VernonDozier or those who are visiting on this web page have any suggestion please discuss with me.

Thanks

Well Vernon has given quite a good explanation but if you still didn't get it its ok.

As the name goes "new" is to create something new and "delete" is to delete something which is already created.

Here in C++ if we don't exactly know how much memory is needed for something while writing the source code then we can make the memory allocation dynamic and hence use new to allocate memory.Usage of delete is not compulsory i,e it not being used doesn't create an error but in the long time can create a much more dreadful failures. delete is just a way to return the memory so that it can be reused.

As every call of new allocates memory in the heap area its usage within a function cannot be regarded as beneficial. If it were to locate memory on the runtime stack then we could say that its call within a function is beneficial because as soon as the function returns the memory will be returned.

I'm till confused to find exact position to utilize 'new' & 'delete'

This statement itself is wrong because the concept of dynamic memory was introduced to give user the freedom to allocate memory wherever he wants. By limiting the positions of usage of new we are going against this concept of dynamic memory.

After all this where exactly should we use new and delete can be answered as below:
Its up to the programmer to decide the exact locations of these two calls new and delete. The only thing which we need to keep in mind is that the freedom given to us should be used effectively within the bounds and the favours done to us by the memory manager by providing us with memory(dynamic) whenever required,should be properly returned with care.

So its up to you to decide where to use them.Only thing you need to know is that every bit of memory allocated is to be freed.There shouldn't be any leaks.

This article has been dead for over six months. Start a new discussion instead.