#define ALLOW_INSTRUCTOR_CODE /*************************************************************************** *************************************************************************** ****** BEFORE BEGINNING OR TURNING IN THIS EXERCISE IT IS EXTREMELY ****** ****** IMPORTANT THAT YOU READ, FULLY UNDERSTAND, AND ADHERE TO THE ****** ****** REQUIREMENTS STATED IN THE DOCUMENT TITLED, "ASSIGNMENT ****** ****** SUBMISSION REQUIREMENTS", DOWNLOADABLE FROM THE COURSE WEB ****** ****** SITE. ****** *************************************************************************** **************************************************************************/ /*************************************************************************** ************* C/C++ Programming 2: Assignment 6 - Exercise 2 ************* ******** BEGIN EXERCISE REQUIREMENT - THIS MUST BE A "C++" PROGRAM ******** *************************************************************************** (2 points) Write functions GetValues and SortValues: GetValues syntax: float *GetValues(float *p, size_t n); Parameters: p - a pointer to the first element of an array of floats n - the number of elements in that array Synopsis: Prompts the user to input whitespace-separated floating point values, which it then stores into the successive elements of the array in

starting with element 0. Return: a pointer to the first element of the array SortValues syntax: float *SortValues(float *p, size_t n); Parameters: p - a pointer to the first element of an array of floats n - the number of elements in that array Synopsis: Sorts the array in p IN DESCENDING ORDER using the "bubble sort" algorithm Return: a pointer to the first element of the sorted array * Use no global variables. * Use no global information about the array. * Do not use the form pointer[offset] unless offset==0. * Do not use the form *(pointer + offset) at all. * Do use the forms *pointer and *pointer++ freely, as necessary. * Use the following test values (copying and pasting them onto your command line after the prompt is an easy way to avoid typing them): 1st prompt: 1.2 3.4 5 6 7.7 8e4 22.6e-4 11.22 .00 0.4 2nd prompt: -20 4 +16.8 -.0003 32.79 76 -6e6 3rd prompt: 1 2 3 4 5 *************************************************************************** ************************ END EXERCISE REQUIREMENT ************************* **************************************************************************/ /*************************************************************************** * BEFORE PRINTING THIS ASSIGNMENT TO TURN IN, DELETE THIS COMMENT AND * EVERYTHING ABOVE IT. **************************************************************************/ /* * TODO: * REPLACE THIS COMMENT WITH YOUR TITLE BLOCK. */ /* * TODO: * REPLACE THIS COMMENT WITH YOUR C++ CODE, PLUS ANYTHING NECESSARY TO * SUPPORT MY TEST CODE BELOW. DO NOT INCLUDE ANY UNNEEDED HEADER FILES! */ /*************************************************************************** * BEFORE PRINTING THIS ASSIGNMENT TO TURN IN, DELETE THIS COMMENT AND * EVERYTHING BELOW IT. **************************************************************************/ /* ***** Change nothing below this comment without instructor permission. **** * Everything that follows was written to help test/verify your code. You * do not need to understand the details of my code to write yours! */ #ifdef ALLOW_INSTRUCTOR_CODE float *DisplayValues(float *p, size_t n); int main() { const int SZ1 = 10; const int SZ2 = 7; const int SZ3 = 5; float array1[SZ1]; float array2[SZ2]; float array3[SZ3]; DisplayValues(SortValues(GetValues(array1, SZ1), SZ1), SZ1); DisplayValues(SortValues(GetValues(array2, SZ2), SZ2), SZ2); DisplayValues(SortValues(GetValues(array3, SZ3), SZ3), SZ3); return EXIT_SUCCESS; } float *DisplayValues(float *p, size_t n) { float previous = *p, *ptr, *end = p + n; setiosflags(ios_base::fixed); for (ptr = p; ptr < end; ++ptr) // get each element { cout << *ptr << '\n'; if (ptr != p) // if not first element... { if (previous < *ptr) // ...check sort order { cerr << "Error - Array sorted incorrectly\n"; return NULL; } } previous = *ptr; // save this element } resetiosflags(ios_base::fixed); return p; } #endif