# Thread: entry level homework help: Passing Arrays into functions.

1. ## entry level homework help: Passing Arrays into functions.

I'm taking a entry level C++ programming course and we just started on arrays. Our assignment is to take a 20 digit number and add it along with another 20 digit number. We create the 20 digit number by creating arrays. My arrays work out but I'm stuck as to how to pass them into another function to display them. I get an "error C2664: 'DisplayResults' : cannot convert parameter 1 from 'int [20]' to 'int'" error. This is what I have so far, sorry if its rough.

Code:
```#include <iostream>
using namespace std;

void GetNumbers();
void DisplayResults(int, int, const int);

const int SIZE = 20;

void main ()
{
char FirstNumber = 0;
char SecondNumber = 0;
char Number = 0;
char Sum = 0;

char Continue;

do
{
cout << "Please enter first number ==> ";
GetNumbers();

cout << "Continue 'Y' or 'N' ==> ";
cin >> Continue;
}

while (Continue == 'Y');
}

void GetNumbers()
{
char Number;
int I, K = 0;

int FirstArray[SIZE];
int SecondArray[SIZE];
int TempArray[SIZE];

cin >> Number;

for (I = 0; I < SIZE; ++I)
{
FirstArray[I] = 0;
SecondArray[I] = 0;
TempArray[I] = 0;
}

I = 0;

while (Number != '\n' && I < SIZE)
{
TempArray[I] = Number - '0';
++I;
cin.get(Number);
}

for (K = SIZE - 1, --I; I >= 0; --I, --K)
FirstArray[K] = TempArray[I];

cout << "Please enter second number ==> ";
cin >> Number;

I = 0;

while (Number != '\n' && I < SIZE)
{
TempArray[I] = Number - '0';
++I;
cin.get(Number);
}

for (K = SIZE - 1, --I; I >= 0; --I, --K)
SecondArray[K] = TempArray[I];

DisplayResults(FirstArray, SecondArray, SIZE);

}

void DisplayResults(int FirstArray[], int SecondArray[], const int SIZE)
{
int I;

for (I = 0; I < SIZE; I++)
cout << FirstArray[I] << endl;

for (I = 0; I < SIZE; I++)
cout << SecondArray[I] << endl;

cout << "====================" << endl;
}```
I get the error trying to pass my 20 digit array into my DisplayResults function. Thanks in advance.

2. Code:
```void DisplayResults(int, int, const int);
//...
void DisplayResults(int FirstArray[], int SecondArray[], const int SIZE)```
Try changing your prototype to match how you actually implement it, as the compiler still "thinks" the function only accepts 3 separate integer parameters.

3. Awesome, thanks man. Never noticed that through all of my notes and all of my examples : / Thanks again.

4. Code:
```for (I = 0; I < SIZE; ++I)
{
FirstArray[i] = 0;
SecondArray[i] = 0;
TempArray[i] = 0;
}```
Code:
```while (Number != '\n' && I < SIZE)
{
TempArray[i] = Number - '0';
++I;
cin.get(Number);
}```
How does this even compile these should all be uppercase since 'i' is undefined?

5. Good point on the "i", I don't know that changed when I copy and pasted it. They're all capital. I am currently having a problem in my program when I add the two arrays. It displays the correct answer, and the ones carry over correctly, but I get an error message saying, "Run-Time check failure #2- Stack around variable 'SumArray' was corrupted" This is the current code

Code:
```#include <iostream>
using namespace std;

void GetNumbers();
void DisplayResults(int[], int[], int[], const int);

const int SIZE = 20;

void main ()
{
char FirstNumber = 0;
char SecondNumber = 0;
char Number = 0;
char Sum = 0;

char Continue;

do
{
cout << "Please enter first number ==> ";
GetNumbers();

cout << "\nContinue 'Y' or 'N' ==> ";
cin >> Continue;
}

while (Continue == 'Y');
}

void GetNumbers()
{
char Number;
int I, K = 0;

int FirstArray[SIZE];
int SecondArray[SIZE];
int SumArray[SIZE];
int TempArray[SIZE];

cin >> Number;

for (I = 0; I < SIZE; ++I)
{
FirstArray[I] = 0;
SecondArray[I] = 0;
TempArray[I] = 0;
}

I = 0;

while (Number != '\n' && I < SIZE)
{
TempArray[I] = Number - '0';
++I;
cin.get(Number);
}

for (K = SIZE - 1, --I; I >= 0; --I, --K)
FirstArray[K] = TempArray[I];

cout << "Please enter second number ==> ";
cin >> Number;

I = 0;

while (Number != '\n' && I < SIZE)
{
TempArray[I] = Number - '0';
++I;
cin.get(Number);
}

for (K = SIZE - 1, --I; I >= 0; --I, --K)
SecondArray[K] = TempArray[I];

}

void AddNumbers(int FirstArray[], int SecondArray[], int SumArray[], const int SIZE)
{
for (int I = 0; I <= SIZE; ++I)
{
if (FirstArray [I] + SecondArray [I] > 9)
{
SumArray[I] = (FirstArray [I] + SecondArray [I]) % 10;
SumArray[I - 1] += 1;
}

else
SumArray[I] = FirstArray [I] + SecondArray [I];
}

DisplayResults(FirstArray, SecondArray, SumArray, SIZE);

}

void DisplayResults(int FirstArray[], int SecondArray[], int SumArray[], const int SIZE)
{
int I;

for (I = 0; I < SIZE; I++)
cout << FirstArray[I];

cout << "\n";

for (I = 0; I < SIZE; I++)
cout << SecondArray[I];

cout << "\n====================" << endl;

for (I = 0; I < SIZE; I++)
cout << SumArray[I];
}```
Like I said, the program runs, but when the answer is outputted I get the run-time error. Any ideas? When I run the debugger it tells me that the Array is corrupted after it outputs it from DisplayResults.

6. Because the loop in your add function goes one too far, thus accessing and overwriting other bits of memory.

7. Tabstop you're the man. Thanks again. One last question, whats the best way to pull my arrays out of the functions so I don't have to have them run inside one another? How do I get the Arrays out so I can run the functions from main?

8. Declare the arrays inside main (and C++ has never had "void main", you need to change that to "int main"). Pass them to the Get function, pass them to the Add function, pass them to the Display function.

9. But once I return to the main from GetNumbers their values disappear. Thats why I ended up never breaking from the functions because I am unsure on how to return/reference an array.