所以我的函数按递减顺序积分多项式。
例如,3X ^ 3-2x + 1 = [3 0 2 1]
当然有一些限制,但这只是为了练习。
除非输入0数组,否则我的积分将继续返回-1或不正确。
这带给我另一个问题,当输入前几个数字时,如果我输入一个非数字,它会卡在我的goto循环中。我知道我不应该使用这些,请告诉我如何不使用它。
这是我的代码:
#include <iostream>
#include "eval.h"
int main ()
{
using namespace std; // yes i know using std:: is better... but I got lazy
cout<<"This program takes the coefficients of a polinomial in decreasing \n "
"degree. For example, x^2-1 = [1 0 -1] \n";
// explains to the user what the function does, and how to use it
int columns;
cout<<"Enter The highest degree plus one \n "; // in other words, how many elements the array will have
cin>> columns;
cout<<"Enter Your Coefficients\n ";
float myMatrix[columns]; //This was a code for practice, so I decided to use for loops to get user input
for (int i = 1; i<columns+1; ++i)
{
cin>>myMatrix[i];
}
//Im well aware i could have used other things
float skeletonMatrix[columns+1];
for (int ii = 1; ii< columns+8;++ii) //for loop that injects user array into a skeleton matrix which is one element bigger beacuse of +C
{
skeletonMatrix[ii] = (myMatrix[ii]/(columns+1-ii));
}
cout<<"The Integral is"<<endl;
for (int iii = 1; iii<columns+1; ++iii)
{
if (skeletonMatrix[iii] == 0 )
{
continue;
}
cout<<skeletonMatrix[iii]<<"x^"<<columns+1-iii<<" + ";
}
cout<<"C"<<endl; // Obviously I have not added the +C element because I will do that at another point, for a different purpose
cout<<"Do you wish to Evaluate the Integral??? \n ";
cout<<"Y of yes, N for no";
char chYorN;
tryagain:
cin>>chYorN;
double dScalara;
double dScalarb;
double integral;
switch(chYorN)
{
case 'y':
{
cout<<"Enter where you want the integral \n to be evaluated"<<endl;
cout<<"e.g. from a to b would be a, then b"<<endl;
cin>>dScalara;
cin>>dScalarb;
integral = (eval(columns , dScalarb , myMatrix) - eval(columns , dScalara , myMatrix));
cout<<"The integral is"<< integral <<endl;
break;
}
case 'n':
{
cout<<"Thank you for using my program"<<endl;
break;
}
default: // At the matrix input, input a non number, and the program will go to this for some reason
{
cout<<"Try again Brah";
goto tryagain; // <-----im afraid it has to do with this, please show me how to replace this
}
}
return 0;
}
我的Eval.h如下:
#ifndef EVAL_H
#define EVAL_H
#include <math.h>
double eval(int columns, double dScalar, float myMatrix[])
{
double dSum;
for (int i=0; i<columns ; ++i)
{
myMatrix[columns-i]= myMatrix[columns-i]*(pow(dScalar,(double)i)); //evaluates a function at dScalar
}
for (int ii=1; ii<columns+1;++ii) // sums up the different parts
{
dSum+=myMatrix[ii];
}
return dSum;
}
#endif
如果有其他方式显示我的阵列,请告诉我。 我希望我可以连续显示它,而不是列。
也是一个二维数组。 我希望它能像这样出现:
1 0 2 2 0
0 1 2 3 0
0 0 0 0 1
答案 0 :(得分:2)
错误的一个可能原因可能是您的goto语句将控件置于变量声明之前。而不是:
tryagain:
cin>>chYorN;
double dScalara;
double dScalarb;
double integral;
switch(chYorN)
{
试试这个:
double dScalara;
double dScalarb;
double integral;
tryagain:
cin>>chYorN;
switch(chYorN)
{
另外,请注意数组索引从零开始。如果您有int a[3]
,则会将其存储为a[0], a[1], a[2]
而不是a[1], a[2], a[3]
。 (@Ed Heal有一个关于这个问题的答案,因为我正在打字。所以,我不会详细说明。)
希望这会有所帮助。
答案 1 :(得分:1)
您访问各种数组是不正确的。
例如这段代码:
float myMatrix[columns];
for (int i = 1; i<columns+1; ++i)
{
cin>>myMatrix[i];
}
应该是
float myMatrix[columns];
for (int i = 0; i<columns; ++i)
{
cin>>myMatrix[i];
}
由于数组的索引从0开始,因此最终索引将为columns-1
。
您在其他各种阵列中遇到类似的问题。
此错误将导致未定义的错误。