fork download
  1. /*
  2. ** University of Washington PCE (Professional Continuing Education)
  3. ** Foundations of C++
  4. ** Assignment 8, eoSortMain.c
  5. **
  6. ** Development
  7. ** Author: Colby O'Donnell <colbster@uw.edu>
  8. ** Reviewer: David Nielsen <nielsen@u.washington.edu>
  9. ** Developed: December 2, 2011
  10. ** Updated: July 8, 2012
  11. **
  12. ** Solution
  13. ** Author: Enter your name here
  14. ** Date: Enter date here
  15. **
  16. ** INSTRUCTIONS
  17. **
  18. ** Write a function, EvenOddSort(), that sorts an array of integers by first moving all even numbers before odd numbers,
  19. ** and then sorting from smallest to highest. The resulting order would be even numbers (smallest to highest) followed
  20. ** by odd numbers (smallest to highest). The function uses your choice of an in-place bubble or selection sorting
  21. ** algorithm. It is passed the unsorted integer array (to be modified in place) and its length (the number of elements
  22. ** in the array) and returns the number of swaps made during the sort, as an unsigned int.
  23. **
  24. ** Declare the EvenOddSort() function in eoSort.h, and define it in eoSort.c. Define additional functions
  25. ** that EvenOddSort() requires in eoSort.c. For example, you should define a swap function and a comparison
  26. ** function. Write all sorting, swapping, and comparison functions yourself. The only header you will need is
  27. ** <stdio.h>. Do not use qsort() or any other library sorting function.
  28. **
  29. ** DESIGN HINTS
  30. **
  31. ** Things to remember:
  32. ** - Use Fibonacci from Assignment 7 as an example of how to organize the three files (eoSort.c, eoSort.h, eoSortMain.c).
  33. ** - Consolidate common (redundant) code rather than creating duplicate code via copy/paste.
  34. ** - Keep your program as simple as possible. Avoid creating more variables than necessary.
  35. **
  36. ** Common mistakes to avoid that were made on this assignment in past quarters:
  37. ** - The sorting algorithm and comparison logic are mixed unnecessarily and cannot be varied independently.
  38. ** - Unnecessarily exposing the implementation details of eoSort.c in eoSort.h.
  39. ** - Putting test code in eoSort.c rather than eoSortMain.c.
  40. ** - Having redundant code, such as two printf statements that do nearly identical things.
  41. ** - Declaring variables with more scope and visibility than necessary.
  42. ** - Inconsistent indentation. Mixture of tabs and spaces. Generally sloppy looking code.
  43. ** - The test code does not actually verify the actual versus expected arrays and number of swaps.
  44. **
  45. ** TESTING
  46. **
  47. ** Your main() function will be the automated test driver for your EvenOddSort() function, located in eoSortMain.c.
  48. ** Write a test driver that loops through a list of test cases, where each test case is a given input array and its
  49. ** length. Be sure to test a variety of lengths. The driver is also given a list of expected output arrays, along with
  50. ** the expected number of swaps to be made.
  51. **
  52. ** For each test case, print a header that includes the test case number, and on separate lines (so that elements are
  53. ** vertically aligned and easy to compare) print the given input array, the modified and sorted output array, and the
  54. ** expected array. Along with those, print the expected and actual number of swaps made. Finally, print out a final
  55. ** status, PASS or FAIL, for each test case based on whether the actual results match the expected results.
  56. **
  57. ** To define your test data, define a pair of 2-dimension arrays, called "given" and "expected". The first
  58. ** dimension of each array is the test case. You decide how many test cases there are. The second dimension is the
  59. ** elements of the array being tested for each test case. Reserve the first element of each test case array for
  60. ** a special purpose.
  61. **
  62. ** For the array "given", the first element is the number of elements in that row, and the remaining elements are
  63. ** the unsorted input array. For the array "expected", the first element is the expected number of swaps made, and
  64. ** the remaining elements are the expected sorted output array.
  65. **
  66. ** Below is an INCOMPLETE sample of what the test arrays look like:
  67. **
  68. ** int given[NUM_TESTS][MAX_ARRAY_SIZE] =
  69. ** {
  70. ** { 0 },
  71. ** { 1, 5 },
  72. ** { 2, 10, 12 },
  73. ** { 2, 12, 10 },
  74. ** { 8, 2, 4, 6, 8, 1, 3, 5, 7 },
  75. ** { 8, 7, 5, 3, 1, 8, 6, 4, 2 },
  76. ** };
  77. **
  78. ** int expected[NUM_TESTS][MAX_ARRAY_SIZE] =
  79. ** {
  80. ** { 0 },
  81. ** { 0, 5 },
  82. ** { 0, 10, 12 },
  83. ** { 1, 10, 12 },
  84. ** { 0, 2, 4, 6, 8, 1, 3, 5, 7 },
  85. ** { 28, 2, 4, 6, 8, 1, 3, 5, 7 },
  86. ** };
  87. **
  88. ** Copy and paste your program output to EvenOddSortTest.txt. Below is an incomplete sample test output from my
  89. ** solution:
  90. **
  91. ** Welcome to the EvenOddSort() test driver.
  92. **
  93. ** ------------------------------------------------- Test 1
  94. ** Given: ()
  95. ** Got: () with 0 swaps
  96. ** Expected: () with 0 swaps
  97. ** Outcome: PASS
  98. **
  99. ** ------------------------------------------------- Test 2
  100. ** Given: (5)
  101. ** Got: (5) with 0 swaps
  102. ** Expected: (5) with 0 swaps
  103. ** Outcome: PASS
  104. **
  105. ** ------------------------------------------------- Test 4
  106. ** Given: (10, 12)
  107. ** Got: (10, 12) with 0 swaps
  108. ** Expected: (10, 12) with 0 swaps
  109. ** Outcome: PASS
  110. **
  111. ** ------------------------------------------------- Test 5
  112. ** Given: (12, 10)
  113. ** Got: (10, 12) with 1 swap
  114. ** Expected: (10, 12) with 1 swap
  115. ** Outcome: PASS
  116. **
  117. ** ------------------------------------------------- Test 6
  118. ** Given: (11, 13)
  119. ** Got: (11, 13) with 0 swaps
  120. ** Expected: (11, 13) with 0 swaps
  121. ** Outcome: PASS
  122. **
  123. ** ------------------------------------------------- Test 7
  124. ** Given: (13, 11)
  125. ** Got: (11, 13) with 1 swap
  126. ** Expected: (11, 13) with 1 swap
  127. ** Outcome: PASS
  128. **
  129. */
  130.  
  131. #include <stdio.h>
  132.  
  133. void Swap(int * current, int * next)
  134. {
  135. int temp = 0;
  136.  
  137. temp = *current;
  138. *current = *next;
  139. *next = temp;
  140. }
  141.  
  142. void Compare(int * current, int * next, int * swap_count)
  143. {
  144.  
  145. if (*current > *next)
  146. {
  147. Swap(current, next);
  148. *swap_count = *swap_count + 1;
  149. }
  150.  
  151. }
  152.  
  153. void EvenOddSort(int * test_array)
  154. {
  155. int i = 0;
  156. int swap_count = 0;
  157.  
  158. while(1)
  159. {
  160. swap_count = 0;
  161.  
  162. for (i = 1 ; i < *test_array ; i++)
  163. {
  164. Compare((test_array+i),(test_array+i+1),&swap_count);
  165. }
  166.  
  167. printf("%d",swap_count);
  168.  
  169. if (swap_count == 0);
  170. break;
  171. }
  172. }
  173.  
  174. int main()
  175. {
  176. int test_array[9] = {8,3,2,7,5,4,8,1,9};
  177. int i = 0;
  178.  
  179.  
  180. EvenOddSort(test_array);
  181.  
  182. return 0;
  183. }
  184.  
Success #stdin #stdout 0s 5460KB
stdin
Standard input is empty
stdout
4