Thread: Adding two numbers represented as strings

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    Adding two numbers represented as strings

    Hi guys.I need to make a small program with a function with this prototype: void f(char *a,char *b) that adds two numbers represented as strings without using conversion operators or other tricks.Thanks

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Fine. Do you have a question doing this program ?
    Kurt

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    do you want to pay somebody to do it for you? Or do you have some code you need help with.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Let's say you have two strings, of the form "123454232" and "23475239994" (I just made them up with a few random keystrokes on my keyboard).

    Write down the steps, on paper, that you would use to "add them together". That is an essential first step to prepare for writing code. If you provide enough detail, the steps you write down on paper will represent the design of the code.

    From there, all that is needed is some understanding of how C++ does particular things. If you don't have that, you will need to apply effort to learn ..... because the PURPOSE of your homework (yes, people here know enough to realise your problem is a homework exercise) is to help you "learn by doing".

    If you expect people to just do your homework for you, have a look at this site's homework policy, here, for enlightenment.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    2
    You misunderstood me.Im not a c++ beginner and i dont want the whole code just some advices ..Anyway this problem is easy for me but i must solve this without using conversions like atoi etc..

  6. #6
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Advice on what? How to make this program? Grumpy already gave you that.

    If you're looking for advice on something else, you should just tell us. We're not psychics!
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Once you have described the requirement, as I suggested in my previous post, then the solution is simple.

    There are two bits of knowledge specific to C and C++, which should be trivial for someone who is "not a c++ beginner" to work out. The first is the relationship between each of the digits (characters in the range ['0', '9']) and each of the numeric values that are represented as single decimal digits (i.e. in the range [0,9]). The second bit of knowledge needed concerns the method of obtaining values, or changing the values, of elements of an array using a loop.

    No need to use atoi() or any other conversion.

    So, demonstrate your basic C++ knowledge, and solve this problem on your own. So far you have provided no evidence to substantiate your claim that you are not a beginner, and have not demonstrated any effort to solve your own problem.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Start off with something simple, how could you "add" two single digit ascii characters to produce a one or two digit ascii character result?

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Im not a c++ beginner
    If you were an experienced programmer of any language at all then you would certainly be capable of writing a more concise post question than you staretd with
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2007, 01:38 PM
  2. Numbers adding together = bad
    By Zyk0tiK in forum C Programming
    Replies: 5
    Last Post: 12-04-2005, 04:37 PM
  3. Adding big numbers
    By FoodDude in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 03:36 PM
  4. adding odd numbers only
    By CheyenneWay in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2004, 12:22 AM
  5. Adding two numbers
    By Maybe in forum C Programming
    Replies: 2
    Last Post: 03-11-2003, 01:55 PM