943,800 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2412
  • C++ RSS
Aug 16th, 2009
0

c++ sorting array

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. // function prototypes
  5. void selection_sort(int input_array[], int input_size);
  6. void display_array(int input_array[], int input_size);
  7.  
  8.  
  9. int main()
  10. {
  11. int nums[25] = {2.345, 6.8245, 7.623467, 2345.6543, 7625.33, 24365.672,
  12. 2435.23454, 717689.651, 87984, 656.41654, 0.8546, -95654.121,
  13. 564.6541321, 12.12, 1546.4500, 0.5496, 0.123, 984.456, 2184.456,
  14. 551.5465, 555.1234, 666.4567, 777.6512, 0.0004, 0.1200};
  15. int length = 25;
  16.  
  17. cout << "The array before the sort:\n";
  18. display_array(nums, length);
  19.  
  20. selection_sort(nums, length);
  21.  
  22. cout << "The array after the sort:\n";
  23. display_array(nums, length);
  24.  
  25. system("PAUSE");
  26. return 0;
  27. }
  28.  
  29. // Selection sort procedure. Sorts an array of ints in descending order.
  30. void selection_sort(int input_array[], int input_size)
  31. {
  32. int i, j;
  33. int small, temp;
  34.  
  35. for (i = input_size - 1; i > 0; i--)
  36. {
  37. small = 0; // Initialize small to first element.
  38.  
  39. // Find the smallest element between the positions 1 and i.
  40. for (j = 1; j <= i; j++)
  41. {
  42. if (input_array[j] < input_array[small])
  43. {
  44. small = j;
  45. }
  46. }
  47. // Swap the smallest element found with element in position i.
  48. temp = input_array[small];
  49. input_array[small] = input_array[i];
  50. input_array[i] = temp;
  51. }
  52. }
  53.  
  54. // Function that simply displays each element of input_array.
  55. void display_array(int input_array[], int input_size)
  56. {
  57. int i;
  58.  
  59. for (i = 0; i < input_size; i++)
  60. {
  61. cout << input_array[i] << ' ';
  62. }
  63. cout << "\n\n";
  64. }
Last edited by John A; Aug 17th, 2009 at 9:22 pm. Reason: added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jake43 is offline Offline
18 posts
since Jul 2009
Aug 16th, 2009
0

Re: c++ sorting array

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
C++ Syntax (Toggle Plain Text)
  1. for(int m=0;m<length;m++)
  2. {
  3. for(int j=0; j<(length-1); j++)
  4. {
  5. if(num2[j]<num2[j+1])
  6. {
  7. temp=nums[j];
  8. nums[j]=nums[j+1];
  9. nums[j+1]=temp;
  10. }
  11. }
  12. }
btw you should post your code in the middle of the code tags

[code]code goes here[/code]
Last edited by poncho4all; Aug 16th, 2009 at 9:41 pm.
Reputation Points: 10
Solved Threads: 2
Light Poster
poncho4all is offline Offline
34 posts
since Jul 2009
Aug 16th, 2009
0

Re: c++ sorting array

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jake43 is offline Offline
18 posts
since Jul 2009
Aug 16th, 2009
0

Re: c++ sorting array

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
Reputation Points: 10
Solved Threads: 2
Light Poster
poncho4all is offline Offline
34 posts
since Jul 2009
Aug 16th, 2009
-1

Re: c++ sorting array

On an alternative note, there is std::sort() function.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 16th, 2009
0

Re: c++ sorting array

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.
Reputation Points: 105
Solved Threads: 25
Posting Whiz in Training
necrolin is offline Offline
223 posts
since Jun 2009
Aug 16th, 2009
0

Re: c++ sorting array

the program still won't compile
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jake43 is offline Offline
18 posts
since Jul 2009
Aug 16th, 2009
-1

Re: c++ sorting array

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.

C++ Syntax (Toggle Plain Text)
  1. void selection_sort(int input_array[], int input_size);
  2. void display_array(int input_array[], int input_size);

to
C++ Syntax (Toggle Plain Text)
  1. void selection_sort(double input_array[], int input_size);
  2. void display_array(double input_array[], int input_size);

And also change the function header to match the prototype.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 17th, 2009
0

Re: c++ sorting array

Quote ...
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
C++ Syntax (Toggle Plain Text)
  1. 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
C++ Syntax (Toggle Plain Text)
  1. 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)
Last edited by zalezog; Aug 17th, 2009 at 11:55 am.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Aug 17th, 2009
-1

Re: c++ sorting array

Quote ...
True.
But the program still wouldn't work
Yes thats true. I never said it would work.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need to use 2 Mailslots to communicate between local applications? [C++ & C#]
Next Thread in C++ Forum Timeline: c++ standard deviaton





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC