0

I keep getting a corruption for my array and I do not know why. Can anyone help me figure out what is causing my run-time error to happen?

my heapInsert function is at the very bottom

```
#include <iostream>
#include <algorithm>
using namespace std;
//function prototypes
int getParent(int index);
int getLeftChild(int index);
int getRightChild(int index);
void swap(int& item1, int& item2);
void heapify(int A[], int index, int size);
void build_heap(int A[], int size);
void printArray(int A[], int size);
void heapInsert(int A[], int &size, int Item);
int main()
{
int A[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
int size = sizeof A/sizeof(int);
cout << "Print Array A: " << endl;
printArray(A, size);
cout << endl;
cout << "Parent of node 5" << " is the node " << getParent(5) << endl;
cout << "Left child of the node 3" << " is the node " << getLeftChild(3) << endl;
cout << "Right child of the node 3" << " is the node " << getRightChild(3) << endl;
cout << "Print Heap A:" << endl;
build_heap(A, size);
printArray(A, size);
heapInsert(A, size, 20);
cout << "\nAfter inserting the number into the heap A:" << endl;
build_heap(A, size);
printArray(A, size);
return 0;
}
void printArray(int A[], int size)
{
if(size == 0)
{
return;
}
else
{
printArray(A, size -1);
cout << A[size - 1] << " ";
}
}
int getParent(int index)
{
return(index/2);
}
int getLeftChild(int index)
{
return(index * 2 + 1);
}
int getRightChild(int index)
{
return(index * 2 + 2);
}
void swap(int &item1, int &item2)
{
int temp = item1;
item1 = item2;
item2 = temp;
}
void heapify(int A[], int index, int size)
{
int left = getLeftChild(index);
int right = getRightChild(index);
int largest;
if(left < size && A[left] > A[index])
{
largest = left;
}
else
{
largest = index;
}
if(right < size && A[right] > A[largest])
{
largest = right;
}
if(largest != index)
{
swap(A[index], A[largest]);
heapify(A, largest, size);
}
}
void build_heap(int A[], int size)
{
for(int i = size/2; i >= 0; i--)
{
heapify(A, i, size);
}
}
void heapInsert(int A[], int &size, int Item)
{
size = size + 1;
int i = (size - 1);
while(i > 0 && A[getParent(i)] < Item)
{
cout << A[i] << endl;
A[i] = A[getParent(i)];
i = getParent(i);
A[i] = Item;
}
cout << A[i];
//i = getParent(i);
//A[i] = Item;
}
```