954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Display Polynomial

Hello, and happy new year first of all.

What I am trying to do is make a program that according to the values the user inputs then displays a polynomial.
The forumla for the polynomial is this:

float par(float x) //H timh ths synarthshs sto shmeio Xo
{
	float tel=trap[0][1], timx=1;
	int i;
	for(i=0;i<n-1;i++)
	{
		timx=timx*(x-trap[i][0]);
		tel=tel+trap[0][i+2]*timx;
        
	}
	return tel;
}


Where then the user is asked to input a value for x and then the result of the polynomial is calculated. It all works fine- the result is correct and all- but I can't seem to get the program to display the polynomial before the result is calculated.
It should be like this:
f(x0)+f[x0,x1](x-x0)+f[x0,x1,x2](x-x0)(x-x1)...+f[x0,...,xn](x-x0)...(x-xn)

where f[...]=trap[i][j]


Thanks for the help people.

Raymond10
Newbie Poster
7 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

It shouldn't be too difficult. You could either create another function to display the polynomial or add a couple of cout statements in your current function. In either case, you'll probably need two for loops, an outer one that will iterate over f(x0), f[x0, x1], ..., f[x0, ... xn] and an inner one that will iterate over x0, x1, ..., xk, for each f[x0, ..., xk] (it looks like you store x0, x1, ..., xk as trap[0][0], trap[1][0], ..., trap[k][0], so, you already have everything you need). By the way, I believe you also need to use two loops in order to compute the value of the polynomial (which implies that your current code doesn't calculate the result correctly).

Also, I have a question too: Do you need this for Mrs. Gousidou's exercise?

EDIT:

Ok, I see what you do now. You don't need two loops to compute the result. But if you want to print the polynomial, you'll need two loops.

m4ster_r0shi
Posting Whiz in Training
229 posts since Mar 2010
Reputation Points: 154
Solved Threads: 31
 
It shouldn't be too difficult. You could either create another function to display the polynomial or add a couple of cout statements in your current function. In either case, you'll probably need two for loops, an outer one that will iterate over f(x0), f[x0, x1], ..., f[x0, ... xn] and an inner one that will iterate over x0, x1, ..., xk, for each f[x0, ..., xk] (it looks like you store x0, x1, ..., xk as trap[0][0], trap[1][0], ..., trap[k][0], so, you already have everything you need). By the way, I believe you also need to use two loops in order to compute the value of the polynomial (which implies that your current code doesn't calculate the result correctly).


Thanks for the help, but also part of my problem is that I'm not sure how I can let x stay as a character since it is a part of the formula and the function... I'm not very experienced in programming, so in my terms this is what I mean:

xval=xval*(<strong>"x"</strong>-table[i][0]);


I don't think I can use cout because I need to have it looped. Sorry but I'm not very good at explaining...Also, I have a question too: Do you need this for Mrs. Gousidou's exercise?

Hmm nah not sure who that Mrs. Gousidou is, it's a project I started a few days ago by myself and I still haven't finished it. Tough stuff.

Raymond10
Newbie Poster
7 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
Sorry but I'm not very good at explaining...


Then why don't you post an exact example of what you want the output to look like? It's always easier toshow than explain, although both are important.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Well I want the polynomial to be displayed like this:

f(x0)+f[x0,x1](x-x0)+f[x0,x1,x2](x-x0)(x-x1)...+f[x0,...,xn](x-x0)...(x-xn)
eg.

2+3(x-4)-1(x-4)(x-5)+7(x-4)(x-5)(x-2)

I know that I need one loop to change x0,x1,...xn inside the parenthesis. And I also need another loop to change the number of the parenthesis each time, but I'm not sure how to do that since the parenthesis contains x, which should be just a character.
Get it now?

Raymond10
Newbie Poster
7 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
I don't think I can use cout


But, how will you print anything without using cout?part of my problem is that I'm not sure how I can let x stay as a character since it is a part of the formula and the function
You can't do it like this. You'll have to use two loops: for i = 0 -> N
{
if ( /* something involving i */ ) print "+"

print "("
print f[x0, ..., xi]
print ")"

for j = ??? -> ???
{
print "(x-"
print xj
print ")"
}
}

m4ster_r0shi
Posting Whiz in Training
229 posts since Mar 2010
Reputation Points: 154
Solved Threads: 31
 

I don't know how it will work if I break this line

xval=xval*(x-table[i][0]);


with cout.

Raymond10
Newbie Poster
7 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Well, as I said above, you can't do it like this. Leave that as it is and write
another function to print the polynomial, using the pseudocode I posted
above. It shouldn't be too difficult, unless the code you posted isn't yours.

m4ster_r0shi
Posting Whiz in Training
229 posts since Mar 2010
Reputation Points: 154
Solved Threads: 31
 

Thanks it worked indeed... I need more exercising in C++...
I might need more help later but for now I'm covered, thanks!

Raymond10
Newbie Poster
7 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Alright, I have one more question... can anyone give me a hint of how I can convert reverse polish notation into "normal" mathematical notation whn using C++?
Thanks.Alright, I have one more question... can anyone give me a hint of how I can convert reverse polish notation into "normal" mathematical notation whn using C++?
Thanks.

Raymond10
Newbie Poster
7 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Recommend you search the site as this is a common problem to solve with C++. If you have any questions after doing the search, then start another thread given there is no continuity between the polynomial thread and the reverse polish logic other than you are the poster for both questions.

HINT: stacks are commonly used.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

Yeah got carried away sorry. I'll make a search and be a good boy.

Raymond10
Newbie Poster
7 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You