445,820 Members | 1,142 Online Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

# Help in my program it is not given the right answer

 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 #include 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>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 "<
12 Replies

 P: n/a sd****@gmail.com wrote: 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[i]; } cout<<"You enter "<

 P: n/a #include 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>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 Also, you only initialized elements 0 through c, so you have garbage in a[c+1] through a for (int i=n-1; i>=n; i--) { p=x*p-a[i]; } cout<<"You enter "<

 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 #include 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>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]; } 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<<"You enter "<

 P: n/a On Jun 8, 5:44 pm, Alan Johnson #include 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>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]; } 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<<"You enter "< #include 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>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 "<

 P: n/a sd****@gmail.com wrote: On Jun 8, 5:44 pm, Alan Johnson 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 #include 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>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]; } You have a number of problems here. First, you should be using an arrayof int for your coefficients instead of an array of char. Note thatmultiplying by character '3', for example, is not the same asmultiplying by 3.Second, n is not the number of coefficients you have. n is the MAXIMUMnumber of coefficients. You should be looping c times, not n times. Infact, this function doesn't even need n as a parameter.Third, your operation in Horner's rule is wrong. It should useaddition, not subtraction.Fourth, your loop conditions make no sense. You start with i=n-1, andthen loop while i>=n. But n-1 already fails that condition, so yourloop never executes at all. Life will be more simple for you if youinitialize p to 0, and then loop over every index less than c. Thecommon idiom for doing that is:int p=0;for (size_t i=c; i--;) p=x*p+a[i];cout<<"You enter "< #include 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>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 "<

 P: n/a On Jun 9, 4:46 am, Alan Johnson #include 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>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]; } 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<<"You enter "< #include 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>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 "<

 P: n/a sd****@gmail.com wrote: On Jun 9, 4:46 am, Alan Johnson sdl...@gmail.com wrote: >>On Jun 8, 5:44 pm, Alan Johnson #include 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>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]; }You have a number of problems here. First, you should be using an arrayof int for your coefficients instead of an array of char. Note thatmultiplying by character '3', for example, is not the same asmultiplying by 3.Second, n is not the number of coefficients you have. n is the MAXIMUMnumber of coefficients. You should be looping c times, not n times. Infact, this function doesn't even need n as a parameter.Third, your operation in Horner's rule is wrong. It should useaddition, not subtraction.Fourth, your loop conditions make no sense. You start with i=n-1, andthen loop while i>=n. But n-1 already fails that condition, so yourloop never executes at all. Life will be more simple for you if youinitialize p to 0, and then loop over every index less than c. Thecommon idiom for doing that is:int p=0;for (size_t i=c; i--;) p=x*p+a[i];cout<<"You enter "<#include 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>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 "<

 P: n/a I need help please Jun 9 '07 #9

 P: n/a

 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 Jun 10 '07 #11

 P: n/a

 P: n/a Ok, I got it. Thanks ^_^ Jun 10 '07 #13

### This discussion thread is closed

Replies have been disabled for this discussion. 