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!

Edited 5 Years Ago by WaltP: Added CODE Tags - please use them.

Please use the CODE tags when posting code, it makes it much easier to read.

Also, please post your whole code.

Edited 3 Years Ago by happygeek: fixed formatting

#include <iostream> 
using namespace std; 
const int arraySize = 10; //Global Constant
void InsertIntoArray (int [], int, int); //Function Prototype
void OutputArray (int [], int); //Function Prototype

int main()
{
	int myArray [arraySize];
	int input = 0;
	int p = 0;
	int x = 0; 
	for (x = 0; x < 10; x++)
	{ 
		cout << "Please enter a positive integer (or -1 to quit): " << endl; 
		cin >> input; 
		if (input == -1)
		{
			break; 
		}  
		InsertIntoArray (myArray, input, p);
	} 
	OutputArray (myArray, x);  
	system( "pause" ); 
return 0; 
}
void InsertIntoArray (int myArray [], int x, int HowMany) 
{
	if (HowMany == 0) 
	{	
		myArray [0] = x;
		return;
	} 
	if (x > myArray [HowMany - 1]) //meaning that this is the biggest entry yet
	{
		myArray [HowMany] = x; 
		return;
	}
	int insertPoint = -1;
	for (int m = 0; m < HowMany; m++) 
	{
		if (x < myArray [m]) 
		{
			insertPoint = m; 
			break; //it's been found!
		}
	} 
	for (int m = HowMany - 1; m >= insertPoint; m--)
	{
		myArray [m + 1] = myArray [m]; //this is moving the entry to the right
	} 
	myArray [insertPoint] = x; //this is putting it in the proper place
} 

void OutputArray (int SortedArray[], int Printer)
{
	for (int z = 0; z < Printer; z++) 
	{ 
		cout << z << "......" << SortedArray [z] << endl;
	} 
}

Ah, now we're getting somewhere.

I believe you're missing something. What is the variable "p" for and what are you using it for? Do you even need it at all or do you already have a variable that can fulfill its purpose?

Edited 5 Years Ago by Red Goose: n/a

What do you think I should do???

I think you should examine the variable "p" in int main() and what exactly it's doing.

Or, at least, explain to me step-by-step what it's doing in your code.

Edited 5 Years Ago by Red Goose: n/a

Ok so I see that p really isn't used anywhere and therefore not necessary but how does that help with the insertintoarray function? When I tried to run it without p I was missing a parameter for the function and the myarray was still corrupted.

Any ideas?

Ok so I see that p really isn't used anywhere and therefore not necessary but how does that help with the insertintoarray function? When I tried to run it without p I was missing a parameter for the function and the myarray was still corrupted.

Any ideas?

You wrote the code, what was p SUPPOSED to do? You clearly needed it for SOMETHING. The functionality exists, it's against the spirit of this forum for me to hand you the answer on a silver platter. I want you to step back and breathe. Look at what you expected p to do and why it wasn't doing it. Then, look at what you can do to apply that functionality. Just deleting the variable won't solve the problem.

Edited 5 Years Ago by Red Goose: n/a

I'm so sorry...thanks for your patience I was writing this at two different times and didn't realize I hadn't checked to see if all my variables matched up with one another

so now that the problem with p is gone I still come up with an error message with the corrupted array only sometimes when I run it

why would it do that and what does it mean?

I'm so sorry...thanks for your patience I was writing this at two different times and didn't realize I hadn't checked to see if all my variables matched up with one another

so now that the problem with p is gone I still come up with an error message with the corrupted array only sometimes when I run it

why would it do that and what does it mean?

Could you give an example input of a time that it gives you the error?

That's really odd...I tried 12, 12, 12, 12, 13, 14, 15, 16, 17, 11 one time and it didn't work

The next time I tried that same sequence and it worked and sorted fine...I guess a few tweaks with the p variable problem changed that

Thanks so for your help!

This article has been dead for over six months. Start a new discussion instead.