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: -
#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.
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!
Next time try using CODE tags when you post your code, your original formatting will be (mostly) preserved.
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.
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.
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.
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?
Here is a solution that reduces the use of arrays - 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;
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
...
|
by: KC |
last post by:
How can I represent a 22-digit integer in C? float is not large enough.
Thanks!
|
by: Sri |
last post by:
How do you add an n-bit number in C?
Regards,
Sri
|
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...
|
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?
|
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...
|
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,...
|
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.
|
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. ...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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...
| |