I have got thsi program that is meant to help with a coursework project but i do not understand it fully, and consequently its not much help.
The initial section confuses me the most. Is this just simple definitions for double arrays?
I have annotated the lines in the code which esape me most. could any one possibly quote these lines with understandable annotations, or helpful suggestions?

#include <iostream>
#include <complex>

//*  This section below in green confuses me the most
using namespace std; 

#define EPS 0.001

complex<double> P(complex<double> a[], int n, complex<double> x);
complex<double> P1(complex<double> a[], int n, complex<double> x);
complex<double> P2(complex<double> a[], int n, complex<double> x);
complex<double> findRoot(complex<double> a[],int n);
complex<double> power(complex<double> x,int p);
complex<double> *roots(complex <double> a[], int n);
double magn(complex<double> x);

int main()
{
	int n;
	double tmp;
	cout<<"Enter range of polynom:";
	cin>>n;
	complex<double> *a=new complex<double>[n+1];
	for(int i=0;i<=n;i++)
	{
		cout<<"Enter a["<<n-i<<"]:"; //* I have never used out with brackets and speach marks before. what does this do?
		cin>>tmp;
		a[i]=complex<double>(tmp,0);
	}
	complex<double> *x;
	x=roots(a,n);
	for(i=0;i<n;i++)
		cout<<"x("<<i+1<<")="<<x[i]<<endl;
	system("pause");
	return 0;
}

complex<double> P(complex<double> a[], int n, complex<double> x)
{
	complex<double> sum=complex<double>(0,0);
	for(int i=0;i<=n;i++)
	{
		sum+=a[i]*power(x,n-i);
	}
	return sum;
}

complex<double> P1(complex<double> a[], int n, complex<double> x)
{
	complex<double> *b=new complex<double>[n];
	for(int i=0;i<n;i++)
	{
		b[i]=a[i]*complex<double>((n-i),0);
	}
	return P(b,n-1,x);
}

complex<double> P2(complex<double> a[], int n, complex<double> x)
{
	complex<double> *b=new complex<double>[n-1];
	for(int i=0;i<n-1;i++)
	{
		b[i]=a[i]*complex<double>((n-i),0)*complex<double>((n-i-1),0);
	}
	return P(b,n-2,x);
}

complex<double> power(complex<double> x,int p)
{
	complex<double> tmp=x;
	if(p==0)
		return complex<double>(1,0);

	for(int i=0;i<p-1;i++)
	{
		tmp*=x;
	}
	return tmp;
}

complex<double> findRoot(complex<double> a[],int n)
{
	complex<double> z0,z1,al,bet,gam,A,B,C,nk,nc,d,c1,c2;
	z0=complex<double>(0,0);
	int j;
	nk=complex<double>((n-1),0);
	nc=complex<double>(n,0);
	do
	{
		al=a[0];
		bet=complex<double>(0,0);
		gam=complex<double>(0,0);
		for(j=1;j<=n;j++)
		{
			gam=z0*gam+bet;
			bet=z0*bet+al;
			al=z0*al+a[j];
		}
		if(magn(al)<EPS) // is EPS just a low number used for comparisons?			break;
		A=-bet/al;
		B=A*A-complex<double>(2,0)*gam/al;
		C=sqrt(nk*(nc*B-A*A));
		c1=A+C;
		c2=A-C;
		if(magn(c1)<magn(c2))
			c1=c2;
		d=nc/c1;
		z0=z0+d;
	}while(magn(d)>EPS);
	return z0;
}

double magn(complex<double> x)
{
	return sqrt(x.real()*x.real()+x.imag()*x.imag());
}

complex<double> *roots(complex <double> a[], int n)
{
	complex<double> *xr=new complex<double>[n];
	complex<double> *b=new complex<double>[n+1];
	int i,j,k=n;
	for(j=0;j<=n;j++)
		b[j]=a[j];
	for(i=0;i<n;i++)
	{
		xr[i]=findRoot(b,k);
		for(j=1;j<k;j++)
		{
			b[j]+=b[j-1]*xr[i];
		}
		k--;
	}
	return xr;
}

Is it possible to describe this in a way an idiot could understand? I get the basics of the loops, but struggle more with the arrays and the maths.
Thanks. PabloD

>cout<<"Enter a["<<n-i<<"]:";
Gah! That's ugly. It could be formatted better to show the intention:

cout<<"Enter a["<< n - i <<"]:";

This basically prints "Enter a[[i]x[/i]]: ", where x is an index counting from the back of the array. If i is 0 and n is 5 then it would print "Enter a[5]: ". Likewise, if i is 2 and n is 5, it would print "Enter a[3]: ".

>if(magn(al)<EPS) // is EPS just a low number used for comparisons?
Yes, EPS is an unnecessary shortening of epsilon, which is used for floating-point comparisons that are "close enough" for all practical purposes.

Thanks. That helps a lot! I can see how and what the loop is doing now.
My only boggle now is the use of complex<double> throughout the program. Do you think you could explain what this does in the ways it is used. Does every use in the main program body refer to the definitions at the start?

complex<double> is a type, not a reference to an object or function. I don't quite understand your question enough to answer it properly.

I believe the query is about how functions are used.

In your code, complex<double> is a declaration of variable/return type, like int or char.

The lines in green at the top of your code are function prototypes.
(Please correct me on this explanation, anyone. Better to take out my ignorance now than let it fester.)

When the program is compiled, it progresses through the written code more or less from top to bottom. If you are using functions that you call from main, the code has to know what you mean by 'P1' (for example). Depending on your compiler, it may have the intelligence to search for the function while compiling; however, don't make that assumption.
A function prototype informs the program knows that any time it sees 'P1', there is something that has those input and return types that may be defined later on, and to not panic if it suddenly encounters 'P1' in the code.

It's also good coding practice, as it makes your code easier to read if you've got descriptive function titles and input arguments.

As to the meaning of complex<double> in the function prototypes, that's what you should be putting in and taking out of the function. When you call the function, it has to be set to a declared complex<double> variable, and have an array (a[]) of complex doubles as input.
Then, inside each function, you use the argument names as you would the variables you submitted as arguments.


Note that for arrays, you're not actually passing the data structure by value, but a pointer to the first object in the array. You could also pass

complex<double> findRoot(complex<double> *a,int n);

and get the same results. But that's another topic.

For the 'roots' function, that returns a pointer to a complex <double>, most likely an array because you can have multiple complex roots.

Does this help answer your question?

Now if you'd taken the time to write that program instead of getting someone else to write it for you you'd have figured all that (and more) out yourself...

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