// Lab Problem 1.5 // CS211 ADS2 // T Naughton CS NUIM #include void SortArray(int list[], int len); int FindSmallest(int list[], int begin, int len); void Exchange(int * elem1, int * elem2); int FindSmallest(int list[], int begin, int len){ /* ** list ranges from [0..(len-1)]. FindSmallest returns the index ** corresponding to the smallest element in the subrange ** [begin..(len-1)]. */ int sindex, counter; sindex = begin; counter = begin + 1; while (counter < len){ if (list[counter] < list[sindex]){ sindex = counter; } counter++; } return sindex; } void Exchange(int * elem1, int * elem2){ int temp; temp = *elem1; *elem1 = *elem2; *elem2 = temp; } void SortArray(int list[], int len){ /* ** list ranges from [0..(len-1)]. SortArray sorts list[], which is ** passed by reference. It works by exchanging the smallest unsorted ** element in the list with the smallest unsorted index. */ int counter = 0; while (counter < len){ Exchange(&list[counter], &list[FindSmallest(list,counter,len)]); counter++; } } void main(void){ int * iarray; double * darray; int length, counter; cout << endl << "How many integers do you have?" << endl; cin >> length; iarray = new int[length]; darray = new double[length]; cout << endl << "Please enter the " << length << " integers, "; cout << "one on each line:" << endl; counter = 0; while (counter < length) { cin >> iarray[counter]; counter++; } cout << "Thanks." << endl; /* ** Now sort the integers */ SortArray(iarray, length); counter = 0; while (counter < length) { darray[counter] = double(iarray[counter]) / 2.0; counter++; } counter = 0; while (counter < length) { cout << iarray[counter] << "\t" << darray[counter] << endl; counter++; } delete [] iarray; delete [] darray; }