在我的程序中帮助它没有得到正确的答案 [英] Help in my program it is not given the right answer

查看:47
本文介绍了在我的程序中帮助它没有得到正确的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助。

我的程序正在运行,但没有给我正确的答案。

它支持解决多项式的程序,使用Horners。

用户将输入x和系数的值。

这是我的代码:


#include< ; iostream>

#include< iomanip>

使用命名空间std;


const int n = 100;

void HornerP(char [],int,int,int);


int main()

{

char P [n];

int x,c;


cout<<"输入x的值:" ;;

cin>> x;

cout<<"多少系数? " ;;

cin>> c;


for(int i = 0; i< c; i ++)

{

cout<<""系数是:" ;;

cin>> P [i];

}

HornerP(P,n,x,c);


返回0;

}


void HornerP(char * a,int n,int x,int c)

{

int p = a [n];

for(int i = n-1; i> = n; i--)

{

p = x * pa [i];

}

cout<<"您输入"<<<< c-1<<<""度多项式,答案是:

"<<< p<< endl;

}

Hi, I need some help.
My program is runing but it is not given me the right answer.
The program it suppost to solve a polynomial, using Horners.
The user will enter the value for x and the coefficients.
This is my code:

#include <iostream>
#include <iomanip>
using namespace std;

const int n = 100;
void HornerP(char[], int, int, int);

int main()
{
char P[n];
int x, c;

cout<<"Enter the value for x: ";
cin>>x;
cout<<"How many coefficients? ";
cin>>c;

for(int i=0; i<c; i++)
{
cout<<"The coefficients are: ";
cin>>P[i];
}
HornerP(P, n, x, c);

return 0;
}

void HornerP(char *a, int n, int x, int c)
{
int p=a[n];
for (int i=n-1; i>=n; i--)
{
p=x*p-a[i];
}
cout<<"You enter "<<c-1<<" degree polynomial and the answer is:
"<<p<<endl;
}

推荐答案

sd****@gmail.com 写道:

我需要一些帮助。

[..]

for(int i = n-1; i> = n ; i--)
Hi, I need some help.
[..]
for (int i=n-1; i>=n; i--)



我相信如果你倒计时,你需要一个不同的条件。

不是''我> = n''...请检查''for''循环...

I believe if you''re counting down, you need a different condition.
Not ''i>=n''... Please check this ''for'' loop...


{

p = x * pa [i ];

}

cout<<"您输入"<<<< c-1<<""度多项式,答案是:

"<< p<< endl;

}
{
p=x*p-a[i];
}
cout<<"You enter "<<c-1<<" degree polynomial and the answer is:
"<<p<<endl;
}



V

-

请在通过电子邮件回复时删除资金''A'

我没有回复顶部-posted回复,请不要问

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask




< sd **** @ gmail.comwrote in message

news:11 ********************** @ p47g2000hsd.googlegr oups.com ...

<sd****@gmail.comwrote in message
news:11**********************@p47g2000hsd.googlegr oups.com...

我需要一些帮助。

我的程序正在运行,但没有给我正确答案。

它为解决多项式而设想的程序,使用Horners。

用户将输入x和系数的值。

这是我的代码:


#包括< iostream>

#include< iomanip>

使用命名空间std;


const int n = 100;

void HornerP(char [],int,int,int);


int main()

{

char P [n];

int x,c;


cout<<"输入x的值:" ;;

cin>> x;

cout<<"多少系数? " ;;

cin>> c;


for(int i = 0; i< c; i ++)

{

cout<<""系数是:" ;;

cin>> P [i];

}

HornerP(P,n,x,c);


返回0;

}


void HornerP(char * a,int n,int x,int c)

{

int p = a [n];
Hi, I need some help.
My program is runing but it is not given me the right answer.
The program it suppost to solve a polynomial, using Horners.
The user will enter the value for x and the coefficients.
This is my code:

#include <iostream>
#include <iomanip>
using namespace std;

const int n = 100;
void HornerP(char[], int, int, int);

int main()
{
char P[n];
int x, c;

cout<<"Enter the value for x: ";
cin>>x;
cout<<"How many coefficients? ";
cin>>c;

for(int i=0; i<c; i++)
{
cout<<"The coefficients are: ";
cin>>P[i];
}
HornerP(P, n, x, c);

return 0;
}

void HornerP(char *a, int n, int x, int c)
{
int p=a[n];



糟糕 - p中只有100个元素,从0到99.

你不能引用[100]


另外,你只是初始化了元素0到c,

所以你在[c + 1]到[99]

Oops - there are only 100 elements in p, going from 0 to 99.
You cannot reference a[100]

Also, you only initialized elements 0 through c,
so you have garbage in a[c+1] through a[99]


for(int i = n-1; i> = n; i--)

{

p = x * pa [i ];

}

cout<<"您输入"<<<< c-1<<""度多项式,答案是:

"<< p<< endl;

}
for (int i=n-1; i>=n; i--)
{
p=x*p-a[i];
}
cout<<"You enter "<<c-1<<" degree polynomial and the answer is:
"<<p<<endl;
}



-

Fred L. Kleinschmidt

波音助理技术研究员

航空稳定性和控制计算

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing

< br>

sd****@gmail.com 写道:

我需要一些帮助。

我的程序正在运行,但没有给我正确答案。

它为解决多项式而设想的程序,使用Horners。

用户将输入x和系数的值。

这是我的代码:


#包括< iostream>

#include< iomanip>

使用命名空间std;


const int n = 100;

void HornerP(char [],int,int,int);


int main()

{

char P [n];

int x,c;


cout<<"输入x的值: " ;;

cin>> x;

cout<<"多少系数? " ;;

cin>> c;


for(int i = 0; i< c; i ++)

{

cout<<""系数是:" ;;

cin>> P [i];

}

HornerP(P,n,x,c);


返回0;

}


void HornerP(char * a,int n,int x,int c)

{

int p = a [n];

for(int i = n-1; i> = n; i--)

{

p = x * pa [i];

}
Hi, I need some help.
My program is runing but it is not given me the right answer.
The program it suppost to solve a polynomial, using Horners.
The user will enter the value for x and the coefficients.
This is my code:

#include <iostream>
#include <iomanip>
using namespace std;

const int n = 100;
void HornerP(char[], int, int, int);

int main()
{
char P[n];
int x, c;

cout<<"Enter the value for x: ";
cin>>x;
cout<<"How many coefficients? ";
cin>>c;

for(int i=0; i<c; i++)
{
cout<<"The coefficients are: ";
cin>>P[i];
}
HornerP(P, n, x, c);

return 0;
}

void HornerP(char *a, int n, int x, int c)
{
int p=a[n];
for (int i=n-1; i>=n; i--)
{
p=x*p-a[i];
}



这里有很多问题。首先,您应该使用数组

of int作为系数而不是char数组。请注意,

乘以字符3,与

乘以3不同。


其次,n不是您拥有的系数数。 n是MAXIMUM

个系数。你应该循环c次,而不是n次。在

的事实上,这个函数甚至不需要n作为参数。


第三,你在Horner规则中的操作是错误的。它应该使用

加法,而不是减法。


第四,你的循环条件毫无意义。你从i = n-1开始,然后

然后循环,而i> = n。但是n-1已经失败了,所以你的

循环根本就不会执行。如果你把b初始化为0,然后循环遍历每个低于c的索引,生活对你来说会更简单。

这样做的常用习语是:

int p = 0;

for(size_t i = c; i - ;)

p = x * p + a [i];

You have a number of problems here. First, you should be using an array
of int for your coefficients instead of an array of char. Note that
multiplying by character ''3'', for example, is not the same as
multiplying by 3.

Second, n is not the number of coefficients you have. n is the MAXIMUM
number of coefficients. You should be looping c times, not n times. In
fact, this function doesn''t even need n as a parameter.

Third, your operation in Horner''s rule is wrong. It should use
addition, not subtraction.

Fourth, your loop conditions make no sense. You start with i=n-1, and
then loop while i>=n. But n-1 already fails that condition, so your
loop never executes at all. Life will be more simple for you if you
initialize p to 0, and then loop over every index less than c. The
common idiom for doing that is:
int p=0;
for (size_t i=c; i--;)
p=x*p+a[i];


cout<<"您输入"<<< c-1< ;<"度多项式,答案是:

"<< p<< endl;

}
cout<<"You enter "<<c-1<<" degree polynomial and the answer is:
"<<p<<endl;
}



-

Alan Johnson

--
Alan Johnson


这篇关于在我的程序中帮助它没有得到正确的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆