| | |
c++ sorting array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2009
Posts: 18
Reputation:
Solved Threads: 0
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)
#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"; }
Last edited by John A; Aug 17th, 2009 at 9:22 pm. Reason: added code tags
•
•
Join Date: Jul 2009
Posts: 34
Reputation:
Solved Threads: 2
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
btw you should post your code in the middle of the code tags
[code]code goes here[/code]
C++ Syntax (Toggle Plain Text)
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; } } }
[code]code goes here[/code]
Last edited by poncho4all; Aug 16th, 2009 at 9:41 pm.
Do {me} While (you can)
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? 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.
to
And also change the function header to match the prototype.
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)
void selection_sort(int input_array[], int input_size); void display_array(int input_array[], int input_size);
to
C++ Syntax (Toggle Plain Text)
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.
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?•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
•
•
•
•
void selection_sort(double input_array[], int input_size);
void display_array(double input_array[], int input_size);
But the program still wouldn't work
This line
C++ Syntax (Toggle Plain Text)
int small, temp;
All values like 0.0004, 0.1200.. would appear as 0's.
Apart from that
C++ Syntax (Toggle Plain Text)
small = 0; // Initialize small to first element.
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.
•
•
•
•
True.
But the program still wouldn't 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?![]() |
Similar Threads
- sorting an 2d array (C++)
- Sorting an Array of numbers in VB 2008 (Visual Basic 4 / 5 / 6)
- sorting 2d array (C++)
- SORTING ARRAY (C++)
- sorting array (C++)
- bubble sorting in an array (C)
- sorting an array of string (C)
Other Threads in the C++ Forum
- Previous Thread: Need to use 2 Mailslots to communicate between local applications? [C++ & C#]
- Next Thread: c++ standard deviaton
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






