I'm trying to write a function that uses pointer arithmetic to ad the contents of two int arrays, putting the sums in a third array. Everything seems to fuction, except that it spits out zeros every time.
#include <iostream>
usingnamespace std;
void AddArrays(int arrayOne[], int arrayTwo[], int arrayThree[], int arraySize);
int main()
{
int arrayOne[50];
int arrayTwo[50];
int arrayThree[50];
int arraySize;
cout << "\nEnter the array size: ";
cin >> arraySize;
for (int i = 0; i < arraySize; i++)
{
cout << "Enter int " << i+1 << " for Array 1: ";
cin >> arrayOne[i];
}
cout << endl;
for (int j=0; j < arraySize; j++)
{
cout << "Enter int " << j+1 << " for Array 2: ";
cin >> arrayTwo[j];
}
AddArrays(arrayOne, arrayTwo, arrayThree, arraySize);
for (int k = 0; k < arraySize; k++)
{
cout << endl;
cout << arrayThree[k] << endl;
}
return 0;
}
void AddArrays(int arrayOne[], int arrayTwo[], int arrayThree[], int arraySize)
{
int *pointer1; //pointer to array one
pointer1 = arrayOne;
int *pointer2; //pointer to array two
pointer2 = arrayTwo;
int *pointer3; //pointer to array three
pointer3 = arrayThree;
for(int x; x < arraySize; x++)
{
*pointer3 = (*pointer1 + *pointer2);
*pointer1++;
*pointer2++;
*pointer3++;
}
}
Enter the array size: 3
Enter int 1 for Array 1: 1
Enter int 2 for Array 1: 2
Enter int 3 for Array 1: 3
Enter int 1 for Array 2: 1
Enter int 2 for Array 2: 2
Enter int 3 for Array 2: 3
0
0
0