-6 c++
所以我正在尝试用 C++ 为我的编程课编写代码。我们目前正在尝试使用 Newton-Raphson 方法来求三次多项式的 3 个根。我收到一条错误消息,显示“预期的不合格 ID”。这就是它所说的一切。计算机在其下方的支架下放置了一根胡萝卜。我已经为这个错误苦苦挣扎了一段时间,所以任何建议都会很棒!我将在下面发布代码。
#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;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
编译器会告诉您错误是什么以及错误在哪里:
a.cpp:40:73: error: expected unqualified-id before '{' token
double nrmethod(int r1, double fr1, double dr1fr1, int imax, double es);{
^
Run Code Online (Sandbox Code Playgroud)
在本例中,是一个无关的分号。如果你想成为一名程序员,你就必须学会注意编译器告诉你的内容。
| 归档时间: |
|
| 查看次数: |
7987 次 |
| 最近记录: |