Hey guys, back again with a question

This is a discussion on Hey guys, back again with a question within the C++ Programming forums, part of the General Programming Boards category; All right, so this time I have an assignment to add two positive integers up to 20 digits long and ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    36

    Hey guys, back again with a question

    All right, so this time I have an assignment to add two positive integers up to 20 digits long and output the two entries along with the sum. To this point I've been able to get the program to compile but the output is out of whack. Here's what I've got, and an example of the output:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
      char augend[20] = {0}, addend[20] = {0}, sum[21], temp[20];
      int aug = 0, add = 0, sumc = 20, tempc = 0;
      int carry = 1;
    
      cout << "This program adds two positive integers up to 20 digits long."
           << "\n\n";
      cout << "Enter the augend: ";
      cin >> noskipws >> temp[0];
      while(temp[tempc]!='\n')
        {
          tempc++;
          cin >> temp[tempc];
        }
      cout << "Enter the addend: ";
      cin >> noskipws >> addend[0];
      while(addend[add]!='\n')
        {
          add++;
          cin >> addend[add];
        }
      for(aug=tempc; aug>=0; aug--, tempc--)
        {
          augend[aug] = temp[tempc];
        }
      for(aug=19, sumc=20; aug>=0, sumc>=0, add>=0; aug--, sumc--, add--)
        {
          if(augend[aug] + addend[add] >= 10)
            {
              sum[sumc] = augend[aug] + addend[add] - 10;
              sum[sumc - 1] = carry;
            }
          else
            {
              sum[sumc] = augend[aug] + addend[add];
            }
        }
      temp[tempc]='\0';
      addend[add]='\0';
      cout << "\n\n" << temp << "+" << addend << "=" << sum << ".\n";
      return 0;
    }
    And a sample output:

    Code:
    This program adds two positive integers up to 20 digits long.
    
    Enter the augend: 2021
    Enter the addend: 395
    
    
    2021
    +395
    = R@.
    All right, what's screwy here? I have a feeling part of it is due to the addition of the two arrays (augend and addend) - I'm not confident I've done that right. Advice/assistance?

  2. #2
    deletedforumuser
    Guest
    Could you say step by step what you are trying to do?

    I really don't understand what your trying to do.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Sure... at this point in my class (well, for this homework assignment, really), it's an emphasis on arrays. What the assignment is asking is for us to do the following...

    1) Read in up to a 20-digit positive integer into the "temp" (first number) array.
    2) Read in up to a 20-digit positive integer into the "addend" (second number) array.
    3) Shift the "temp" array into an "augend" array which allows for easier addition. If it's a three digit number, then it's akin to taking the 0, 1, 2 places of the "temp" array and putting them into the 17, 18, 19 places (respectively) of the "augend" array.
    4) Add the individual digits of the "augend" and "addend" arrays place by place (i.e., augend[19]+addend[19]) and putting them into a "sum" array.
    5) Display the augend and addend arrays in the output with the sum array.

    Example:

    temp[] is read in as 2345. addend[] is read in as 678.
    temp[0] = 2, temp[1] = 3, temp[2] = 4, temp[3] = 5. Once the shift is done, it would be: augend[16] = 2, augend[17] = 3, augend[18] = 4, augend[19] = 5.

    Then add the "augend" and "addend" arrays. So it would be...

    augend[19] + addend[2] = sum[20]
    augend[18] + addend[1] = sum[19]
    augend[17] + addend[0] = sum[18]
    augend[16] = sum[17]

    Of course another wrinkle is the use of "carry" to aid in carrying over the 1 that comes from things like 8+5 and 7+4.

  4. #4
    deletedforumuser
    Guest
    And what is the error your getting?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Well, look at the sample output above. I put in 2021 and 395 ... and the result is R@?

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    1) You have wrong initialization. This just makes the first char to 0. You want everything in the array to 0. Actually you want everything in the array to '0' not 0!
    2) Shouldn't it be augend[aug] + addend[add] >= '10' //'10' not 10 ?
    3) Shouldn't it be sum[sumc] += augend[aug] + addend[add] - 10; // += not = since you might have a carry ?
    4) Shouldn't you initialize sum to '0'?

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Well I've made a few changes and still am getting an error upon output. Here's the current code along with a sample output.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
      char augend[20] = {'0'}, addend[20] = {'0'}, sum[21] = {'0'}, temp[20];
      int aug = 0, add = 0, sumc = 20, tempc = 0;
      int carry = 1;
    
      cout << "This program adds two positive integers up to 20 digits long."
           << "\n\n";
      cout << "Enter the augend: ";
      cin >> noskipws >> temp[0];
      while(temp[tempc]!='\n')
        {
          tempc++;
          cin >> temp[tempc];
        }
      cout << "Enter the addend: ";
      cin >> noskipws >> addend[0];
      while(addend[add]!='\n')
        {
          add++;
          cin >> addend[add];
        }
      for(aug=19; tempc>=0; aug--, tempc--)
        {
          augend[aug] = temp[tempc];
        }
      for(aug=19, sumc=20; aug>=0, add>=0; aug--, sumc--, add--)
        {
          if(augend[aug] + addend[add] >= '10')
            {
              sum[sumc] += augend[aug] + addend[add] - 10;
              sum[sumc - 1] = carry;
            }
          else
            {
              sum[sumc] += augend[aug] + addend[add];
            }
        }
      cout << "\n\n" << temp << " + " << addend << " = " << sum << ".\n";
      return 0;
    }
    Code:
    This program adds two positive integers up to 20 digits long.
    
    Enter the augend: 1234
    Enter the addend: 3456
    
    
    1234
     + 3456
     = 0.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Does anybody else have advice on this? I would like to do the assignment with arrays to really get the concept of it down but at this point I'm considering just scrapping it and using strings (which we haven't done yet) since the assignment is due tonight at midnight. The output is still the part that's making me scratch my head -- for one, the returns are still in the character arrays and, of course, the sum is being given out as 0 no matter what numbers I input.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    aug>=0, add>=0
    You can't use commas in the second part of a for-loop. (Well, you can, obviously, but it's not going to be what you expect -- the first part will be completely ignored. Use || if you want or, or && if you want and.)

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Honestly, I would have stepped in and helped by now, but your variable names are poorly chosen & terse, and you haven't added any comments for what you intend your code to be doing at any given point in time, so.... I passed on participating.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Dino, I was just glad to see something syntactical that I didn't need to actually parse. And Velocity, you shouldn't have needed me to tell you that, since your compiler has been complaining about it the whole time:
    Code:
    temp.cpp:31:39: warning: multi-character character constant
    temp.cpp: In function ‘int main()’:
    temp.cpp:29: warning: left-hand operand of comma has no effect
    And yes '10' is amazingly wrong. If you add '1' to '9' you'll get 'j'. You need to, when you see '1', actually use the value 1, not the value 49.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys..need help on pointers
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2008, 03:57 PM
  2. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  3. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 06:10 AM
  5. hey peeps, the lord of the nightmares is back
    By Null Shinji in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-13-2001, 12:17 AM

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