### Sorting Array

I'm writing a program that asks the user for a series of numbers, prints them back out, and then sorts them. I have it doing that already, it is sorted and everything, but how can I print the numbers back out ones they are sorted? For now, all I can do is print out specific numbers from the array, as you can see from my code.

Any help? Thank you.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include using namespace std; int main() { int digits[10], input, counter=0; cout << "Enter 10 numbers or -1 to stop: "; for (int i = 0; i < 10; i++) { cin >> input; if (input != -1) { digits[i]=input; counter++; } else break; } cout << "Digits entered: "; for (int j = 0; j < counter; j++) cout << digits[j] << " "; cout << endl; cout << "Now sorting..." << endl; for (int i = 0; i < counter-1; i++) { int smallest = i; for (int j=i+1; j < counter; j++) { if (digits[j] < digits[smallest]) smallest = j; } swap (digits[smallest], digits[i]); } cout << "Smallest: " << digits[0] << " Largest: " << digits[counter-1]; return 0; }``````
Last edited on
There is no need for a nested for-loop, a single for-loop will work for this problem.

 ``123456789101112`` ``````for (int g = 0, temp = 0; g < counter; ++g) for (int f = g; f < counter; ++f) if (digits[f] > digits[f+1] && digits[f+1] != '\0') { temp = digits[f]; digits[f] = digits[f+1]; digits[f+1] = temp; } for (int h = 0; h < counter; ++h) cout << digits[h] << " "; cout <<"\n";``````

 ``123456789101112`` ``````for (int g = 0, temp = 0; g < counter; ++g) for (int f = g; f+1 < counter; ++f) if (digits[g] > digits[f+1]) { temp = digits[g]; digits[g] = digits[f+1]; digits[f+1] = temp; } for (int h = 0; h < counter; ++h) cout << digits[h] << " "; cout <<"\n";``````

 ```\$ ./File Enter 10 numbers or -1 to stop: 2 43 2 8 7 6 -1 Digits entered: 2 43 2 8 7 6 Now sorting... 2 2 6 7 8 43```
Last edited on
@Smac89
There is no need for a nested for-loop, a single for-loop will work for this problem.

 ``123456789`` ``````for (int g = 0, temp = 0; g < counter; ++g) { if (digits[g] > digits[g+1] && digits[g+1] != '\0') { temp = digits[g]; digits[g] = digits[g+1]; digits[g+1] = temp; } }``````

This code is invalid because you are trying to access an element digits[counter] (that is digits[g+1] when g == counter -1) thta has an undefined value.

EDIT: And moreover your code does not sort the array. So your code is invalid in whole.

Last edited on

Yup I realised that, so I fixed it ^

 This code is invalid because you are trying to access an element digits[counter] (that is digits[g+1] when g == counter -1) thta has an undefined value.

I specified this : `&& digits[g+1] != '\0'` in the condition. Wouldn't that stop it from reading beyond the elements in the array?
Last edited on
I know my code is longer but for me it's clearer to understand since I'm new to this but I was able to figure out how to print them back out.

This is my final code:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include using namespace std; int main() { int digits[10], input, counter=0; cout << "Enter 10 numbers or -1 to stop: "; for (int i = 0; i < 10; i++) { cin >> input; if (input != -1) { digits[i]=input; counter++; } else break; } cout << "Digits entered: "; for (int j = 0; j < counter; j++) cout << digits[j] << " "; cout << endl; cout << "Now sorting..." << endl; for (int i = 0; i < counter-1; i++) { int smallest = i; for (int j=i+1; j < counter; j++) { if (digits[j] < digits[smallest]) smallest = j; } swap (digits[smallest], digits[i]); } for (int k = 0; k < counter; k++) cout << digits[k] << " "; cout << endl; return 0; }``````
A good code! Only I would include the header that contains the declaration of std::swap.. Ir is <utility>.
EDIT: And I would declare a named constant for the magic number 10. For example

const int N = 10;

and use this name N everywhere in the code instead of 10.
Last edited on
 @Smac89 I specified this : && digits[g+1] != '\0' in the condition. Wouldn't that stop it from reading beyond the elements in the array?

This will not help. First of all not used elements of the array have undefined values. If you would define the array the following way

int digits[10] = {};

in this case all elements would have initial values equal to 0. But this also will not help because a user could enter all 10 elements of the array. That is the array would not have an element with value 0.

Topic archived. No new replies allowed.