- #1
Saladsamurai
- 3,020
- 7
So I have a simple selection sort function written. It takes an array of type double as the array to be sorted. If I declare the array to be of type int in my main program and then pass it to my sSort function, it gets mad. There must be a way that programmers deal with this kind fo stuff.
Here is the code if needed:
Here is the code if needed:
Code:
/* SORTING ALGORITHMS: Etter Chapter6_5 p281 */
/*----------------------------------------------------
This is a driver program to test the sorting
functions below.
----------------------------------------------------*/
#include <iostream>
#include <iomanip>
using namespace std;
// funtion prototype
void sSort(double sortableArray[], int numElements);
// driver program
int main () {
// declare objects
double x[11] = {1.1,1.0,1.5,1.4,1.3,1.8,1.9,1.2,1.7,1.6,2.0};
int n(11);
// set format flags for standard output
cout.setf(ios::fixed); //decimal notation
cout.precision(2); // out to 2 places
// display initial state of x[]
cout << "********************" << endl;
cout << "Unsorted: \n";
for (int i=0;i<=n-1;++i)
{
cout << x[i] << endl;
}
// call sorting algorithm Select Sort
sSort(x,n);
// display final state of x[]
cout << "********************" << endl;
cout << "Sorted: \n";
for (int i=0;i<=n-1;++i)
{
cout << x[i] << endl;
}
return 0;
}
/*----------------------------------------------------
SELECTION SORT: this function sorts an array with n
elements into ascending order
----------------------------------------------------*/
void sSort(double x[], int n)
{
// declare local objects
int m;
double hold;
// implement selection sort algorithm
for (int k=0;k<=n-2;++k)
{
// find position of smallest value in array
// beginning at k
m=k;
for (int j=k+1;j<=n-1;++j)
{
if (x[j]<x[m])
m = j;
}
// exchange smallest value with value at k
hold = x[m];
x[m] = x[k];
x[k] = hold;
}
// void return
return;
}
/*---------------------------------------------------*/