Large Digit Addition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bearsindc
    New Member
    • Apr 2010
    • 3

    Large Digit Addition

    howdy,

    as part of an introductory C course, i have been tasked to write a program that will add two large integers (in this case 10 digits max). the method that we have been asked to use is:

    read in the user input integers as character strings
    convert the character strings to int strings
    add them

    my addition method (as you'll see in the code below) is successful, until it reaches c[0] (where c[] is the final sum). i'm not sure what the error is, but i get a "junk" number in that position and when i print my final summation the value of c[] is returned as "-1".

    here's my code:
    Code:
    #include<stdio.h>
    #include<string.h>
    int main ()
    {
            int Length1, Length2, i, j, E;
            char str1[11], str2[11];
            int a[10]={0}, b[10]={0}, c[11]={0};
            printf("Please Enter Two Integers, 10 Digits Maximum >>");
            scanf("%s%s", str1, str2);
            Length1 = strlen(str1);
            Length2 = strlen(str2);
            i=0;
            while(str1[i] != '\0')
            {
                    a[(10-(Length1-i))] = ((int)str1[i] - 48);
                    i++;
            }
            i=0;
            while(str2[i] != '\0')
            {
                    b[(10-(Length2-i))] = ((int)str2[i] - 48);
                    i++;
            }
            j=10;
            E=0;
            while(j > -1)
            {
                    if(j==0)
                    {
                            E=0;
                            E=(a[0]+b[0])/10;
                            c[0]=E;
                    }
                    c[j] = (a[j-1]+b[j-1]+E)%10;
                    E=(a[j-1]+b[j-1])/10;
                    j--;  
            }
            printf("%d\n", c[10]);
            printf("%d\n", c[9]);
            printf("%d\n", c[8]);
            printf("%d\n", c[7]);
            printf("%d\n", c[6]);
            printf("%d\n", c[5]);
            printf("%d\n", c[4]);
            printf("%d\n", c[3]);
            printf("%d\n", c[2]);
            printf("%d\n", c[1]);
            printf("%d\n", c[0]);
            for(i=0; i < 10; i++)
                    printf("%d", a[i]);
            printf(" + ");
            for(i=0; i < 10; i++)
                    printf("%d", b[i]);
            printf(" = ");
            for(i=0; i < 11; i++);
                    printf("%d", c[i]);
            printf("\n");
            return 0;
    }
    the large group of printf statements was my way of debugging, and it proves that the addition method works until c[0].

    i'd greatly appreciate any insight.

    thanks!

    p.s. attached is the code in a .txt in case... whatever.
    Attached Files
    Last edited by RedSon; Apr 2 '10, 10:17 PM. Reason: Be sure to use CODE tags or most people will ignore your post.
  • bearsindc
    New Member
    • Apr 2010
    • 3

    #2
    an addendum:

    the .txt preserves my original formatting, and is much easier to read. next time i'll preview my post beforehand.

    thanks!

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      Next time try using CODE tags when you post your code, your original formatting will be (mostly) preserved.

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        Of course it is possible to sum two 10 digit numbers and finish with a number comprising 10 digits but I can`t see where the garbage is coming from.
        I`m wondering why you wouldn`t use the atoi() function [viz. int atoi(const char* str1)] to convert each string to an int before summing the 2. This saves all the reverse looping to keep the digits aligned from the least significant RH end.

        Comment

        • bearsindc
          New Member
          • Apr 2010
          • 3

          #5
          i would have loved to use atoi(), however it was one of those "do it the hard way then i'll show you a trick," type deals.

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            It just occurs that each string when converted would be just short of 10 billion if max allowable number of digits were used. This is well in excess of the max value of an unsigned int. So presumably it would generate some form of undefined behavior. If this is correct you would need to use a type float or double. I`m not sure about a long long.

            Comment

            • whodgson
              Contributor
              • Jan 2007
              • 542

              #7
              Your code compiled ok but could not get it to accept the 2 strings. How did you enter these two data? Just 123654 789654 then enter or 123654 enter then
              789654 enter?

              Comment

              • whodgson
                Contributor
                • Jan 2007
                • 542

                #8
                Here is a solution that reduces the use of arrays
                Code:
                 cout<<"type a series of digits\n";
                    getline(cin,s);
                    cout<<"the string is "<<s<<endl;
                    len=s.length();
                    n=len;
                    x=pow(10,n-1);
                    for(int i=0;i<len;i++)
                    {
                    sum_s+=(s[i]-'0')*x;
                    x/=10;}
                    cout<<"the number converted from the string is "<<sum_s<<endl;

                Comment

                Working...