c++ addition program without operators?!

Discussion in 'Mac Programming' started by macman2790, Sep 3, 2007.

  1. macman2790 macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #1
    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>)
     
  2. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    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
     
  3. macman2790 thread starter macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #3
    no bitwise operators or overloading allowed. that would've been my idea. So i have really no other choice to ask for help, lol.
     
  4. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #4
    really? what happened when you combed through the allowed libraries for ideas?
     
  5. macman2790 thread starter macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #5
    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.
     
  6. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #6
    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.
     
  7. ChrisBrightwell macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #7
    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.
     
  8. weg macrumors 6502a

    weg

    Joined:
    Mar 29, 2004
    Location:
    nj
    #8
    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.
     
  9. AlmostThere macrumors 6502a

    #9
    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...
     
  10. dcr macrumors member

    Joined:
    Jun 10, 2002
    #10
    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.
     
  11. macman2790 thread starter macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #11
    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.
     
  12. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #12
    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
     
  13. weg macrumors 6502a

    weg

    Joined:
    Mar 29, 2004
    Location:
    nj
    #13
    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
     

Share This Page