473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main ()
  4. {
  5.         int Length1, Length2, i, j, E;
  6.         char str1[11], str2[11];
  7.         int a[10]={0}, b[10]={0}, c[11]={0};
  8.         printf("Please Enter Two Integers, 10 Digits Maximum >>");
  9.         scanf("%s%s", str1, str2);
  10.         Length1 = strlen(str1);
  11.         Length2 = strlen(str2);
  12.         i=0;
  13.         while(str1[i] != '\0')
  14.         {
  15.                 a[(10-(Length1-i))] = ((int)str1[i] - 48);
  16.                 i++;
  17.         }
  18.         i=0;
  19.         while(str2[i] != '\0')
  20.         {
  21.                 b[(10-(Length2-i))] = ((int)str2[i] - 48);
  22.                 i++;
  23.         }
  24.         j=10;
  25.         E=0;
  26.         while(j > -1)
  27.         {
  28.                 if(j==0)
  29.                 {
  30.                         E=0;
  31.                         E=(a[0]+b[0])/10;
  32.                         c[0]=E;
  33.                 }
  34.                 c[j] = (a[j-1]+b[j-1]+E)%10;
  35.                 E=(a[j-1]+b[j-1])/10;
  36.                 j--;  
  37.         }
  38.         printf("%d\n", c[10]);
  39.         printf("%d\n", c[9]);
  40.         printf("%d\n", c[8]);
  41.         printf("%d\n", c[7]);
  42.         printf("%d\n", c[6]);
  43.         printf("%d\n", c[5]);
  44.         printf("%d\n", c[4]);
  45.         printf("%d\n", c[3]);
  46.         printf("%d\n", c[2]);
  47.         printf("%d\n", c[1]);
  48.         printf("%d\n", c[0]);
  49.         for(i=0; i < 10; i++)
  50.                 printf("%d", a[i]);
  51.         printf(" + ");
  52.         for(i=0; i < 10; i++)
  53.                 printf("%d", b[i]);
  54.         printf(" = ");
  55.         for(i=0; i < 11; i++);
  56.                 printf("%d", c[i]);
  57.         printf("\n");
  58.         return 0;
  59. }
  60.  
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
File Type: txt LargeAdd.txt (1.6 KB, 462 views)
Apr 2 '10 #1
7 3601
an addendum:

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

thanks!
Apr 2 '10 #2
RedSon
5,000 Expert 4TB
Next time try using CODE tags when you post your code, your original formatting will be (mostly) preserved.
Apr 2 '10 #3
whodgson
542 512MB
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.
Apr 3 '10 #4
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.
Apr 3 '10 #5
whodgson
542 512MB
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.
Apr 4 '10 #6
whodgson
542 512MB
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?
Apr 4 '10 #7
whodgson
542 512MB
Here is a solution that reduces the use of arrays
Expand|Select|Wrap|Line Numbers
  1.  cout<<"type a series of digits\n";
  2.     getline(cin,s);
  3.     cout<<"the string is "<<s<<endl;
  4.     len=s.length();
  5.     n=len;
  6.     x=pow(10,n-1);
  7.     for(int i=0;i<len;i++)
  8.     {
  9.     sum_s+=(s[i]-'0')*x;
  10.     x/=10;}
  11.     cout<<"the number converted from the string is "<<sum_s<<endl;    
Apr 7 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mariusz Sakowski | last post by:
I'm writing class which will be able to store large numbers (my ambition is to make it able to operand on thousands of bits) and perform various operations on it (similiar to those available with...
11
by: Grant Edwards | last post by:
I give up, how do I make this not fail under 2.4? fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pack("HBB",0x1c,0x00,0x00)) I get an OverflowError: long int too large to convert to int ...
4
by: KC | last post by:
How can I represent a 22-digit integer in C? float is not large enough. Thanks!
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
22
by: Frinton | last post by:
Hi, I am trying to do some calculations on large numbers (ie 7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it doesn't get it quite right. Its always somewhere between 10...
13
by: bob | last post by:
If you were designing a class to represent very large integers in C++, what kind of internal representation would be best?
1
by: xvader | last post by:
Hi, I'm new here, and I'm having a problem implementing a multiplication method for a class called VeryLongInt. It's for a homework problem. As far as I know, it should work just like the Bignum...
14
by: thehobbit | last post by:
Hi, Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and pass the result back to a calling program in COBOL? We were able to add up to 15 digit numbers without any problems,...
27
by: jty0734 | last post by:
i wanna large divide. input is 1~1024 bit integer. first, i think using sub is solution. so i made two array. and sub divisor to dividend. but this situation happen.
12
by: fermineutron | last post by:
I am trying to write a function which will convert a large ascii number, say 100 ascii digits, to its binary representation. It seems that evey algorithm I am trying to think of is backwards. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.