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?
//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>
usingnamespace 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;
}
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().
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.
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!