A Terribly Inefficient Sort Strategy – Program 7 – Fall... Due: Friday, December 2 – Noon – No late...

advertisement
A Terribly Inefficient Sort Strategy – Program 7 – Fall 2011
Due: Friday, December 2 – Noon – No late programs accepted
This program is optional. If you turn it in, it will count as “extra credit.” IF you have missed a program or have a very
low grade on one, I will replace the grade with this one. If your program grades are pretty good and this grade is about
the same, then I will add 10% of this grade to your lowest program grade. (To get the 10% this grade must be at least
70.) I will use the method which will improve your grade most. It will not lower your grade.
Declare 2 arrays to hold 200 integers. The names of the arrays are to be ORIGINAL and SORTED. Read data from the
posted file to fill array ORIGINAL. Initialize SORTED to all zeros in the declaration. You can assume that all the data
items are between 0 and 100. You will then use the method described below to move the numbers in ORIGINAL to
SORTED so that the numbers in SORTED will be in sorted order from small to large. (NOTE: The method described below
is a terribly inefficient method, but it is easy to implement. You WILL use this method.) Use const int size = 200 so that
your program will work for any size array without recoding.
Declare your input file in main. Declare your output file as global.
Overview of sort method – TERRIBLE_SORT:
1. Search ORIGINAL for smallest value.
2. Copy it into SORTED in the next available location.
3. Change the value copied in ORIGINAL to a value 1000.
4. Repeat until all values are copied into SORTED.
In main you are to do the following:
1. Read data into ORIGINAL from a file
2. Call PRINT_ARRAY for ORIGINAL
2. Call TERRIBLE_SORT
3. Call PRINT_ARRAY for SORTED
You MUST implement and use the following functions as named and described.
TERRIBLE_SORT:
Parameters: 2 integer arrays, integer size (of array)
Returns: nothing (SORTED contains sorted integers)
Actions: move integers from ORIGINAL to SORTED by calling FIND_SMALLES & MOVE_SMALLEST appropriately
FIND_SMALLEST:
Parameters: integer array, integer & value, integer & location, integer size (of array)
Returns: value of the smallest element in the array and its location through reference parameters
Action: Searches array only; Leaves array unchanged
MOVE_SMALLEST:
Parameters: 2 integer arrays, integer location_small, integer location_new
Returns: Nothing; Both arrays are changed
Action: copies the value from location_small in ORIGINAL to location_new in SORTED, changes value in
location_small to value 1000;
PRINT_ARRAY:
Parameters: integer array, integer size (of array)
(You may assume size is divisible by 10, but not that it is exactly 200. Must work for any multiple of 10.)
Returns: Nothing
Action: Prints array with 10 values per line, each column right aligned (Headers are to be printed in main.)
A data file containing at least 200 integers will be posted. RECOMMENDATION: Develop your program using your own
smaller file then change to 200 for turning in.)
Download