Hi. I was wondering if someone could help me out with a project.
The assignment says to call a void function called InsertIntoArray passing the array, the entry the user entered and place the positive integer into the correct element of the array so that the array is always in ascending order. Change entries around inside the function so that all your entries are properly ordered. Cannot solve this problem by sorting the array after all the entries are entered!
Here is what I have so far.
The problem I keep having is that when I run it a message pops up saying the myArray is corrupted, but I don't know how to fix this.
void InsertIntoArray (int myArray [], int x, int HowMuch)
{
if (HowMuch == 0)
{
myArray [0] = x;
return;
}
if (x > myArray [HowMuch - 1])
{
myArray [HowMuch] = x;
return;
}
int insertPoint = -1;
for (int m = 0; m < HowMuch; m++)
{
if (x < myArray [m])
{
insertPoint = m;
break;
}
}
for (int m = HowMuch - 1; m >= insertPoint; m--)
{
myArray [m + 1] = myArray [m];
}
myArray [insertPoint] = x;
}
Thanks so much!