Hello. I'm supposed to store the two numbers in two int arrays of size 30, one digit per array element. If the number is less than 30 in length, enter enough leading zeroes (to the left) to make number 30 digits long. I will need a loop to add the digits in corresponding array elements and carry digits if needed.

The first digit will be read into A, second in A and so on until I hit end of line. Then, here's where it get complicated. My teacher said I should make sure the total number of digits read in is less than 30 (I thought the numbers should be up to 30). After I read in the list, I should push the list down so that the last digit is in A and the second last in A and so on. How do I do that?

Afterwhich, I will add the corresponding digits from both arrays: A to B etc. I need to carry numbers if the sum is greater than 9. I'm confused on what to do....here's what I have so far.

``````//File: lab8b.cpp
/*Purpose: to read two integers up to 30 digits each,
add them together, and display the result*/

#include <iostream>
#include <conio.h>
#define SIZE 30
using namespace std;

void inputValue1(long int[]);
void inputValue2(long int[]);
void Sum(long int[]);

void main()
{
long int A[SIZE];
long int B[SIZE];
long int C[SIZE];

inputValue1(A); //function call
inputValue2(B); //function call
Sum(C); //function call

getch();
}

void inputValue1(long int A[])
{
cout << "Enter first integer (30-digit): ";
for(int i=0; i<SIZE; i++)
cin >> A[i];
}

void inputValue2(long int B[])
{
cout << "Enter second integer (30-digit): ";
for(int i=0; i<SIZE; i++)
cin >> B[i];
}

void Sum(long int C[])
{
long int i;
long int A,B;
for(i=0; i<SIZE; i++)
C[i] = A[i] + B[i];
cout << "The sum of these two integers is: " << C[i];
}``````

You don't need to make the arrays consist of longs, depending on your implementation and whether your computer is 32/64 bit it may be the same size as an int anyway. Really, you only need a datatype that will hold one digit per index of the array, but stick with the ints.

You will probably have to have the user input the digits with a space in between, as cin uses space as a delimiter.

In terms of the shifting, say your user enters 12. A contains 1 and A contains 2. You need to shift A down to A and A down to A (so count the number of digits and shift everything to right by total digits - digits in number.

No, your upper limit on digits is 29 so you can have a carry into the final digit. Your sum function doesn't take into account this carrying that must go on at each digit.

If you enter the number 2651, they will fill
A=2
A=6
A=5
A=1
You just need to move the values so you have
A to A=0
A=0
A=2
A=6
A=5
A=1
and so on. 2 simple loops and a little thought will help you accomplish that. Look at the pattern above.

okay thanks ill give it a try

and jonsca, how would i go abouts changing ti from 0 to 29?

and jonsca, how would i go abouts changing ti from 0 to 29?

Your input loops currently run to 30, make them only run until 29 (it takes just a small change)

these are the changes ive made, it didnt work :/

``````//File: lab8b.cpp
/*Purpose: to read two integers up to 30 digits each,
add them together, and display the result*/

#include <iostream>
#include <conio.h>
#define SIZE 29
using namespace std;

void inputValue1(long int[]);
void inputValue2(long int[]);
void Sum(long int[]);

void main()
{
long int A[SIZE];
long int B[SIZE];
long int C[SIZE];

inputValue1(A); //function call
inputValue2(B); //function call
Sum(C); //function call

getch();
}

void inputValue1(long int A[])
{
cout << "Enter first integer (30-digit): ";
for(int i=0; i<SIZE; i++)
cin >> A[i];
}

void inputValue2(long int B[])
{
cout << "Enter second integer (30-digit): ";
for(int i=0; i<SIZE; i++)
cin >> B[i];
}

void Sum(long int C[])
{
long int i;
long int A,B;
for(i=0; i<SIZE; i++)
C[i] = A[i] + B[i];
cout << "The sum of these two integers is: " << C[i];
}``````

Instead of changing your size constant, why not run the loops on lines 30 and 37 to SIZE - 1? (your Sum function is going to have to be changed all around to account for the shifting and carries, so don't worry about that one for right now)

hey, i tried all the suggestions uv made but none have worked so far, i dont know what im doing wrong :/

``````//File: lab8b.cpp
/*Purpose: to read two integers up to 30 digits each,
add them together, and display the result*/

#include <iostream>
#include <conio.h>
#define SIZE 29
using namespace std;

void inputValue1(long int[]);
void inputValue2(long int[]);
void Sum(long int[]);

void main()
{
long int A[SIZE];
long int B[SIZE];
long int C[SIZE];

inputValue1(A); //function call
inputValue2(B); //function call
Sum(C); //function call

getch();
}

void inputValue1(long int A[])
{
cout << "Enter first integer (30-digit): ";
for(int i=-1; i<SIZE; i++)
cin >> A[i];
}

void inputValue2(long int B[])
{
cout << "Enter second integer (30-digit): ";
for(int i=-1; i<SIZE; i++)
cin >> B[i];
}

void Sum(long int C[])
{
long int i;
long int A,B;
for(i=0; i<SIZE; i++)
C[i] = A[i] + B[i];
cout << "The sum of these two integers is: " << C[i];
}``````

No, you had it right going from 0. I meant leave SIZE = 30 and have the loop go to SIZE - 1 instead of SIZE. There is no -1 position in the array. Think about it, 0 to SIZE - 1 (provided SIZE = 30) will give you indexes 0 to 28, which you can shift down to indexes 1 to 29 as the assignment prescribes.

Write down (pencil and paper style) the steps it would take you to accomplish this and draw out the array. It will be helpful.

okay i understand what you are saying, but I tried that, and when I ran it it still doesn't give me the right numbers. When I input my first number it skips by my second and says the sum is -8656465something

I would hold off on the sum function for now. I had told you that you are summing 2 arrays that are uninitialized so there's going to be garbage in those slots in A and B.

Work on getting the input in and shifting it down (and "padding" with the appropriate number of zeros. Hint, keep track of how many numbers the user entered and have them type 999 to finish (otherwise they have to enter 29 numbers each time).

doesn't this version of the program add the numbers, and their digits, correctly only if the arrays are the same size, that is, consisting of the same number of digits? Since, I'm thinking the array fills from 0 to SIZE, which is done left to right, as entered. So I guess what i'm saying is it's adding left to right, according to the array entry, and not right to left which is the correct method for summing. A consists of 12345 and B is 678, wouldn't the result, according to the way the arrays are filled up and added column-by-column, be 80145, since it's filling the en-entered B and B with zeros, hence the addition it sees is 12345+67800=80145, and not the intended 12345+678=13023. Just a thought...

Your variables are most likely going out of range.

There a few ways to correct this. One would be to have the functions return the values to plug in, such as:

``````//in main
for (int i = 0; i != SIZE - 1; i++)
A[i] = InputValue1();

//function
int somevariable;
cin >> somevariable;
return somevariable;``````

By the way, you probably want to make your arrays

``short``

since they'll only contain 0-9.

doesn't this version of the program add the numbers, and their digits, correctly only if the arrays are the same size, that is, consisting of the same number of digits? Since, I'm thinking the array fills from 0 to SIZE, which is done left to right, as entered. So I guess what i'm saying is it's adding left to right, according to the array entry, and not right to left which is the correct method for summing. A consists of 12345 and B is 678, wouldn't the result, according to the way the arrays are filled up and added column-by-column, be 80145, since it's filling the en-entered B and B with zeros, hence the addition it sees is 12345+67800=80145, and not the intended 12345+678=13023. Just a thought...

As a programmer, write the program to correct for this. Once entered, adjust the numbers to the right in the arrays.

Here are a few things you might have overlooked :

When you write

``long int A;``

what you are doing is to create an array of 29 long int. Your variable A is a pointer to the beginning of this array. If you write

``cout << *A;``

you would print on the console the first long int of the Array. But it's more convenient to access the array using indices, so writing A. The indices start at 0. As you defined 29 elements of this array, and if the first as an index of 0, the last one will have and index od 28 (so the size of your array - 1).

You said you want to create arrays of 30 elements, so you would write long int A; but you would access them from A, ..., A. As said above, why long int ? Each element in the array will hold only one digit. An simple int will do just fine. :)

You seem to overlook the scope of your variables, and how you pass them to functions. If you define a variable in your main() function, it will only exist there, unless you pass it to another function. But by default you pass it to a function by value. That means that the function will make a local copy of whatever you pass, but will not affect your variable from main() in any way. To allow another function to manipulate a variable from your main, you should pass it by reference. You will find plenty of tutorial on this topic (pass-by-value, pass-by-reference, pass-by-pointer).