fork(6) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. bool ok = true;
  7. const int arrsize = 10;
  8. int x // the input
  9. , n = 0 // to keep track of numbers already in the array
  10. , i, j // to iterate in loops
  11. , arr[arrsize];
  12.  
  13. cout << "Enter 10 numbers: \n";
  14. while (cin >> x) {
  15. for (i=0, ok=false; i<n; i++ ) { // iterate for the general case
  16. if (x < arr[i]) { // find first wher x is lower
  17. for (j = n; j>i; j--) // move remaining elements
  18. arr[j] = arr[j - 1];
  19. arr[i] = x;
  20. n++;
  21. ok = true; // we've found the place and inserted x
  22. break;
  23. }
  24. }
  25. if (!ok) { // if we didn't insert until now, we have to add it at the end
  26. if (n<arrsize)
  27. arr[n++] = x;
  28. else cerr << "Array full "<<endl;
  29. }
  30. }
  31.  
  32. for (i = 0; i < n; i++) // n not 10 or arrsize
  33. cout << arr[i] << " ";
  34.  
  35. // cin.get();
  36. //cin.get();
  37. }
Success #stdin #stdout 0s 3460KB
stdin
3 5 7 9 6 4 10
stdout
Enter 10 numbers: 
3 4 5 6 7 9 10