473,480 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

polynomials in c++

how do i make a program that gets numbers from a user. and then stores
it into an array. using polynomials
if the user entered 7 5 4 3 2
the polynomial would be 7x^4+5x^3+4x^2+3x+2
the program would then ask for a value of x, and solve it.

i think i got an idea how it would store plain old numbers into an
array. but not sure how it would store a polynomial into an array.
does anyone have any ideas?

Mar 27 '07 #1
4 4612
"Matthias" <pu******@gmail.comwrote in message
news:11*********************@d57g2000hsg.googlegro ups.com...
how do i make a program that gets numbers from a user. and then stores
it into an array. using polynomials
if the user entered 7 5 4 3 2
the polynomial would be 7x^4+5x^3+4x^2+3x+2
the program would then ask for a value of x, and solve it.

i think i got an idea how it would store plain old numbers into an
array. but not sure how it would store a polynomial into an array.
does anyone have any ideas?
Easist way, just store the numbers the user enters. Then get the value for
x. Then after you have all the data calculate the polynomal.
Mar 27 '07 #2
Matthias wrote:
how do i make a program that gets numbers from a user. and then stores
it into an array. using polynomials
if the user entered 7 5 4 3 2
the polynomial would be 7x^4+5x^3+4x^2+3x+2
the program would then ask for a value of x, and solve it.

i think i got an idea how it would store plain old numbers into an
array. but not sure how it would store a polynomial into an array.
does anyone have any ideas?
Use a vector. The polynomial

a_0 + a_1 x + a_2 x^2 + ... + a_n x^n

will be stored as the sequence

(a_0,a_1,...,a_n)

You can put that into a polynomial class. It would contain a vector as its
only data member. One way to go is to have as an invariant of the class
that the highest order element in the vector is always non-zero (the empty
vector will then represent the 0-polynomial). Alternatively, you would have
to provide a tweaked operator==. It is straight forward to implement the
usual arithmetic operations such as +,-,*, and evaluation at a value. In
order to make polynomials cooperate nicely with std::set<and the like,
you also will want to implement operator< or specialize std::less<>.
Best

Kai-Uwe Bux
Mar 27 '07 #3
Matthias wrote:
how do i make a program that gets numbers from a user. and then stores
it into an array. using polynomials
if the user entered 7 5 4 3 2
the polynomial would be 7x^4+5x^3+4x^2+3x+2
the program would then ask for a value of x, and solve it.

i think i got an idea how it would store plain old numbers into an
array. but not sure how it would store a polynomial into an array.
does anyone have any ideas?
C++ doesn't have polynomials as built-in data types, so you'll have to write
your own code for polynomials, like:

class Polynomial
{
protected:
std::vector<intm_Coefficients;
public:
Polynomial (std::vector<intCoefficients);
double ComputeValue (double x);
};

double Polynomial::ComputeValue (double x)
{
// Use Heron's formula to calculate the result.
for (std::vector<int>::iterator it = m_Coefficients.begin ();
it != m_Coefficients.end (); it++)
{
// TODO: Implementation needed here.
}
}
Regards,
Stuart
Mar 27 '07 #4
On Mar 27, 8:57 am, Stuart Redmann <DerTop...@web.dewrote:
Matthias wrote:
how do i make a program that gets numbers from a user. and then stores
it into an array. using polynomials
if the user entered 7 5 4 3 2
the polynomial would be 7x^4+5x^3+4x^2+3x+2
the program would then ask for a value of x, and solve it.
i think i got an idea how it would store plain old numbers into an
array. but not sure how it would store a polynomial into an array.
does anyone have any ideas?

C++ doesn't have polynomials as built-in data types, so you'll have to write
your own code for polynomials, like:

class Polynomial
{
protected:
std::vector<intm_Coefficients;
public:
Polynomial (std::vector<intCoefficients);
double ComputeValue (double x);

};

double Polynomial::ComputeValue (double x)
{
// Use Heron's formula to calculate the result.
for (std::vector<int>::iterator it = m_Coefficients.begin ();
it != m_Coefficients.end (); it++)
{
// TODO: Implementation needed here.
}

}

Regards,
Stuart
Hi,
just small remark. I think you mean Horner's rule:
7x^4+5x^3+4x^2+3x+2 = (((7x+5)x+4)x+3)x+2

Kind regards
Serge
http://www.sergejusz.com

Mar 28 '07 #5

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

Similar topics

3
1692
by: Chris | last post by:
Does anyone know of a good standalone implementation of multivariable polynomials in python? Thanks, Chris
7
4453
by: adrin | last post by:
hi, anyone knows how does an algorithm for calculating GCD of two polynomials look like ? please help me if you can, or give some relevant links :)
2
2467
by: dicapryl | last post by:
Hi all! I'm new to here .. so please excuse me if my questions are unclear etc :) I am trying to store polynomials in C++ using linked lists. My class is called Polynomial and i've created a...
0
1111
by: galathaea | last post by:
you will fall in love !! these are the cutest critters !! 3-ary polynomial: http://i10.tinypic.com/2hwlir6.png 4-ary polynomial: full http://i13.tinypic.com/47u0yfm.png
1
3514
by: beck2 | last post by:
hello i am angela and i would like if its kind of anyone to help me by writing me the code of adding 2 oplynomials? polynomials should be entered as where 3,5 and 1 are coefficients of X to the...
7
13929
by: yodadbl07 | last post by:
hey im trying to write a class of polynomials using a linked list. But I am getting an error at run time with my Polynomial* getNext() function. The error says access violation reading location. Im...
2
1758
by: manohara | last post by:
WAP a program to add 2 polynomials using a DLL
1
3191
by: unknowncute | last post by:
can you help me make a code about division of polynomials using linked list? thnx!
1
2245
by: Motanyane Tlotliso | last post by:
I wish someone to help me in creating a programm in C++ that can be able to sum two polynomials, subtract one from the other and multiply both polynomials together using a ADT's(struct term). I...
0
7076
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...
1
6730
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4767
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.