Large Numbers

This is a discussion on Large Numbers within the C++ Programming forums, part of the General Programming Boards category; I Have Another Homework Assignment This Time Adding Large Numbers. The Program Must Ready Up to 12 large positive numbers ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    Large Numbers

    I Have Another Homework Assignment This Time Adding Large Numbers. The Program Must Ready Up to 12 large positive numbers and add them accurately. Here Are A Few Examples On How The Teacher Wants The Numbers Displayed.

    First Ask How Many Then Get the Input i.e.

    How many numbers? -----> 4

    Input Number #1 -----> 4
    Input Number #2 ------>1000000009.12345
    Input Number #3 ------> 99999.999
    Input Number #4 -----> 723456.

    The Sum Is:
    123.00000
    1,000,000,009.12345
    99,999.99900
    723,456.00000
    +)
    --------------------------------
    1,000,823,588.12245

    Here is what I have so Just To Get The Math Right. I Still Need To Ask For The Input And Display The Outcome Any Suggestions Will Be Helpful.

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    
        //large numbers:
        int one[10] = { 1,2,3,4,5,6,7,8,9,9}; //actual number 1,234,567,899
        int two[10] = { 2,3,3,4,5,6,7,8,9,0}; //actual number 2,334,567,890
        int result[10]; // we'll use this to store digits
    
        //some holders we'll need
        int carryOver = 0, value = 0;
        
        for(int i = 9; i >=0; i--) {
            //add
            value = one[i] + two[i];
            if (value > 9){ 
                value -=10; 
               
                result[i] = value + carryOver;
                carryOver = 1;
            }
            else {
                result[i] = value + carryOver;
                carryOver = 0;
            }
        }
        //display
        for(int i = 0; i < 10; i++){
            cout << result[i] << " ";
        }
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your input doesn't match your output.

    Anyway: you will need to always add in the carry from the previous. The carry can be more than 1, so there's no reason to hard-code that in.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Oh, come on!! Just use long int or even long long int to input large numbers! (I don't know about long long int or LARGE_INT, because your OS might not support them.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Also use "unsigned" if you want to input only positive values, to make the max number twice as large

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Large_integer

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    not LARGE_INT

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    With capital letters

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    There's a good way to increase your post count...
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Sorry... i'm not used to this posting technic...

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    You mean "technique"

    ...

    with lowercase letters.


    mcdant01: What is the largest number your program needs to be able to accept? Also, how many decimal places are you going to accept?
    How do you plan to deal with the decimal point in your internal representation?
    We can't really give any help until you've said what you're having trouble with though.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to deal wth large numbers
    By bvgsrs in forum C++ Programming
    Replies: 2
    Last Post: 06-18-2007, 06:16 AM
  2. Handling Large Numbers
    By Xzyx987X in forum C Programming
    Replies: 2
    Last Post: 05-03-2004, 02:33 PM
  3. Using really big numbers!
    By Machewy in forum C++ Programming
    Replies: 11
    Last Post: 02-26-2004, 09:49 AM
  4. Help needed with VERY large numbers.
    By jwarner in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2004, 11:01 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

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