473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Help in my program it is not given the right answer

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

Jun 8 '07 #1
12 1357
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 "<<c-1<<" degree polynomial and the answer is:
"<<p<<endl;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '07 #2

<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=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
Boeing Associate Technical Fellow
Aero Stability and Controls Computing
Jun 8 '07 #3
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=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 "<<c-1<<" degree polynomial and the answer is:
"<<p<<endl;
}
--
Alan Johnson
Jun 8 '07 #4
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=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 "<<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[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 "<<c-1<<" degree polynomial and the answer
is: "<<p<<endl;
}

Jun 9 '07 #5
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=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 "<<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[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 "<<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.

--
Alan Johnson
Jun 9 '07 #6
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=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 "<<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[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 "<<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.

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

Jun 9 '07 #7
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=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 "<<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[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 "<<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.

--
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
Jun 9 '07 #8
I need help please

Jun 9 '07 #9

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

<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 "<<c-1
<<" 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
Jun 10 '07 #12
Ok, I got it.
Thanks ^_^

Jun 10 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Josh | last post by:
Howdy i am newb somewhat to programing and i was just for fun trying to compile a program that asks the user for an odd int less than 22 and then returns this ***************** ******* *********...
15
by: Buck Rogers | last post by:
Hi guys! Task 1: Write a program which presents a menu with 5 options. The 5th option quits the program. Each option should execute a system command using system(). Below is my humble...
54
by: bnp | last post by:
Hi, I took a test on C. there was an objective question for program output type. following is the program: main() { char ch; int i =2;
4
by: DAL | last post by:
I want to build my kid a program that cycles through questions (using a label for the question), and lets him choose one of two radio buttons for the right answer. How do I get every set of...
16
by: C++ Hell | last post by:
Hey everyone just designing a quiz for school and managed to write the code for the questions and answers thou i have to add scores and at the end an overall score does anyone have any idea what to...
10
by: gshy2014 | last post by:
Hi, all. IN Windows, TCHAR is translated to be wchar or char according to whether UNICODE is defined. Dose the complier handle the UNICODE(define or not define) according to the OS ? Many...
8
by: pamelafluente | last post by:
I am beginning aspNet, I know well win apps. Need a simple and schematic code example to start work. This is what I need to accomplish: ---------------------- Given button and a TextBox on a...
18
by: Neehar | last post by:
Hello For one of the interviews I took recently, I was given an offline programming quiz. In 30 minutes I had to write code in C++ to counts the number of times each unique word appears in a...
1
by: laila2ethan | last post by:
Need someones help , I am having a very difficult time understanding parameters and functions, can someone help me answer these questions 1. Write a program that helps an elementary school student...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.