Help in my program it is not given the right answer

S

sdlt85

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;
}
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;
}
cout<<"You enter "<<c-1<<" degree polynomial and the answer is:
"<<p<<endl;
}
 
V

Victor Bazarov

Hi, I need some help.
[..]
for (int i=n-1; 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*p-a;
}
cout<<"You enter "<<c-1<<" degree polynomial and the answer is:
"<<p<<endl;
}


V
 
F

Fred Kleinschmidt

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;
}
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=n-1; i>=n; i--)
{
p=x*p-a;
}
cout<<"You enter "<<c-1<<" degree polynomial and the answer is:
"<<p<<endl;
}
 
A

Alan Johnson

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;
}
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;
}


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;
 
S

sdlt85

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;
}
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;
}


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;

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


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;
}
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;
}
cout<<"You enter "<<c-1<<" degree polynomial and the answer
is: "<<p<<endl;
}
 
A

Alan Johnson

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;
}
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;
}

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;

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


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;
}
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;
}
cout<<"You enter "<<c-1<<" 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.
 
S

sdlt85

(e-mail address removed) 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;
}
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;
}
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;
cout<<"You enter "<<c-1<<" degree polynomial and the answer is: "<<p<<endl;
}

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;
}
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;
}
cout<<"You enter "<<c-1<<" 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.


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.
 
A

Alan Johnson

(e-mail address removed) 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;
}
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;
}
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;
cout<<"You enter "<<c-1<<" 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;
}
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;
}
cout<<"You enter "<<c-1<<" 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.


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
 
B

BobR

TRIM!
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'.
 
S

sdlt85

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
 
B

BobR

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="<<a<<std::endl;
p = x * p + a[ i ];
} // for(i)
cout<<"You enter "<<c-1
<<" degree polynomial and the answer is: "<<p<<std::endl;
}
/* - output -
for(--i=0) p=0 a=3
for(--i=1) p=3 a=4
for(--i=2) p=10 a=5
You enter 2 degree polynomial and the answer is: 25
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top