# Help Adding Large Numbers in reverse from individually stored arrays

Page 1 of 1

## 2 Replies - 3839 Views - Last Post: 16 November 2008 - 01:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=71918&amp;s=cd85afd20560fc16a12e33c05220e62a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jerejuana2001

Reputation: 0
• Posts: 12
• Joined: 14-November 08

# Help Adding Large Numbers in reverse from individually stored arrays

Posted 14 November 2008 - 11:22 AM

I am having trouble adding the two input integers after storing them backwards in two arrays. Could I please receive some help in the add portion of this program. The two numbers will not add properly and I think it has something to do with obtaining the size_Sum input.

Here is my code.
```#include <cstdlib>
#include <iostream>
#include <cctype>

using namespace std;

const int MAXIMUM_DIGITS = 20;

int size;			  // number of digits in the integer
int digit[MAXIMUM_DIGITS]; // the digits stored in reverse order

void introduction();
void input_Large_Int(int a[], int& size_of_A);
void output_Large_Int(int a[], int size_of_A);
void add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int& size_Sum);

int main()
{
int a[MAXIMUM_DIGITS], b[MAXIMUM_DIGITS], sum[MAXIMUM_DIGITS];
int size_of_A, size_of_B, size_Sum;
char ans;

introduction();

do {

input_Large_Int(a, size_of_A);
input_Large_Int(b, size_of_B);

add(a, size_of_A, b, size_of_B, sum, size_Sum);
cout << "The sum of ";
output_Large_Int(a, size_of_A);
cout << " and ";
output_Large_Int(b, size_of_B);
cout << " is ";

output_Large_Int(sum, size_Sum);
cout << endl << endl;
cout << "Would you like to add two more numbers? (y or n): ";
cin >> ans;
}
while (ans == 'y' || ans == 'Y');

system("PAUSE");
return 0;
}

void introduction()
{
cout << "This program will allow you to input two large integers and add\n"
<< "them together. Then you will be allowed to run the program again\n"
<< "and again depending on how many calculations you would like to make.\n"
<< endl << endl;
}

void input_Large_Int(int a[], int& size_of_A)
{
char digit[MAXIMUM_DIGITS];
char change;
int i = 0;

cout << "Please enter a positive integer with no more than " << MAXIMUM_DIGITS
<< " digits: ";

cin.get(change);
if (change == '\n')
cin.get(change);
while (isdigit(change) && i < MAXIMUM_DIGITS)
{
digit[i] = change;
i++;
cin.get(change);
}

size_of_A = i;
int  j = 0;
while (i > 0)
{
i--;
a[j] = digit[i] - '0';
j++;
}

for (int i = j; i < MAXIMUM_DIGITS; i++)
a[i] = 0;

}

void add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int& size_Sum)
{
int i;

for(i = 0; i < MAXIMUM_DIGITS - 1; i++);
{
sum[i] += (a[i] + b[i]) % 10;
sum[i + 1] += (a[i] + b[i]) / 10;
}
i++;
sum[i] += (a[i] + b[i]) %10;
if ((a[i] + b[i])/10 > 0)
{
cout << "There was an overflow causing more than 20 integers.\n"
<< endl;
}
}
void output_Large_Int(int a[], int size_of_A)
{

for (int i = 0; i < size_of_A; i++)
cout << a[size_of_A - i - 1];
}

```

//This is my code, I am having trouble with obtaining the size_Sum, I believe the void add() should
//work if the size_Sum could be found. Any ideas? Thank you for your help.

This post has been edited by jerejuana2001: 14 November 2008 - 11:59 AM

Is This A Good Question/Topic? 0

## Replies To: Help Adding Large Numbers in reverse from individually stored arrays

### #2 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: Help Adding Large Numbers in reverse from individually stored arrays

Posted 14 November 2008 - 02:22 PM

jerejuana2001, on 14 Nov, 2008 - 10:22 AM, said:

I am having trouble adding the two input integers after storing them backwards in two arrays. Could I please receive some help in the add portion of this program. The two numbers will not add properly and I think it has something to do with obtaining the size_Sum input.
```#include <cstdlib>
#include <iostream>
#include <cctype>

using namespace std;

const int MAXIMUM_DIGITS = 20;

int size;              // number of digits in the integer
int digit[MAXIMUM_DIGITS]; // the digits stored in reverse order

void introduction();
void input_Large_Int(int a[], int& size_of_A);
void output_Large_Int(int a[], int size_of_A);
void add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int& size_Sum);

int main()
{
int a[MAXIMUM_DIGITS], b[MAXIMUM_DIGITS], sum[MAXIMUM_DIGITS];
int size_of_A, size_of_B, size_Sum;
char ans;

introduction();

do {

input_Large_Int(a, size_of_A);
input_Large_Int(b, size_of_B)/>;

add(a, size_of_A, b, size_of_B, sum, size_Sum);
cout << "The sum of ";
output_Large_Int(a, size_of_A);
cout << " and ";
output_Large_Int(b, size_of_B)/>;
cout << " is ";

output_Large_Int(sum, size_Sum);
cout << endl << endl;
cout << "Would you like to add two more numbers? (y or n): ";
cin >> ans;
}
while (ans == 'y' || ans == 'Y');

system("PAUSE");
return 0;
}

void introduction()
{
cout << "This program will allow you to input two large integers and add\n"
<< "them together. Then you will be allowed to run the program again\n"
<< "and again depending on how many calculations you would like to make.\n"
<< endl << endl;
}

void input_Large_Int(int a[], int& size_of_A)
{
char digit[MAXIMUM_DIGITS];
char change;
int i = 0;

cout << "Please enter a positive integer with no more than " << MAXIMUM_DIGITS
<< " digits: ";

cin.get(change);
if (change == '\n')
cin.get(change);
while (isdigit(change) && i < MAXIMUM_DIGITS)
{
digit[i] = change;
i++;
cin.get(change);
}

size_of_A = i;
int  j = 0;
while (i > 0)
{
i--;
a[j] = digit[i] - '0';
j++;
}

for (int i = j; i < MAXIMUM_DIGITS; i++)
a[i] = 0;

}

void add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int& size_Sum)
{
int i;

for(i = 0; i < MAXIMUM_DIGITS - 1; i++);
{
sum[i] += (a[i] + b[i]) % 10;
sum[i + 1] += (a[i] + b[i]) / 10;
}
i++;
sum[i] += (a[i] + b[i]) %10;
if ((a[i] + b[i])/10 > 0)
{
cout << "There was an overflow causing more than 20 integers.\n"
<< endl;
}
}
void output_Large_Int(int a[], int size_of_A)
{

for (int i = 0; i < size_of_A; i++)
cout << a[size_of_A - i - 1];
}

```

//This is my code, I am having trouble with obtaining the size_Sum, I believe the void add() should
//work if the size_Sum could be found. Any ideas? Thank you for your help.

You have a couple of problems.
1) sum[] never gets initialized. In add() you use sum[i] += ... several places.
```    char ans;

for (int i = 0; i < MAXIMUM_DIGITS; i++)
{
a[i] = 0;
b[i] = 0;
sum[i] = 0;
}

introduction();

```

2) size_Sum never gets set.
3) in add(), for(i = 0; i < MAXIMUM_DIGITS - 1; i++); has 2 problems - a terminating semicolon, and the wrong size for the end of the loop. Should be MAXIMUM_DIGITS - 2.

### #3 jerejuana2001

Reputation: 0
• Posts: 12
• Joined: 14-November 08

## Re: Help Adding Large Numbers in reverse from individually stored arrays

Posted 16 November 2008 - 01:15 PM

Thank you for your help. I got the program up and running yesterday with a few more adjustments.