P: n/a

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=n1; i>=n; i)
{
p=x*pa[i];
}
cout<<"You enter "<<c1<<" degree polynomial and the answer is:
"<<p<<endl;
}  
Share this Question
P: n/a
 sd****@gmail.com wrote:
Hi, I need some help.
[..]
for (int i=n1; i>=n; i)
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<<"You enter "<<c1<<" degree polynomial and the answer is:
"<<p<<endl;
}
V

Please remove capital 'A's when replying by email
I do not respond to topposted replies, please don't ask  
P: n/a

<sd****@gmail.comwrote in message
news:11**********************@p47g2000hsd.googlegr oups.com...
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];
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=n1; i>=n; i)
{
p=x*pa[i];
}
cout<<"You enter "<<c1<<" degree polynomial and the answer is:
"<<p<<endl;
}

Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing  
P: n/a
 sd****@gmail.com wrote:
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=n1; i>=n; i)
{
p=x*pa[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=n1, and
then loop while i>=n. But n1 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<<"You enter "<<c1<<" degree polynomial and the answer is:
"<<p<<endl;
}

Alan Johnson  
P: n/a

On Jun 8, 5:44 pm, Alan Johnson <a...@yahoo.comwrote:
sdl...@gmail.com wrote:
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=n1; i>=n; i)
{
p=x*pa[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=n1, and
then loop while i>=n. But n1 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<<"You enter "<<c1<<" degree polynomial and the answer is: "<<p<<endl;
}

Alan Johnson Hide quoted text 
 Show quoted text 
Thanks for all your help,
but still it is not getting the right answer.
Here is what I have:
#include <iostream>
#include <iomanip>
using namespace std;
const int n = 100;
void HornerP(int [], int, int);
int main()
{
int 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, x, c);
return 0;
}
void HornerP(int *a, int x, int c)
{
int p=0;
for (size_t i=c; i;)
{
p=x*p+a[i];
}
cout<<"You enter "<<c1<<" degree polynomial and the answer
is: "<<p<<endl;
}  
P: n/a
 sd****@gmail.com wrote:
On Jun 8, 5:44 pm, Alan Johnson <a...@yahoo.comwrote:
>sdl...@gmail.com wrote:
>>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=n1; i>=n; i) { p=x*pa[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=n1, and then loop while i>=n. But n1 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<<"You enter "<<c1<<" degree polynomial and the answer is: "<<p<<endl; }
 Alan Johnson Hide quoted text 
 Show quoted text 
Thanks for all your help,
but still it is not getting the right answer.
Here is what I have:
#include <iostream>
#include <iomanip>
using namespace std;
const int n = 100;
void HornerP(int [], int, int);
int main()
{
int 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, x, c);
return 0;
}
void HornerP(int *a, int x, int c)
{
int p=0;
for (size_t i=c; i;)
{
p=x*p+a[i];
}
cout<<"You enter "<<c1<<" degree polynomial and the answer
is: "<<p<<endl;
}
How is it not working? Every example I try with your code is giving me
the correct answer.

Alan Johnson  
P: n/a

On Jun 9, 4:46 am, Alan Johnson <a...@yahoo.comwrote:
sdl...@gmail.com wrote:
On Jun 8, 5:44 pm, Alan Johnson <a...@yahoo.comwrote:
sdl...@gmail.com wrote: 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=n1; i>=n; i) { p=x*pa[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=n1, and
then loop while i>=n. But n1 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<<"You enter "<<c1<<" degree polynomial and the answer is: "<<p<<endl;
}

Alan Johnson Hide quoted text 
 Show quoted text 
Thanks for all your help,
but still it is not getting the right answer.
Here is what I have:
#include <iostream>
#include <iomanip>
using namespace std;
const int n = 100;
void HornerP(int [], int, int);
int main()
{
int 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, x, c);
return 0;
}
void HornerP(int *a, int x, int c)
{
int p=0;
for (size_t i=c; i;)
{
p=x*p+a[i];
}
cout<<"You enter "<<c1<<" degree polynomial and the answer
is: "<<p<<endl;
}
How is it not working? Every example I try with your code is giving me
the correct answer.

Alan Johnson Hide quoted text 
 Show quoted text 
How come, I try different examples and it is not working.
I try x=3, cofficients = 3, the coefficients are 3, 4, 5.
and it suppost to give me 25 but gives me 60 instead.  
P: n/a
 sd****@gmail.com wrote:
On Jun 9, 4:46 am, Alan Johnson <a...@yahoo.comwrote:
>sdl...@gmail.com wrote:
>>On Jun 8, 5:44 pm, Alan Johnson <a...@yahoo.comwrote: sdl...@gmail.com wrote: 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=n1; i>=n; i) { p=x*pa[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=n1, and then loop while i>=n. But n1 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<<"You enter "<<c1<<" degree polynomial and the answer is: "<<p<<endl; }  Alan Johnson Hide quoted text   Show quoted text  Thanks for all your help, but still it is not getting the right answer. Here is what I have: #include <iostream> #include <iomanip> using namespace std; const int n = 100; void HornerP(int [], int, int); int main() { int 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, x, c); return 0; } void HornerP(int *a, int x, int c) { int p=0; for (size_t i=c; i;) { p=x*p+a[i]; } cout<<"You enter "<<c1<<" degree polynomial and the answer is: "<<p<<endl; }
How is it not working? Every example I try with your code is giving me the correct answer.
 Alan Johnson Hide quoted text 
 Show quoted text 
How come, I try different examples and it is not working.
I try x=3, cofficients = 3, the coefficients are 3, 4, 5.
and it suppost to give me 25 but gives me 60 instead.
As far as I can tell, 60 is the correct output for that input.
3*3^0 + 4*3^1 + 5*3^2 = 3 + 12 + 45 = 60

Alan Johnson  
P: n/a

<sd****@gmail.comwrote in message...
TRIM!
On Jun 9, 4:46 am, Alan Johnson <a...@yahoo.comwrote:
How is it not working? Every example I try with your code is giving me
the correct answer.
Alan Johnson
How come, I try different examples and it is not working.
I try x=3, cofficients = 3, the coefficients are 3, 4, 5.
and it suppost to give me 25 but gives me 60 instead.
int a[ 10 ] = {3, 4, 5, 0, 0, 0, 0, 0, 0};
int x( 3 );
int c( 3 );
int p( 0 );
for( size_t i( c ); i; ){ p = x * p + a[ i ]; }
//  in the loop
p = 3 * 0 + 5 [ p == 5 ] // i == 2
p = 3 * 5 + 4 [ p == 19 ] // i == 1
p = 3 * 19 + 3 [ p == 60 ] // i == 0
// 
So, '60' is the correct answer for the algo given!
If you change the ordering:
for( size_t i( c ); i; ){ p = x * ( p + a[ i ] ); }
p == 180
for( size_t i( 0 ); i < size_t( c ); ++i){ p = x * ( p + a[ i ] ); }
p == 132
for( size_t i( 0 ); i < size_t( c ); ++i){ p = x * p + a[ i ]; }
p == 44
Show your math that gives '25'.

Bob R
POVrookie  
P: n/a

sorry I meant x = 2, and is suppose to go like this
3*2^2 + 4*2^1 + 5*2^0 = 12 + 8 + 5 = 25  
P: n/a

<sd****@gmail.comwrote in message...
sorry I meant x = 2, and is suppose to go like this
3*2^2 + 4*2^1 + 5*2^0 = 12 + 8 + 5 = 25
{
int a[ 10 ] = {3, 4, 5, 0, 0, 0, 0, 0, 0};
int x( 2 );
int c( 3 );
int p( 0 );
for( size_t i( 0 ); i < size_t( c ); ++i){
cout<<" for(i="<<i<<") p="<<p
<<" a[i]="<<a[i]<<std::endl;
p = x * p + a[ i ];
} // for(i)
cout<<"You enter "<<c1
<<" degree polynomial and the answer is: "<<p<<std::endl;
}
/*  output 
for(i=0) p=0 a[i]=3
for(i=1) p=3 a[i]=4
for(i=2) p=10 a[i]=5
You enter 2 degree polynomial and the answer is: 25
*/

Bob R
POVrookie   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 1085
 replies: 12
 date asked: Jun 8 '07
