I need help to sort this numbers, but the problem is it won't like i want it to be with the decimals. I tried changing the int to double but then the program will not run. Here is the code, please provide feedback.

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

// function prototypes
void selection_sort(int input_array[], int input_size);
void display_array(int input_array[], int input_size);

int main()
{
int nums[25] = {2.345, 6.8245, 7.623467, 2345.6543, 7625.33, 24365.672,
2435.23454, 717689.651, 87984, 656.41654, 0.8546, -95654.121,
564.6541321, 12.12, 1546.4500, 0.5496, 0.123, 984.456, 2184.456,
551.5465, 555.1234, 666.4567, 777.6512, 0.0004, 0.1200};
int length = 25;

cout << "The array before the sort:\n";
display_array(nums, length);

selection_sort(nums, length);

cout << "The array after the sort:\n";
display_array(nums, length);

system("PAUSE");
return 0;
}

// Selection sort procedure. Sorts an array of ints in descending order.
void selection_sort(int input_array[], int input_size)
{
int i, j;
int small, temp;

for (i = input_size - 1; i > 0; i--)
{
small = 0;  // Initialize small to first element.

// Find the smallest element between the positions 1 and i.
for (j = 1; j <= i; j++)
{
if (input_array[j] < input_array[small])
{
small = j;
}
}
// Swap the smallest element found with element in position i.
temp = input_array[small];
input_array[small] = input_array[i];
input_array[i] = temp;
}
}

// Function that simply displays each element of input_array.
void display_array(int input_array[], int input_size)
{
int i;

for (i = 0; i < input_size; i++)
{
cout << input_array[i] << ' ';
}
cout << "\n\n";
}``````
5
Contributors
9
Replies
17
Views
8 Years
Discussion Span
Last Post by firstPerson

ok maybe you are looking for something like this study it im pretty sure its what you want to order the array in descendent order

``````for(int m=0;m<length;m++)
{
for(int j=0; j<(length-1); j++)
{
if(num2[j]<num2[j+1])
{
temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}``````

btw you should post your code in the middle of the code tags

[[/B]code[B]]code goes here[[/B]/code[B]]

where do I put this code after the return 0;
It does not seem to run, it says warning convert int to double and illegall constructor.

well now that i read your code a bit slower, you have an issue on the array, you are declaring it an int and you are imputing floating point values so that is one thing the other the code i gave you should be where you have the sorting array function

On an alternative note, there is std::sort() function.

An integer (int) has no decimals. It will never have any decimals no matter what you do with it. Instead of converting to a double start with a double because you will be able to convert doubles to floats (if you so desire), but not the other way around.

the program still won't compile

I tried changing the int to double but then the program will not run"

Thats because of your function. It accepts an int array. You need
to change the function to accept an double array.

``````void selection_sort(int input_array[], int input_size);
void display_array(int input_array[], int input_size);``````

to

``````void selection_sort(double input_array[], int input_size);
void display_array(double input_array[], int input_size);``````

And also change the function header to match the prototype.

void selection_sort(double input_array[], int input_size);
void display_array(double input_array[], int input_size);

True.
But the program still wouldn't work
This line

``int small, temp;``

small (its the index)is Ok but temp stores the value of the element in the double[] input_array, so you will always have a truncated value stored whenever the elements are swapped.
All values like 0.0004, 0.1200.. would appear as 0's.

Apart from that

``small = 0; // Initialize small to first element.``

is a pretty crude assumption ,suppose that your first element is the smallest element in the whole array, your wouldn't know, worse still your algorithm wouldn't check num[0].( i > 0 , and then j = 1)

So in all 2 changes
1) double temp; (from int temp)
2 ) int j = 0; (instead of starting from j = 1)

True.
But the program still wouldn't work

Yes thats true. I never said it would work.

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.