Adding large integers

I am working on this code, and it will not run. I cannot tell what is wrong with it. Please help.

In C++, the largest int value is 2147483647. So, an integer larger than this cannot be stored and processed as an integer. Similarly, if the sum or product of two positive integers is greater than 2147483647, the result will be incorrect. One way to store and manipulate large integers is to store each individual digit of the number in an array. Write a program that inputs two positive integers of, at most, 20 digits and outputs the sum of the numbers. If the sum of the numbers has more than
20 digits, output the sum with an appropriate message. Your program must, at least, contain a function to read and store a number into an array and another function to output the sum of the numbers. (Hint: Read numbers as strings and store the digits of the number in the reverse order.)
Last edited on
Just a few quick observations:
1. Use code tags. It is difficult to read your code otherwise, and I cannot point to the wrong line.
2. Don't read num1 and num2 as int. They cannot get 20 digits, read them as strings
3. Readnum should not take an array of ints as argument, it should take a string. It should return an array of ints
It is completely unclear to me how you read your numbers. cin>>num1; will read the first integer, num1=readnum(numone); will change that integer to something that depends on an array of zeros. Do you read the input integers in main, or in readnum?
I'm sorry if this is not very good formatting. This is my first time using this site. I'll try to figure out how to display it better. Thank you for telling me about using the string. And I was reading the input integers in main.
Ok. I just fixed the format. Thank you in advance.
If you read the integer in main, there should be no getline in readnum
I don't have time to look at this tonight, but I'll check back tomorrow for you.
You need to think about how the bigint is stored in memory. You have a vague idea, but you must be specific.

For your purposes (and what you seem to be trying to do):

Each individual digit of the number is stored in an int array, where each element of the array is a digit in 0 through 9.

The order in which you store the digits matters. You should store the least-significant digit (the rightmost) in index 0.

So, the number "12345" would be stored as:
int num[20] = { 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

Notice that unused digits are zeros. This is fine, since that is the same as "00000000000000012345".

You will need to fix your calculations on lines 63 and 64.

Also, it is a good idea not to hardcode ASCII values. It is easier to understand what you are doing just to say
ascii_value - '0'
anyway.


Since we have stored the number with the most significant digits at later positions in the array, you don't actually have to remember what the actual number of digits are, right?

In other words, adding
   123
+   12
is the same as adding
 00123
+00012
right?


Once you fix that, your main function should have code something like this:
1
2
3
4
	int n1[20] = {0};

	cout<<"Please enter the first positive integer (20 digits at the most): ";
	readnum(n1);


The way the number is defined also makes your addition function much simpler -- you only need lines 78 through 83:
78
79
80
81
82
83
84
85
86
87
88
89
90
void addnum(int a[20], int b[20], int sum[20])
{
	int carry=0;
	int temp;
	int i;

	for(i=0; i<20; i++)
	{
		temp=a[i]+b[i]+carry;
		sum[i]=temp%10;
		carry=temp/10;
	}
}
(All I did was chop out stuff you didn't need. Oh, and change the return type to void. Since your bignums are fixed at twenty-digits long, there is no point in caring about overflow (if carry != 0 at the end of the function).

I'll leave figuring out how to display the number up to you. You've got the right idea already, just remember that we normally write "00012" as just "12".

Hope this helps.
Thank you so much. I couldn't figure it out to save the life of me. But I'm learning. Thanks again
Topic archived. No new replies allowed.