simple math/c question

This is a discussion on simple math/c question within the C Programming forums, part of the General Programming Boards category; Hello everyone! I need to write a program in C, but i'm stuck. This is what I have to do: ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    simple math/c question

    Hello everyone!

    I need to write a program in C, but i'm stuck.
    This is what I have to do:
    A program, that will add two base-5 numbers and display the result on the screen afterwards.

    The problem is, that when you add two numbers in C, they will always be added like they're base-10 numbers of course. How to avoid that?

    Thank you very much,
    unimatrix

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you add numbers that are bigger than a single digit on paper? You carry anything over 10. Certainly you can figure out how to change a 10 into a 5.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    Thanks for the reply quzah. You've made quite a good point. I certainly can add any based numbers on paper. But the problem is I don't know how to do that in C. Well, to be honest, I'm not quite an experienced programmer yet.
    I'm not asking you to write me a program or anything, but any clue would be welcome.

    Thanks,
    unimatrix

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Computers calculate in base 2 - base 5 would just be a visual representation of the result.

    Like with this non-portable function
    http://www.mkssoftware.com/docs/man3/itoa.3.asp
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    There are a few ways you could approach this. One way would be to take the input, which will be in base 5, convert it to base 10, use C's inbuilt arithmetic operators to do your calculation, convert back to base 5, then output the number. Another way would be to store each base 5 digit (pendit? quingit? I don't know...) in an array, and add them like you would do on paper. To do it this way, you'd work out the steps you take when adding by hand, then convert them into C code. The trickiest thing in this method would probably be working out how much to carry and how much to keep in each column. The modulus operator (%) would likely be quite useful at this point.

    I'm sure there are other ways, those are just the first two that sprang to mind. Which way you choose to do it depends on what the requirements of your task are.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    Talking Success!

    Hey, I just wanted to tell you that after almost one week of trying i've finaly succeeded

    Thanks,
    unimatrix
    Last edited by unimatrix; 10-18-2005 at 10:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 09:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21