-6

so I am trying to write code for my programming class in c++. We are currently attempting to use the Newton-Raphson method to find the 3 roots of a third degree polynomial. I am getting an error that says "expected unqualified-id". That is all that it says. Under that the computer places a carrot under a bracket. I have been struggling with this error for a while so any advice would be amazing! I will post the code below.

#include <iostream>
#include <cmath>

using namespace std;

double nrmethod(int r1, double fr1, double dr1fr1, int imax, double es);

int main ()

{
    int sxx=10, sxy=14, sxz=25, syy=7, syz=15, szz=16;/* declaring integers to calculate the coefficients of the algebraic equation. this algebraic equations roots are the values we are looking for*/

    int I;
    int II;
    int III;/*delcaring the coefficents of the algebraic equation*/

    I=sxx+syy+szz;
    II=(sxx*syy)+(sxx*szz)+(syy*szz)-(pow(sxy,2))-(pow(sxz,2))-(pow(syz,2));
    III=(sxx*syy*szz)-(sxx*pow(syz,2))-(syy*pow(sxz,2))-(szz*pow(sxy,2))+(2*sxy*sxz*syz);/*solving for the coefficents of the algebraic equation*/
    cout << I << ", " << II << ", " << III << endl; /*displaying the coefficients of the algebraic equation*/

    int imax=100;
    double es=0.01;
    double dr1fr1;
    double fr1;
    int r1;


        cout <<"enter guess for the root";

        cin >> r1;

    double r = nrmethod(r1, fr1, dr1fr1, imax, es);

    cout << r << std::endl;

    return 0;
}

double nrmethod(int r1, double fr1, double dr1fr1, int imax, double es);{

double fr1=pow(r1,3)-I*pow(r1,2)+(II*r1-III);
double dr1fr1= 3*pow(r1,2)-(2*I*r1)+II;

do
    int k=1;

    if(k<= imax){
    k++;
    r2=r1-(fr1/dr1fr1);
    if r2 !=0 then int er=(abs(r2-r1)/abs(r2))*100;
        if(er<es){end do}
        else {return r2}
}
cout << r2;

return 0;
}
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
Noah Brous
  • 11
  • 1
  • 1
  • 1
    Please [read why using std is bad](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Also having meaningful variable names will help alot – Ed Heal Feb 05 '17 at 21:48
  • 2
    Do you really think those variable names are remotely sensible? –  Feb 05 '17 at 21:50
  • On what line is the error? – DeiDei Feb 05 '17 at 21:50
  • 3
    You didn't feel it important to mention _where_ the error occurred? Go to the line where the error is and read it carefully. You should see the problem. – Greg Kikola Feb 05 '17 at 21:51
  • Well, first of all you have a stray semi-colon at your definition of `nrmethod`. Second of all you have a stray `do` in that same function. – DeiDei Feb 05 '17 at 21:53
  • 2
    Consider using simple multiplications, like `x * x`, instead of all those calls to `pow(x, 2)`, expecially for integer variables. – Bob__ Feb 05 '17 at 21:55
  • 2
    `computer places a carrot under a bracket` - it gives you a healthy snack to encourage you to write more readable code ;) – Anty Feb 05 '17 at 21:58
  • Is it pseudo-code, what you have written inside `nrmethod()`? – Bob__ Feb 05 '17 at 22:01
  • 1
    _"“expected unqualified-id” Thats all it says"_ That is an alternative fact. – Lightness Races in Orbit Feb 05 '17 at 22:06
  • @NeilButterworth: To be fair, in this context, they're not awful. Sum of XX, Sum of XY, and so forth. – Lightness Races in Orbit Feb 05 '17 at 22:08

1 Answers1

5

The compiler tells you what the error is, and where it is:

 a.cpp:40:73: error: expected unqualified-id before '{' token                 
 double nrmethod(int r1, double fr1, double dr1fr1, int imax, double es);{  
                                                                        ^

In this case, an extraneous semicolon. You are going to have to learn to pay attention to what the compiler is telling you if you want to have any hope of being a programmer.

  • The stray semi-colon likely came from copy and paste of the method definition above main ... – Lamar Latrell Feb 05 '17 at 21:54
  • After fixing that - there are a host of more compiler warnings. In that function `fr1` is shadowed. `I` is not in scope .... The code is a mess and should be thrown away – Ed Heal Feb 05 '17 at 22:00
  • @Ed I agree completely with that sentiment; I was simply trying to address the OP's headline question. –  Feb 05 '17 at 22:14
  • luckily for me I DO NOT plan on being a programmer. This is just required course work. That semi-colon was the issue. Thank you very much. also the code did work out in the end with some tweaking. – Noah Brous Feb 05 '17 at 22:59