c++ sorting array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 18
Reputation: jake43 is an unknown quantity at this point 
Solved Threads: 0
jake43 jake43 is offline Offline
Newbie Poster

c++ sorting array

 
0
  #1
Aug 16th, 2009
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 34
Reputation: poncho4all is an unknown quantity at this point 
Solved Threads: 2
poncho4all poncho4all is offline Offline
Light Poster

Re: c++ sorting array

 
0
  #2
Aug 16th, 2009
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
  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.
Do {me} While (you can)
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 18
Reputation: jake43 is an unknown quantity at this point 
Solved Threads: 0
jake43 jake43 is offline Offline
Newbie Poster

Re: c++ sorting array

 
0
  #3
Aug 16th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 34
Reputation: poncho4all is an unknown quantity at this point 
Solved Threads: 2
poncho4all poncho4all is offline Offline
Light Poster

Re: c++ sorting array

 
0
  #4
Aug 16th, 2009
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
Do {me} While (you can)
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,408
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 181
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: c++ sorting array

 
0
  #5
Aug 16th, 2009
On an alternative note, there is std::sort() function.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 176
Reputation: necrolin will become famous soon enough necrolin will become famous soon enough 
Solved Threads: 17
necrolin's Avatar
necrolin necrolin is offline Offline
Junior Poster

Re: c++ sorting array

 
0
  #6
Aug 16th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 18
Reputation: jake43 is an unknown quantity at this point 
Solved Threads: 0
jake43 jake43 is offline Offline
Newbie Poster

Re: c++ sorting array

 
0
  #7
Aug 16th, 2009
the program still won't compile
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,408
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 181
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: c++ sorting array

 
0
  #8
Aug 16th, 2009
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.

  1. void selection_sort(int input_array[], int input_size);
  2. void display_array(int input_array[], int input_size);

to
  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.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 45
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: c++ sorting array

 
0
  #9
Aug 17th, 2009
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
  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
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,408
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 181
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: c++ sorting array

 
0
  #10
Aug 17th, 2009
True.
But the program still wouldn't work
Yes thats true. I never said it would work.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC