I just started learning how to use pointer array and i'm trying out examples to know more about them but i get confused with them easily. So, I tried to do a simple arithmetic calculation using a pointer array.
The user will input 2 string variables for addition. I then reverse the string variable.
int i = 0;
int j;
char temp;
j = s1.length();
while (i < j)
{
temp = s1[i];
s1[i] = s1[j];
s1[j] = temp;
i++;
j--;
}
I tried to convert 2 string variables i have into a integer pointer array before adding them together
void stringtoInt(string& s1, string& s2)
{
int size1 = s1.size();
int size2 = s2.size();
intPtr intArray;
intPtr intArray2;
intArray = new int[size1];
intArray2 = new int[size2];
for (int i = 0; i < size1; i++)
{
intArray[i] = s1[i] - '0';
}
for (int i = 0; i < size2; i++)
{
intArray2[i] = s1[i] - '0';
}
addInteger(intArray, intArray2);
}
I passed in 2 integer array in a function and tried to carry some addition calculation
void addInteger(intPtr intArray, intPtr intArray2)
{
int *p = intArray;
int *q = intArray2;
intPtr total = new int[MAX];
int i = 0;
int sum = 0;
int carried = 0;
while (*p != '\0' || *q != '\0')
{
*(total + i) = *p + *q + carried;
if(*(total + i) > 9)
{
*(total + i) %= 10;
carried = 1;
}
else
carried = 0;
i++;
p++;
q++;
}
printArray(total, i);
}
And this is how i read my pointer array
void printArray(intPtr total, int size)
{
for(int i = 0; i < size; i++)
cout << *(total + i);
}
After trying to debug this pointer integer array, i still can't figure out what is wrong with them and i just started learning how to use pointer integer array.