``````#include <iostream>

using namespace std;
double polynome3(double a0,double a1,double a2, double a3)
`
double y[21];
double z[21];

for (int k=0;k<21;k++)
{

y[0]=1;
y[1]=1;
y[2]=1;

/*  Bernoulli's algorithm */

y[k+3]=-(((a1*y[k+2])+(a2*y[k+1])+(a3*y[k]))/a0);

// cout<<y[10]/y[9]<<endl;
}
double alpha1=y[20]/y[19];
cout<< "The first solution is alpha1 =  " << alpha1<<endl;

double b0=a0;
double b1=a1+alpha1*b0;
double b2=-(a3/alpha1);
cout<<b0<<" " <<b1<<" "<<b2<<endl;

for(int j=0;j<21;j++)
{
z[0]=0;
z[1]=1;
z[j+2]=-((b1*z[j+1]+b2*z[j])/b0);
}

double alpha2=(z[20]/z[19]);
cout<<" The second solution is alpha2=  " <<alpha2<<endl;

double c0=b0;
double c1=-(b2/alpha2);
double alpha3=-(c1/c0);
cout<<c0<<" " <<c1<<endl;
cout<<" The third solution will be alpha3 = "<< alpha3<< "\n"<<endl;

return 0;

}

int main()
{
cout<<"Enter your coefficient : "<<endl;
double x,y,z,t;
cin>>x;
cin>>y;
cin>>z;
cin>>t;
polynome3(x,y,z,t);
/* methode de Bernoulli */

return 0;
}

# Heading Here #
``````

And?

Some of the things you will see are that you have looped over the end of your arrays. e.g. line 18 and 36. You have written:

``````double y[21];
// stuff...
for(int k=0;k<21;k++)
{
y[k+3]=-((a1*y[k+2])+(a2*y[k+1])+(a3*y[k]))/a0);
}
``````

Note that k+3 means that k goes to 24. That will cause memory …

## All 7 Replies

It is not nice to post code with some problem, and then not indicate what/where the problem is. What IS your problem? P.S. NathanOliver was being polite...

``````Hi ,
The code is not a problem it solves it!
I introduced a C++ code using an algorithm (called Bernoulli's formula) and it solves any equation of degree three; just enter the coefficients and it's done!
of course it's available for the real case (can not find the complex solutions)
``````

If this is code that you are submitting for people to see/use than I have a couple pointer for you. First this should have been posted as a code snippet. Secondly you should make sure you code is formated/indented properly. Here a couple examples for you. Personally I prefer the first method.

``````line of code;
more code;
if(something)
{
more code;
even more code;
if(another condition)
{
even more code here;
}
some more code here;
}
last line of code;

// or

line of code;
more code;
if(something){
more code;
even more code;
if(another condition){
even more code here;
}
some more code here;
}
last line of code;
``````

Lastly your code should use meaningful varaible names. It is not readlily appernt based on your variables names what the variables hold. You shouldn't have to understand the code to know what the variables are. Making code maintanible is very important as it helps to find bugs and it allows other people to work on it.

Yes, i understand, but it's not a programm for games or a quiz ,it's to solve equations there's no variable names there are coefficients and degrees..

You can still give your variables sensible names that help the user/programmer to understand your intention. Also, I agree with NathanOliver about preferring the first indentation method. In any case, expressing a 3-dimentional array x, y, and z are reasonable variable names in the mathematical sense, but what is t? Make your intention clear. This will help you as well in the future when you have to re-visit this code and wonder what the heck you meant by some of those terms!

Some of the things you will see are that you have looped over the end of your arrays. e.g. line 18 and 36. You have written:

``````double y[21];
// stuff...
for(int k=0;k<21;k++)
{
y[k+3]=-((a1*y[k+2])+(a2*y[k+1])+(a3*y[k]))/a0);
}
``````

Note that k+3 means that k goes to 24. That will cause memory corruption. Additionally, since you only use up to y[20] in the output, why leave the loop all the way to 20, why not stop at k==17.

(note that there is the same error with z as well.

If you are going to have another look at this code, you can write it without needing to use an array beyond size 3.

Finally, as you may know, the algorithm as given is highly numerically unstable. There are a number of re-normalising operations that can be done after each step to improve things.

commented: Deep insights. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.