have been working on a program that should output the values of an array from lowest to highest order, my program runs but its ouputing -1.#IND which is clearly not right. Im pretty sure im messing up somewhere when i call back to my my function with selectionSort(array, N) but im not 100% sure. any help or suggestions would be appreciated. (code thus far).

``````#include <iostream>
#include <cmath>
using namespace std;

int main(){
double selectionSort(int a[], int size);
const int N = 8;
int array[N] = { 1, 3, 9, 0, 74, 36, 45, 2};
double results;

for( int i(0); i < N; i++){

results = array[i];
}

cout << "The order of the Array is: " << selectionSort(array, N) << endl;

system("PAUSE");
return 0;
}

void selectionSort(int a[], int size){
int m;
double hold;

for (int k=0; k<=size-2; k++){
m = k;
for(int j=k+1; j<size-1; ++j){
if (a[j] < a[m])
m=j;
}
hold = a[m];
a[m] = a[k];
a[k] = hold;
}
return;
}``````
4
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by tKc

I'm confused by your use of double on the function prototype (which is ok where it is, but is usually placed before main instead of on line 6) and void in the definition. You seem to be expecting it to return a double (again, not sure why you want to output a single value) when you output on line 16. Why is hold a double when all the members of a are integers.

Try making those fixes and then if nothing takes post back. I didn't run your program, so I don't know if your sort works properly.

running your program on g++ I get a return of NAN, indicating compiler treats the returning value as "Not a Number"... I'd check your array of ints... the context of your code indicates you really want to work with doubles. You aren't going to be able to cast to double like that...

First, the prototype for selectionSort should match the definition EXACTLY, always. So, this will rectify it:

``void selectionSort(int a[], int size); //and you should place it outside and before main()``

The reason why the compiler doesn't care is because the compiler won't see a difference between two function prototypes if only the result type differs. But at run-time, the resulting value will be garbage (NaN, or any "random" number).

Second, this piece of code:

``````double results;

for( int i(0); i < N; i++){

results = array[i];
}``````

has absolutely no effect what-so-ever. Was there something you expected this snippet of code to accomplish? If yes, tell us, so that we can clarify it for you.

Finally, your selection sort algorithm seems totally fine, but notice that what you are doing is overwriting the values in the array called "array" that is passed to the function, so the final resulting array can be printed to the screen by looping in that array and printing each value. So, if you thought that printing the result of selectionSort was going to print the array in the correct order, you were mistaken.

okay, after taking in your comments i have been able to reach a code that seems to work but is still up to whatever criticism you guys got. and mike your were right about my initial result array loop doing nothing, not sure why i had that. also i dont know what i was thinking when i was putting ints and doubles whenever seemed right

``````#include <iostream>
#include <cmath>
using namespace std;

void selectionSort(int a[], int size);
const int N = 8;

int main(){

int array[N] = { 1, 3, 9, 0, 74, 36, 45, 2};

selectionSort(array, N);

cout << "The order of the Array is: \n";
for (int i=0; i<N; i++)
cout << array[i] << endl;

system("PAUSE");
return 0;
}

void selectionSort(int a[], int size){

int m, hold;

for (int k=0; k<=size-1; k++){
m = k;
for(int j=k+1; j<size; j++){
if (a[j] < a[m]){
m=j;
}
}
hold = a[m];
a[m] = a[k];
a[k] = hold;
}
}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.