I need help with a selection sort program

I need help on a selection sort program, I can't figure out what's wrong with my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58


#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void fillArray(int a[], int size, int numberUsed);
void sort(int a[], int numberUsed);
void swapValues(int& v1, int& v2);
int indexOfSmallest(const int a[], int startIndex, int numberUsed);

int main()
{

	int sampleArray[10], numberUsed;
	fillArray(sampleArray, 10, numberUsed);
	sort(sampleArray, numberUsed);
	
	return 0;
}
void fillArray(int a[], int size, int numberUsed) {
int index = 0;
	int i, next;
	for (i = 0; i < size; i++)
	{
		next= rand() % 1000 + 10;
		numberUsed = next;
	}
}

void sort(int a[], int numberUsed) {
	
	int indexOfNextSmallest;
	for (int index = 0; index < numberUsed - 1; index++) {
		indexOfNextSmallest = indexOfSmallest(a, index, numberUsed);
		swapValues(a[index], a[indexOfNextSmallest]);
	}
	
	
}
void swapValues(int& v1, int& v2) {
	int temp;
	temp = v1;
	v1 = v2;
	v2 = temp;
}
int indexOfSmallest(const int a[], int startIndex, int numberUsed) {
	int min = a[startIndex], 
		indexOfMin = startIndex;
	for (int index = startIndex + 1; index < numberUsed; index++)
		if (a[index] < min)
		{
			min = a[index];
			indexOfMin = index;
		}
	return indexOfMin;
}
Why do you think there's a problem with your code?

We can't read your mind. If there's a problem, you have to actually tell us what it is. Do you have a compilation error? A link error? A runtime crash? Something else?
What I need is to sort an array of random numbers and return out that array in ordered value.

My major problem seems to be the fillArray function, I think I need the numberUsed integer to plug in with my other functions but it's not.

When I ran the program I got a debug error, aside from the fillArray I can't find any problems with my code however when I commented out the fillArray function I still got debug errors.

I've spent hours on this program and logically aside from a missing cout command to dispaly the array and the trouble with the fillArray function it should be in good working order but its not.

I'm trying to figure out how to fix the fillArray function and also find out where the trouble spot is that is generating a debug error so that the program can run normally return a sorted array and display it.
Last edited on
My major problem seems to be the fillArray function, I think I need the numberUsed integer to plug in with my other functions but it's not.

You're passing numberUsed into the function by value. That means that the variable in the function is a local copy of the one in the calling code. Whatever changes your function makes to the local copy, will not change the variable in the calling code.

Instead, you need to pass by reference. Or you could have your function return the number.

When I ran the program I got a debug error

The best way to investigate this would be to step through the code in a debugger. This will show you exactly where the error is occurring, and will allow you to investigate the values in your variables at every step.

I'll note that your fillArray function doesn't do what it says it does. At no point does it put anything into the array. In fact, the array isn't used at all, anywhere in the function.
Last edited on
I tried using by reference, its better but now I'm only get one number I assume the first number of the array. I can't figure how how to pass the whole array out of the function and store it in main so that the other functions can use it.
Well, yes, an int stores one number. If you want your function to pass back a collection of numbers, then you'll need to use an appropriate type for the argument (e.g. an array, or a vector, or something like that).

The number you're getting back is not any number from the array, because, as I said, you don't actually put any data into your array in the fillArray function.

The number you're getting back is the random number generated on the final iteration of your loop - obviously, because that's the last time a value was assigned to that variable.

What is that you want to be stored in numberUsed? In your fillArray function, you're storing a random number in that variable. But elsewhere, it looks as though you want to use it as if it was the size of your array. You need to think clearly about what it is you want that variable to be used for.
Last edited on
I might just have to admit that it would be easier just to write a new program possibly, I haven't quite mastered arrays yet and I haven't found sufficient material online on selection sort to understand it.

So I was wondering if maybe you might know how to create a program with a selection sort function and an array with randomly generated integers that checks to make sure that there is only one occurrence of a number in the array. It would be great to have it online for other people struggling with a lack of information on the subject.
closed account (D80DSL3A)
Let's correct the fill array function, then your solution may advance.
The array is passed to the function so the random values being generated in the for loop could be written to the array, it just isn't being done.
There seems to be no reason at all for numberUsed, so be rid of it. Try this:
1
2
3
4
5
6
7
8
9
10
void fillArray(int a[], int size)//, int numberUsed) { <- uneeded parameter
//int index = 0; this is unused too
	int i;//, next; <-  no need for next either
	for (i = 0; i < size; i++)
	{
                a[i] = rand() % 1000 + 10;
	//	next= rand() % 1000 + 10;
	//	numberUsed = next;
	}
}

which cleans up nicely to:
1
2
3
4
void fillArray(int a[], int size)
	for (int i = 0; i < size; i++)	
                a[i] = rand() % 1000 + 10;	
}

See if that gets you going.
Thank you very much. I'm still having problems with it having a debug error and then crashing and nothing displays to the screen, there's no other problems with it though. I get the feeling it's something simple I'm overlooking. I have re-posted updated code.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void fillArray(int a[], int size);
void sort(int a[], int numberUsed);
void swapValues(int& v1, int& v2);
int indexOfSmallest(const int a[], int startIndex, int numberUsed);
void printArray(int a[]);
int main()
{
	
	int sampleArray[10], numberUsed,a;
	fillArray(sampleArray, 10);
	sort(sampleArray, numberUsed);
	printArray(sampleArray);
	

	return 0;
}
	void fillArray(int a[], int size){
		for (int i = 0; i < size; i++)
			a[i] = rand() % 1000 + 10;
		
}


void sort(int a[], int numberUsed) {
	
	int indexOfNextSmallest;
	for (int index = 0; index < numberUsed - 1; index++) {
		indexOfNextSmallest = indexOfSmallest(a, index, numberUsed);
		swapValues(a[index], a[indexOfNextSmallest]);
	}
	
	
}
void swapValues(int& v1, int& v2) {
	int temp;
	temp = v1;
	v1 = v2;
	v2 = temp;
}
int indexOfSmallest(const int a[], int startIndex, int numberUsed) {
	int min = a[startIndex], 
		indexOfMin = startIndex;
	for (int index = startIndex + 1; index < numberUsed; index++)
		if (a[index] < min)
		{
			min = a[index];
			indexOfMin = index;
		}
	return indexOfMin;
}
void printArray(int a[]) {
	int i;
	cout << a[i];
}
The best way to diagnose this would be to run it through a debugger. That way, you'll be able to see exactly where it's crashing, and what the state of the memory is at that point.

One thing that's easy to see (because it's at the very bottom of your code):

Look at line 58. Which element of your array do you think will be printed out here?
Topic archived. No new replies allowed.