Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 1st, 2012  |  syntax: None  |  size: 8.62 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.   Name: Programming Lab#03 Template
  3.   Copyright:
  4.   Author: Dr. Chang-Sheng Chen, <cschen@mail.nctu.edu.tw>
  5.   Date: 19/03/11 10:13
  6.   Description: In this lab, we would like to practice the following:
  7.                a. passing values of an entire array to functions (e.g., function argument using pass-by-reference)
  8.                b. modifying the elements values of a array in called functions
  9.                c. practicing the recusive problem-solving method by using the recursive function calls
  10.                   * attacked problem: to sort a small list of integers stored in an array
  11.                   * method: recusive insertion sort, recursive selection sort
  12. */
  13. #include <cstdlib>
  14. #include <iostream>
  15. #include <ctime>  // srand(seed) function might use the time(0) function, which requires the header file
  16.  
  17. using namespace std;
  18.  
  19. // Required function prototypes - you must define the following functions
  20. int preSort (int[], const int); // before sorting: prepare (or determine) the required parameters
  21. int preSort (int[], int[], const int, bool &);
  22. void postSort(int[], const int); // after sort: display the sorting result
  23.  
  24. void littledotIsTooHandsome(int [], int, int, int);
  25.  
  26. int insertionSort_rec(int[], const int, int, const bool);
  27. int selectionSort_rec(int [], const int, int, const bool);
  28.  
  29. // Optional function prototypes - you might want to define the following functions
  30. void displayArrayElements(int [], const int);
  31. void swapArrayElements (int &, int &);
  32.  
  33. // If you finish the above programming lab early, you could try design the iterated version
  34. int insertionSort_loop(int[], const int, const bool);  // we have a similar version defined in the textbook
  35. int selectionSort_loop(int [], const int, const bool);
  36.  
  37. // No global variables are permitted in this programming lab !!!!
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41.    // Testing Array and the list of its element - use the following to test your functions
  42.    const int arraySizeT = 10;
  43.    int arrayT[arraySizeT] = {10, 55, 33, 21, 67, 87, 24, 5, 35, 72};
  44.    
  45.    const int arraySizeB = 7;
  46.    int arrayB[arraySizeB] = {};
  47.    bool ascendingOrder =1 ; // 1 == ascending, 0 == descending
  48.    int startS=0; // start element to trigger next sorting sub-process
  49.    int targetV=0; // returned target value, after the determined sorting is done
  50.    
  51.    // Step1 (15%): define (design) a function, preSort, to accomplish the following:
  52.    // -------------------------------------------------------------------------------------------------------
  53.    //        a. Given an integer arrayB with the specified size value(i.e., bSize), generate and store the list
  54.    //           of elements in the arrayB
  55.    //      
  56.    //        b. determine the method of recursive sorting and the sorting order  by a random function (or a
  57.    //           set of function; e.g., simulating the coin-tossing and/or the dice-rolling process).
  58.    //           * recursive sorting method - 0: insertion sort, 1: selection sort, 2: other sort(Not defined in this Lab)
  59.    //           * sorting order - 0: ascending, 1: descending ;
  60.    //=============================================================================================
  61.     srand(time(0));
  62.    
  63.     displayArrayElements(arrayB, arraySizeB);
  64.     int sortingMethod = preSort(arrayT, arrayB, arraySizeB, ascendingOrder);
  65.     printf("sortingMethod=%d ascendingOrder=%d\n", sortingMethod, ascendingOrder);
  66.    
  67.    
  68.    // Step2 (10%):By using the result of the preSort function, define a function to display the initialing factors:
  69.    //---------------------------------------------------------------------------------------------------------------
  70.    //          a. the recursive sorting method to use
  71.    //          b. the sorting order
  72.    //          c. showing the values of the unsorted array
  73.    //        For example, here is a possible output sequence:
  74.    //             "We are doing a recursive selection sort in descending order !!! <newline>"
  75.    //             "Unsorted array: <newline>"
  76.    //               arrayB[0] = 11
  77.    //               arrayB[1] = 35
  78.    //               ...  (list of required elements of the array)
  79.    //=======================================================================================================
  80.    
  81.     littledotIsTooHandsome(arrayB, arraySizeB, ascendingOrder, sortingMethod);
  82.    
  83.     // Step 3(70%=2*35%): define two recursive functions, with the 4 required parameters, to accomplish the following:
  84.     //-----------------------------------------------------------------------------------------------------------
  85.     //       i.e., sort the arrayB and return the target value (i.e., minimun or maximun value of the array)
  86.     //        * methods: recursive insertion sort or recursive selection sort
  87.    
  88.         // targetV = insertionSort_rec (arrayA, arraySizeA, startS, ascendingOrder);
  89.         // targetV = selectionSort_rec(arrayB, arraySizeB, startS, ascendingOrder); // start from the 1st element
  90.         // targetV = selectionSort_loop(arrayB, arraySizeB);
  91.    
  92.        
  93.     // Step 4 (5%) - define a postSort function, to show the result of the sorting:
  94.     //-------------------------------------------------------------------------------
  95.     //         a. the sorted array
  96.     //         b. the expected target value (i.e., maximum, if sorting in ascending; otherwise, minimum.)
  97.    
  98.  
  99.    
  100.     system("PAUSE");
  101.     return EXIT_SUCCESS;
  102. }
  103.  
  104.  
  105. // littledot: The presort function, note that I have OVERLOADED the original function !!!
  106. //            Check with TA to see if overloading is allowed !!!
  107. int preSort (int arrT[], int arrB[], const int bSize, bool &order){
  108.     for(int i=0; i<bSize; i++)
  109.         swapArrayElements(arrT[i], arrB[i]);
  110.        
  111.     order = rand() % 2;
  112.     return rand() % 2;
  113. }
  114.  
  115. // littledot: Reports the current status
  116. void littledotIsTooHandsome(int arrB[], int bSize, int order, int method){
  117.     char orderStr[50], methodStr[50];
  118.    
  119.     if(order==1)strcpy(orderStr, "ascending");
  120.     else strcpy(orderStr, "descending");
  121.    
  122.     if(method==1)strcpy(methodStr, "insertion");
  123.     else strcpy(methodStr, "selection");
  124.    
  125.     printf("We are doing a recursive %s sort in %s order !!!\n",methodStr, orderStr);
  126.     printf("Unsorted array:\n");
  127.     displayArrayElements(arrB, bSize);
  128. }
  129.  
  130.  
  131. int insertionSort_rec (int b[], const int bSize, int next, const bool ascendingOrder)
  132. {
  133.      int targetV=0;
  134.      
  135.      //step1: basic case checking, is there any remaining element to insert ?
  136.      //        if NO, finish and return the last element value of the array b;
  137.      
  138.      //step2: insert the j element into the other sorted sub-array with j-1 elements
  139.      //       * next - the next element to insert; start of the unsorted sub-array
  140.      
  141.      //Step3: recursively called the function itself to continue processing
  142.               // Or, if next element is not the last element, recursively called the function again
  143.  
  144.  return  targetV;      
  145. }
  146.  
  147.  
  148.  
  149. int selectionSort_rec(int b[], const int bSize, int start, const bool ascendingOrder)
  150. {  
  151.   int targetV=0;
  152.    
  153.     //step1: basic case checking, is the last element of the array ?
  154.      //        if YES (aleardy sorted), finish and return the last element value of the array b;
  155.      
  156.      //step2: find the target value by selecting from the unsorted sub-array beginging from the elementt
  157.      //        with the index <start>
  158.      //       * start - start of the unsorted sub-array
  159.      
  160.      //Step3: recursively called the function itself to continue processing
  161.               // Or, if next element is not the last element, recursively called the function again
  162.    
  163.     return  targetV;          
  164.  }
  165.  
  166.  
  167. // Optional- you might want to define the following functions
  168. //------------------------------------------------------------
  169. void displayArrayElements(int b[], const int size)
  170. {    for(int i=0; i<size; i++)
  171.         printf("arr[%d]=%d\n",i,b[i]);
  172. }
  173.  
  174. void swapArrayElements(int &a, int &b)
  175. {  
  176.     printf("before swapping:%d(%x)=%d\t%d(%x)=%d\n",&a,&a,a,&b,&b,b);
  177.  
  178.     int temp = a;
  179.     a = b;
  180.     b = temp;
  181.    
  182.     printf("after swapping:\t%d(%x)=%d\t%d(%x)=%d\n",&a,&a,a,&b,&b,b);
  183. }
  184.  
  185.  
  186. // If you finish the above programming lab early, you could try design the iterated version
  187. //------------------------------------------------------------------------------------------
  188. int insertionSort_loop (int b[], const int size, const bool ascendingOrder)
  189. {
  190.     int targetV=0;
  191.     // To define the missing part
  192.     return  targetV;
  193. }
  194.  
  195. int selectionSort_loop(int b[], const int bSize, const bool ascendingOrder )
  196. {    
  197.     int targetV=0;
  198.     // To define the missing part
  199.     return  targetV;      
  200. }