fork download
  1. // Kurt Feiereisel CSC5 Chapter 8, p.4, #10
  2. /*******************************************************************************
  3.  *
  4.  * Sort Two Arrays
  5.  * _____________________________________________________________________________
  6.  * This program will display two arrays unsorted, then sort the two arrays.
  7.  * While the program is sorting the arrays it will display the elements of
  8.  * the arrays after each time an element is sorted.
  9.  * _____________________________________________________________________________
  10.  * INPUT:
  11.  * first[] : This array holds 8 integer values
  12.  * second[] : This array holds the same 8 integers as array first[]
  13.  *
  14.  * OUTPUT:
  15.  * first[] : Sorted first array
  16.  * second[] : Sorted second array
  17.  *
  18.  *
  19.  * ****************************************************************************/
  20. #include <iostream>
  21. using namespace std;
  22.  
  23. // Function Prototypes
  24. void sortFirst(int first[], int SIZE);
  25. void sortSecond(int second[], int SIZE);
  26. void displayFirst(int first[], int SIZE);
  27. void displaySecond(int second[], int SIZE);
  28.  
  29.  
  30. int main()
  31. {
  32. // Initalize Arrays and Constants
  33. const int SIZE = 8;
  34. int first[SIZE] = {14, 24, 25, 75, 34, 23, 86, 12};
  35. int second[SIZE] = {14, 24, 25, 75, 34, 23, 86, 12};
  36.  
  37. // Function Calls
  38. displayFirst(first, SIZE);
  39. sortFirst(first, SIZE);
  40. displayFirst(first, SIZE);
  41. displaySecond(second, SIZE);
  42. sortSecond(second, SIZE);
  43. displaySecond(second, SIZE);
  44. return 0;
  45. }
  46.  
  47. /*
  48.  * Definition of Function: displayFirst
  49.  * This function displays the array first[].
  50.  */
  51. void displayFirst(int f[], int size)
  52. {
  53. cout << "First Array" << endl;
  54.  
  55. // For loop to display each element of array
  56. for(int index = 0; index < size; index++)
  57. cout << f[index] << " ";
  58. cout << endl;
  59. }
  60.  
  61. /*
  62.  * Definition of Function: sortFirst
  63.  * This function put the array in ascending order by using a bubble sort.
  64.  * This function will display the array after each pass of the sort
  65.  */
  66. void sortFirst(int f[], int size)
  67. {
  68. // Declare Variables
  69. bool swap;
  70. int temp;
  71.  
  72. // Implement do-while loop
  73. do
  74. {
  75. // Initalize swap to false
  76. swap = false;
  77.  
  78. // Implement for loop to compare each element of the array
  79. for (int count = 0; count < (size - 1); count++)
  80. {
  81. // If element count is greater than element count + 1, swap elements
  82. if (f[count] > f[count + 1])
  83. {
  84. temp = f[count];
  85. f[count] = f[count + 1];
  86. f[count + 1] = temp;
  87. swap = true;
  88.  
  89. // Display elements after swap
  90. for(int index = 0; index < size; index++)
  91. cout << f[index] << " ";
  92. cout << endl;
  93. }
  94. }
  95. // while swap = true
  96. } while(swap);
  97. }
  98.  
  99. /*
  100.  * Definition of Function: displaySecond
  101.  * This function displays the array second[].
  102.  */
  103. void displaySecond(int s[], int size)
  104. {
  105. // Display elements of second array
  106. cout << "Second Array" << endl;
  107. for(int index = 0; index < size; index++)
  108. cout << s[index] << " ";
  109. cout << endl;
  110. }
  111.  
  112. /*
  113.  * Definition of Function: sortSecond
  114.  * This function will sort the the function second[] in ascending order
  115.  * using the bubble function. This function will display the elements of the
  116.  * array after each sort.
  117.  */
  118. void sortSecond(int s[], int size)
  119. {
  120. // Declare Local Variables
  121. bool swap;
  122. int temp;
  123.  
  124. // Implement do-while loops
  125. do
  126. {
  127. // Initalize Local Variable
  128. swap = false;
  129.  
  130. // For loop to compare element count and count + 1
  131. for(int count = 0; count < (size - 1); count++)
  132. {
  133. // If element count is greater than element count + 1, then swap
  134. if(s[count] > s[count + 1])
  135. {
  136. temp = s[count];
  137. s[count] = s[count + 1];
  138. s[count + 1] = temp;
  139. swap = true;
  140. for (int index = 0; index < size; index++)
  141. cout << s[index] << " ";
  142. cout << endl;
  143. }
  144. }
  145. // while swap = true
  146. } while(swap);
  147. }
  148.  
Success #stdin #stdout 0s 5300KB
stdin
Standard input is empty
stdout
First Array
14 24 25 75 34 23 86 12 
14 24 25 34 75 23 86 12 
14 24 25 34 23 75 86 12 
14 24 25 34 23 75 12 86 
14 24 25 23 34 75 12 86 
14 24 25 23 34 12 75 86 
14 24 23 25 34 12 75 86 
14 24 23 25 12 34 75 86 
14 23 24 25 12 34 75 86 
14 23 24 12 25 34 75 86 
14 23 12 24 25 34 75 86 
14 12 23 24 25 34 75 86 
12 14 23 24 25 34 75 86 
First Array
12 14 23 24 25 34 75 86 
Second Array
14 24 25 75 34 23 86 12 
14 24 25 34 75 23 86 12 
14 24 25 34 23 75 86 12 
14 24 25 34 23 75 12 86 
14 24 25 23 34 75 12 86 
14 24 25 23 34 12 75 86 
14 24 23 25 34 12 75 86 
14 24 23 25 12 34 75 86 
14 23 24 25 12 34 75 86 
14 23 24 12 25 34 75 86 
14 23 12 24 25 34 75 86 
14 12 23 24 25 34 75 86 
12 14 23 24 25 34 75 86 
Second Array
12 14 23 24 25 34 75 86