Programming Assignment Question [Selection Sorts using Random Numbers]

I am pretty new to programming, and I am having some minor issues with an assignment. I used my assigned book pretty heavily to write the function used for selection sorts, and I am a little unsure about some things. Basically, am I on the right track here, or what could I do differently to allow this to compile?
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
  //This program populates an array of size 100 random numbers and will print the elements of the array, perform a sort in increasing, and non-decreasing order.

#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
void selectionSort(int [], int);

int main()
{
unsigned seed=time(0);
srand(seed);
int v[100]; 
for(int i=0; i<=99; ++i){
	v[i]=rand() %1000+1;
	cout<<v[i]<<" "<<endl;
}

selectionSort(v, ???); //Not sure what would go here

void selectionSort(int array[], int size)
	{
	   int startScan, minIndex, minValue;
	   for ( startScan = 0; startScan < (size -1); startScan++)
   {
       minIndex = startScan;
       minvalue = array[startScan];
	      for (int index = startScan + 1; index < size; index++)
	       {
	          if (array[index] < minValue)              
	           {
	            minValue = array[index];
	            minIndex = index;
	           }
	       }
	      array[minIndex] = array[startScan];    
	      array[startScan] = minValue;
	   }
	} 
return 0;
}
Last edited on
replace ??? in line 19 with the size of the array.
Also, fix your braces.
Thanks for the help. I did, and it failed to compile saying my 'selectionSort' : local function definitions are illegal in line 25. As well as this error in line 29 'minvalue' : undeclared identifier.
Your code indention is really horrible! So if indenting well you'll see that selectionSort() is declared local to main(). Declaring local functions isn't allowed in C/C++. Use Ada/Pascal/... instead. Or move the declaration in front of main().
Just modified the code a bit, and I feel like I am so close.
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
#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
void selectionSort(int [], int);
void showArray(int [], int);
int main()
{
	const int SIZE=100;
	int array[SIZE];
	showArray(array, SIZE);
	selectionSort(array, SIZE);

	cout<< "The sorted values are\n";
	showArray(array,SIZE);                    //Data is not showing as sorted
	

system("pause");
return 0;
}
void showArray(int array[] , int SIZE)
{
	unsigned seed=time(0);
	srand(seed); 
	for(int i=0; i<=99; ++i)
		{
			array[i]=rand() %1000+1;
			cout<<array[i]<<" "<<endl;
		}
}
void selectionSort(int array[], int size)
	{
	   int startScan, minIndex, minValue;
	   for ( startScan = 0; startScan < (size -1); startScan++)
		{
		minIndex = startScan;
		minValue = array[startScan];
		}
	      for (int index = startScan + 1; index < size; index++)
	       {
	          if (array[index] < minValue)              
	           {
	            minValue = array[index];
	            minIndex = index;
	           }
	       }
	      array[minIndex] = array[startScan];    
	      array[startScan] = minValue;
	   }



Everything is compiling, but now, instead of displaying the sorted array, it is just outputting the same data twice.
Last edited on
I didn't check the logic of your sort function, but your showArray function doesn't do what the name implies. Every time you call your showArray function, you're filling your array with random values.
Also you should only seed your random source once at the beginning of main.
Last edited on
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
#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
void selectionSort(int [], int);
void showArray(int [], int);
int main()
{
	unsigned seed=time(0);
	srand(seed);
	const int SIZE=100;
	int array[SIZE];
	for(int i=0; i<=99; ++i)
		{
			array[i]=rand() %1000+1;
			cout<<array[i]<<" ";
		}
	
	selectionSort(array, SIZE);

	cout<< "The sorted values are: \n";
	showArray(array,SIZE);
	

system("pause");
return 0;
}

void showArray(int array[] , int size)
{
	   for (int count = 0; count < size; count++)
	          cout << array[count]<<" ";
	   cout << endl;
} 

	 
	
void selectionSort(int array[], int size)
	{
	   int startScan, minIndex, minValue;
	   for ( startScan = 0; startScan < (size -1); startScan++)
		{
		minIndex = startScan;
		minValue = array[startScan];
		}
	      for (int index = startScan + 1; index < size; index++)
	       {
	          if (array[index] < minValue)              
	           {
	            minValue = array[index];
	            minIndex = index;
	           }
	       }
	      array[minIndex] = array[startScan];    
	      array[startScan] = minValue;
	   }


I feel there might be something wrong with the selection sort. It is still displaying the same values through the showArray function.
Yes, your logic is wrong. The biggest issue would that a selection sort would require a for loop inside a for loop. Your code just has a second for loop that executes after the first for loop, not inside it.

It looks like you meant to do this judging by your indentation, so I would check your bracket placement.

EDIT: Yeah it was just a simple bracket mismatch, move the bracket at line 45 to the end and it seems to work fine!
Last edited on
Thank you so much. That worked perfectly.
Topic archived. No new replies allowed.