Hey, I need some help inserting into a function and sorting the 10 user input numbers, I'm close, but I get zeros every time.

Here is my code:

#include <iostream.h>

void InsertIntoArray (int[], int, int, int);
int whichSlot (int[], int, int);

int main()
{
	int num;
	int times;
	const int arraysize=10;
	int array[arraysize] = {0};
	times = 0;

	while (times <= 10) //Ask for another number when there are less than 10 numbers in the array
	{

		cout << "Please enter your number!" << endl;

		cin >> num;

		times++;

		int result = whichSlot(array, num, times);
	
		InsertIntoArray(array, num, times, result);

	
		

	}

	for ( int j = 0; j < arraysize; j++ )   
	{
		cout << array[ j ] << endl;
	}
	return 0;
}

int whichSlot (int array[], int num, int times)
{
	
		if (times == 0)
		{
			return 0;
		}

		if (num >= array[times-1])
		{
			return times;
		}

		for (int i=0; i<times; i++)
		{
			if (num <= array[i])
			{
				return i;
			}
		}


}
	
void InsertIntoArray (int array[], int num, int times, int result)
{
	num = array[result];
	return;
}

Please...I need help immediately!! I changes some things, and I'm so close now!

#include <iostream.h>

void InsertIntoArray (int[], int, int, int);
int whichSlot (int[], int, int);

int main()
{
	int num;
	int times;
	const int arraysize=10;
	int array[arraysize] ;
	times = 0;
	int result=0;

	while (times <= 9) //Ask for another number when there are less than 10 numbers in the array
	{

		cout << "Please enter your number!" << endl;

		cin >> num;

		times++;

		int result = whichSlot(array, num, times);
	
		InsertIntoArray(array, num, times, result);

	
		

	}

	for ( int j = 0; j < arraysize; j++ )   
	{
		cout << array[ j ] << endl;
	}
	return 0;
}

int whichSlot (int array[], int num, int times)
{
	
		if (times == 1)
		{
			return 0;
		}

			if (num >= array[times-2])
			{
				return times;
			}

				for (int i=0; i<times; i++)
				{
					if (num <= array[i])
					{
						return i;
					}
		}


}
	
void InsertIntoArray (int array[], int num, int times, int result)
{
	array[result] = num;
	return;
}
commented: patience is a virtue young one +0

It's only been 25-mins. This is a forum, you can't expect immediate help. Consider yourself lucky I'm even posting.

As far as your code goes, WTF are you trying to accomplish? All I'm seeing is a bunch of screwing around to assign an input to an array element. Why not just run a loop through 10 iterations and assign the input to the specific element based on the current value of the iteration control variable.

for(int i=0; i<10; i++) {
int inputVar;

/* get your input */
cin >> inputVar;

/* assign it to your array */
array[i] = inputVar;

/* any other actions that repeat*/

}

/* anything else you may need to do */

Once you have all the numbers just use a standard sorting algorithm. A bubble sort should be more than sufficient for 10 elements.

You don't need all that other bounce around calculate this and that crap.

I need to fix the following errors and then I think I have it...

\My Workspace\project sort\main.cpp(70) : error C2059: syntax error : ')'
My Workspace\project sort\main.cpp(71) : error C2143: syntax error : missing ';' before '{'
My Workspace\project sort\main.cpp(75) : error C2143: syntax error : missing ';' before '}'

All the errors are in the following code:

void InsertIntoArray (int array[], int num, int times, int result)
{
	for ( int j = 10; j > result; j--- )
	{
		while (j != 10)
		{
			array[j] = array[j+1]
		}
	}
	array[result] = num;
	return;
}

It's only been 25-mins. This is a forum, you can't expect immediate help. Consider yourself lucky I'm even posting.

As far as your code goes, WTF are you trying to accomplish? All I'm seeing is a bunch of screwing around to assign an input to an array element. Why not just run a loop through 10 iterations and assign the input to the specific element based on the current value of the iteration control variable.

for(int i=0; i<10; i++) {
int inputVar;

/* get your input */
cin >> inputVar;

/* assign it to your array */
array[i] = inputVar;

/* any other actions that repeat*/

}

/* anything else you may need to do */

Once you have all the numbers just use a standard sorting algorithm. A bubble sort should be more than sufficient for 10 elements.

You don't need all that other bounce around calculate this and that crap.

@ Fbody, we have to use a functions... I think I am close to having it sorted and outputted correctly, I just have to fix some errors...See my post.

\My Workspace\project sort\main.cpp(70) : error C2059: syntax error : ')'
My Workspace\project sort\main.cpp(71) : error C2143: syntax error : missing ';' before '{'
My Workspace\project sort\main.cpp(75) : error C2143: syntax error : missing ';' before '}'

These are all pretty obvious... check your punctuation.

EDIT:
There is using a function, then there is using a function effectively. If you need a function, create an input function that contains the loop, then create a sort function that does the sort. Pretty simple... Much simpler than whatever you're doing.

WTF are you trying to accomplish? All I'm seeing is a bunch of screwing around to assign an input to an array element. Why not just run a loop through 10 iterations and assign the input to the specific element based on the current value of the iteration control variable.

for(int i=0; i<10; i++) {
int inputVar;

/* get your input */
cin >> inputVar;

/* assign it to your array */
array[i] = inputVar;
}

You don't need all that other bounce around calculate this and that crap.

I imagine it's supposed to be an insertion sort (not positive). Your code doesn't sort anything (unless the insertion part is intended to be placed inside the comments?).


To the OP, to reiterate Fbody's point, it hasn't been that long. You're not being ignored. You can't expect instant gratification.

As to the code itself, you've fixed line 66, but there are still problems. Even assuming whichSlot returns the correct place, if you are doing an insertion sort, you need to change more than one element in the array when you insert unless you are inserting into the LAST elemnt. Otherwise, you are simply overwriting an element. However, with the variable names you are using, it's not clear what you are trying to do. I'm only guessing it is an insertion sort.

[EDIT]
Lots of posts since I first started proofreading. This post could be obsolete by now!
[/EDIT]

I can't find it...this is my first year doing c++. Could you fix em for me?

I imagine it's supposed to be an insertion sort (not positive). Your code doesn't sort anything (unless the insertion part is intended to be placed inside the comments?).


To the OP, to reiterate Fbody's point, it hasn't been that long. You're not being ignored. You can't expect instant gratification.

As to the code itself, you've fixed line 66, but there are still problems. Even assuming whichSlot returns the correct place, if you are doing an insertion sort, you need to change more than one element in the array when you insert unless you are inserting into the LAST elemnt. Otherwise, you are simply overwriting an element. However, with the variable names you are using, it's not clear what you are trying to do. I'm only guessing it is an insertion sort.

Yeah, I changed my INsertINtoArray function...look at my previous post.

I can't find it...this is my first year doing c++. Could you fix em for me?

Patience. We're posting on top of each other. Debugging takes time. Give it a shot and report back if you are stuck. It appears you have more variables than needed. Rename them so people (and you) are clear on what they represent.

I am stuck...I can't seem to work out these errors.

I am stuck...I can't seem to work out these errors.

You aren't stuck. You simply haven't put in the time. Your PM that it was due in ten minutes shows that. Take the F and chalk it up as a learning experience. You need to expect to put in several hours when you are beginning. Start the assignment way sooner.

I've been working at it for weeks. At this point I just want to at least figure it out. I can't seem to find what is wrong and why I'm getting the errors.

Have you tried googling those errors? Most of the errors are simple enough to understand. If there is an error, most probably it's a syntactical error. Check your semi-colons, brackets, etc. After that you can try figuring out the logical errors you have.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.