Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
im taking discrete mathematics which is a freshman class(the class that im doing this for), and i've only taken one cs class to go towards my degree. I'm having trouble figuring out this program. you cant use any operator whatsoever except == && || = ! and thats about it. im not proficient in c++ and i only need to do an addition method, our professor picked a students sample code without an addition method, which we have to use. The person used reverse iterators and <list> of an enum type that lists numbers(0-9) and a - sign. I need to figure out how to add iterate through the two numbers given by the user and add them up without plus signs. My professor doesnt even want a for loop to have ++ -- so this is really ridiculous, but he let one fly with the student that made this that will be fixed later by the professor.But i cant do that with mine. Anyone have an ideas on how i would take to integers given by a user to add them up? They are a enum type and i cant use + - so it's a lot harder than a typical person would think adding something up would be:) I dont feel comfortable posting someone elses code so someone can get an idea, but i can pm it. but if this can be explained without that it will be great. thanks.

one more rule:
The only libraries i may use are <list> and the I/O libraries (<stdio>, <conio>, <iostream>)
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Are bit operations allowed?

If the objective of the assignment is about overloading operators, could you overload a different operator, or, create your own?

Todd
 

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
Are bit operations allowed?

If the objective of the assignment is about overloading operators, could you overload a different operator, or, create your own?

Todd

no bitwise operators or overloading allowed. that would've been my idea. So i have really no other choice to ask for help, lol.
 

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
really? what happened when you combed through the allowed libraries for ideas?
look that was clever, i just need help, im not quite sure what i can do, like i said im not proficient in c++, i only need help with an addition method.
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
you should do your own work. developers who know how to research unknown technologies are much more desirable than lazy ones already familiar with it.

believe it or not, i'm helping you.
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
look that was clever, i just need help, im not quite sure what i can do, like i said im not proficient in c++, i only need help with an addition method.

Sarcasm will get you nowhere here.

If you're working with a discrete number set, just create an array of integers and traverse the array as you do your operations.
 

weg

macrumors 6502a
Mar 29, 2004
888
0
nj
I need to figure out how to add iterate through the two numbers given by the user and add them up without plus signs.

Using lists, you could build a kind of lookup table for adding Integers (i.e., when you encounter 3+9, look up the result in the list that you've stored somewhere). If you take commutativity into account, the list doesn't become that large.
 
I am not clear how you are getting the numbers from the user, but using a list to do simple addition:

Code:
list<T> list1(firstNumber);
list<T> list2(secondNumber);
list1.splice(list1.end(), list2);
cout << "firstNumber plus secondNumber = " << list1.size() << endl;

If these are actually enums (and not, say integers) you will probably want a sorted reference list where T is your enum type (enum have a finite range so not really a problem) and then look up the correct position of the appropriate value, for which you will need iterators.

Not sure if I understand this right though, seems to be a bit of an odd assignment...
 

dcr

macrumors member
Jun 10, 2002
57
0
seems to be a bit of an odd assignment...

It doesn't seem so odd if you know what a course in Discrete Mathematics is trying to teach you. (Think: logic, combinatorics, state machines, etc). I suggest going to the instructor's office hours or a tutor if it's provided.
 

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
I decided that i would make a lookup table for this program. Im not sure how to make a lookup table since i havent had any data structures experience yet. Theres one problem though, my program cant have any sort of memory overflow, even if it is 10,000 digits long, is this do-able or not? i cant use ++ --, or any bit operator, so that makes it even harder.
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
AlmostThere hit the nail on the head. And then I thought - "ok, that's a stretch - and what's the point of THAT exercise?"

But, then DCR fills in the gap with the Discrete Mathematics 20,000-foot overview, and it makes sense.

(but in my opinion, still a BS assignment. There are so many other useful programming exercises and other problems to solve than this intellectual mastu... er.. exercise)

Todd
 

weg

macrumors 6502a
Mar 29, 2004
888
0
nj
I decided that i would make a lookup table for this program. Im not sure how to make a lookup table since i havent had any data structures experience yet. Theres one problem though, my program cant have any sort of memory overflow, even if it is 10,000 digits long, is this do-able or not? i cant use ++ --, or any bit operator, so that makes it even harder.

Using a LUT, you should be able to perform the addition digit by digit, e.g.:

82+79:

look up 2+9 in your table: -> 11, so the first digit of your result is 1
look up 8+7 in your table: -> 15, this gives you 5 for the next digit, plus the remaining 1 from the last position, so look up 5+1 -> 6, 6 is your second digit
finally, you end up with 1 for the third digit, so you obtain

161

For your LUT, all you need is a list of tuples of digits paired with the corresponding result, so you'd store a list

(1,1, 0,2)
(1,2, 0,3)
(1,3, 0,4)
(1,4, 0,5)
...
(2,3, 0,5)
(2,4, 0,6)
...
(9,9, 1,8)

If you have to add 4 and 1 you iterate over the list and find the
(1,4, 0,5) element.

As for the other approach that concatenates two lists and takes the length:
Try doing that with two really large numbers, it'll use quite a lot of memory.
The approach described above should scale pretty well. In fact, you don't even
have to keep the full number in the memory, you can do it digit by digit
(assuming that you can read the two input numbers digit by digit from the input file).

Of course, if you want a REALLY nice solution, you would implement a rewriting
algorithm that applies the Peano axioms ;-)

cheers,
Georg
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.