LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2010, 07:56 PM   #1
Galib
Member
 
Registered: Mar 2009
Location: $HOME
Distribution: Slackware64
Posts: 69

Rep: Reputation: 17
Question Expression Simplification (Adding like terms) [C++]


I am having a little trouble understanding how to loop through 6x^2 + 10x^2 + 9x^2 + 6x^1 + 15x^1 + 9 to combine like terms.

Each polynomial is stored as a LinkedList holding a coefficient and exponent. Thus, I can just compare the exponents to see if they match, and if they do, add them together and insert into new LinkedList. If they don't match, then add them into the LinkedList.

I actually used the List ADT to create a list of polynomials. So I've got a list of polynomials, each containing a struct that stores a coefficient and exponent.

I am having a hard time combining like terms... I know what I need to do, but I just can't seem to get it in code form.

6x^3 + 10x^2 + 9x^2 + 6x^1 + 15x^1 + 9
should turn into
6x^3 + 19x^2 + 21x^1 + 9

We can assume that the list will always be sorted in descending order.
Thank you!
 
Old 10-06-2010, 11:49 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Hint: Write a function that receives a single argument the exponent value. This function will step through the linked list and return the node with the matching exponent value or null if there is no match.
 
Old 10-07-2010, 01:23 AM   #3
Galib
Member
 
Registered: Mar 2009
Location: $HOME
Distribution: Slackware64
Posts: 69

Original Poster
Rep: Reputation: 17
I've gotta use iterators, I've written a simplify that works using pointers. This is due to having access to next, which I don't with iterators.

Some pseudo code,
Code:
for(i = list.begin(); i != list.end(); i++) {
if(i->exp == (this is where I would have i->next->exp)) // to check to see if the next exponent is the same value
} 

if it was the same, then it just added the coefficients and input them into a new list
My problem is combining like terms using iterators and the list ADT.

Thank you.
 
Old 10-07-2010, 01:43 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Well unless you know that the equation has been entered in order you will need a second iterator to step through each node in the original list. Although I suspect that given you will have two lists you can work with two iterators one on each list.
 
Old 10-07-2010, 02:48 AM   #5
Galib
Member
 
Registered: Mar 2009
Location: $HOME
Distribution: Slackware64
Posts: 69

Original Poster
Rep: Reputation: 17
I have one list, which stores 6x^3 + 10x^2 + 9x^2 + 6x^1 + 15x^1 + 9 .

EDIT: I actually found a fix, but it's very ghetto yet works. I am still interested if any of you know how to combine like terms using iterators.

Last edited by Galib; 10-07-2010 at 03:05 AM. Reason: Found solution.
 
Old 10-07-2010, 03:28 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
So on that example you iterate through the list getting the node and then add it to your result list. You will insert the node to the list if the previous node (of the result list) has a different exponent otherwise you will increment the coefficient of the previous node if the exponent is the same. The trick is with the first time through when the previous node doesn't exist and so you will need to add a comparison to null to your test.

However this would not work with a list stored as follows: 10x^2 + 6x^3 + 9x^2 + 6x^1 + 15x^1 + 9 .

Last edited by graemef; 10-07-2010 at 03:30 AM.
 
Old 10-07-2010, 10:53 PM   #7
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by Galib View Post
I have one list, which stores 6x^3 + 10x^2 + 9x^2 + 6x^1 + 15x^1 + 9 .

EDIT: I actually found a fix, but it's very ghetto yet works. I am still interested if any of you know how to combine like terms using iterators.
Please show your solution. Since this task seems like a school-work problem, showing you a solution would be a breach of the (homework) code of conduct for this forum.
 
Old 10-07-2010, 10:58 PM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by graemef View Post
...
However this would not work with a list stored as follows: 10x^2 + 6x^3 + 9x^2 + 6x^1 + 15x^1 + 9 .
Since the OP is apparently using the STL list, he could always sort the list before attempting to simplify the equation.
 
Old 10-07-2010, 11:43 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by dwhitney67 View Post
Since the OP is apparently using the STL list, he could always sort the list before attempting to simplify the equation.
Sure but using a second iterator on the second (condensed list) would be more efficient. As I was hinting at in an earlier post
 
Old 10-08-2010, 07:06 AM   #10
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Galib View Post
We can assume that the list will always be sorted in descending order.
That assumption eliminates the issue graemef and dwhitney67 have been discussing, as well as eliminating anything that would make the project non trivial.

The fact that you are reading one list and writing a new one (vs. modifying the original list in place) makes the task even simpler.

It's hard to guess why using iterators for this task is in any way inconvenient.

If you have code that is more awkward than you would like, post the code and you probably will get some suggestions about what you might do better.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: The application of Linux simplification LXer Syndicated Linux News 0 06-21-2010 10:50 PM
regex simplification chrismiceli Linux - General 2 06-14-2008 11:04 AM
LXer: Linux leaders call for open source license simplification LXer Syndicated Linux News 0 10-30-2006 08:54 PM
LXer: Open source in '05 -- simplification, assurance LXer Syndicated Linux News 0 12-18-2005 10:31 AM
unix scrpit simplification kirmet Programming 2 10-05-2005 05:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration