#include using namespace std; int size, k = 0, save; int * arr = NULL; char ch; string temp; // This assignment is worth 5 points // -2 if the elements of the array are only printed in order and not sorted int main() { cout << "Enter array size: "; cin >> size; cin.ignore(); arr = new int[size]; cout << "Enter " << size << " values: "; do { cin >> arr[k]; k++; ch = cin.get(); } while(ch != '\n' && k < size); if(ch != '\n' && k == size) getline(cin, temp); else if(ch == '\n' && k < size) for(k; k < size; k++) arr[k] = 0; /* There are many sort techniques in the world of algorithms. The one * I'm using is one of the simpler methods known as selection sort. */ for(int i = 0; i < size; i++) { save = i; for(int j = i+1; j < size; j++) { if(arr[j] < arr[save]) save = j; // Save the index with the lowest value } /* Now swap the current index value with the lowest found. This is the technique from way back in week 1 on how to swap the values in two variables without using a third variable. */ if(save != i) { arr[i] += arr[save]; arr[save] = arr[i] - arr[save]; arr[i] -= arr[save]; } } // Print out the elements of the sorted array for(int i = 0; i < size; i++) cout << arr[i] << ' '; cout << endl; return 0; }