fork(1) download
  1. // If you are not sure what some lines of code do, try looking back at
  2. // previous example programs, notes, or ask a question.
  3.  
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. // Sorting function prototypes
  11. void bubbleSort(int[],int);
  12. void selectionSort(int[],int);
  13. void quickSort(int[],int,int);
  14.  
  15. // Will put random values between 10 and to into an array
  16. void randomize(int[],int);
  17.  
  18. // Shows how to pass a two dimensional array to a function
  19. void func(int[][10]);
  20.  
  21. int main() {
  22.  
  23. srand(time(NULL));
  24.  
  25. // MULTIDIMENTIONAL ARRAYS
  26.  
  27. // Declares a two dimensional array of integers of sizes 10 and 10, so it
  28. // can store up to 100 integers in total.
  29. int array2D[10][10];
  30.  
  31. // Declares a tree dimensional array of integers of sizes 10, 10, and 10, so it
  32. // can store up to 1000 integers in total.
  33. int array3D[10][10][10];
  34.  
  35. // Passing a multidimensional array is the same as a one dimensional array
  36. func(array2D);
  37.  
  38. // Loop though all three dimensions of the 3D array using three nested loops
  39. for(int i = 0; i < 10; i++)
  40. for(int j = 0; j < 10; j++)
  41. for(int k = 0; k < 10; k++)
  42. array3D[i][j][k] = 0;
  43.  
  44. // Output each value in the 3D Array
  45. cout << "3D Array: " << endl;
  46. for(int i = 0; i < 10; i++) {
  47. for(int j = 0; j < 10; j++) {
  48. for(int k = 0; k < 10; k++)
  49. cout << array3D[i][j][k];
  50. cout << " ";
  51. }
  52. cout << endl;
  53. }
  54. cout << endl;
  55.  
  56.  
  57. // SORTING
  58.  
  59. int sortArray[10];
  60.  
  61.  
  62. // Demonstrate Bubble Sort
  63. randomize(sortArray,10);
  64.  
  65. cout << "Array: ";
  66. for(int i = 0; i < 10; i++)
  67. cout << sortArray[i] << " ";
  68.  
  69. bubbleSort(sortArray,10);
  70.  
  71. cout << endl << "Bubble Sort: ";
  72. for(int i = 0; i < 10; i++)
  73. cout << sortArray[i] << " ";
  74. cout << endl << endl;
  75.  
  76.  
  77. // Demonstrate selection sort
  78. randomize(sortArray,10);
  79.  
  80. cout << "Array: ";
  81. for(int i = 0; i < 10; i++)
  82. cout << sortArray[i] << " ";
  83.  
  84. selectionSort(sortArray,10);
  85.  
  86. cout << endl << "Selection Sort: ";
  87. for(int i = 0; i < 10; i++)
  88. cout << sortArray[i] << " ";
  89. cout << endl << endl;
  90.  
  91.  
  92. // Demonstrate quick sort
  93. randomize(sortArray,10);
  94.  
  95. cout << "Array: ";
  96. for(int i = 0; i < 10; i++)
  97. cout << sortArray[i] << " ";
  98.  
  99. quickSort(sortArray,0,10);
  100.  
  101. cout << endl << "Quick Sort: ";
  102. for(int i = 0; i < 10; i++)
  103. cout << sortArray[i] << " ";
  104. cout << endl << endl;
  105.  
  106.  
  107. // End program
  108. cout << endl;
  109. system("pause");
  110.  
  111. return 0;
  112. }
  113.  
  114. void func(int array[][10]) {
  115. // Do stuff with array
  116. }
  117.  
  118. void bubbleSort(int array[], int size) {
  119.  
  120. int temp;
  121.  
  122. // See notes
  123. for(int i = 0; i < size; i++)
  124. for(int j = 0; j < size - 1; j++)
  125. if(array[j] > array[j+1]) {
  126. temp = array[j];
  127. array[j] = array[j+1];
  128. array[j+1] = temp;
  129. }
  130. }
  131.  
  132. void selectionSort(int array[], int size) {
  133.  
  134. int temp, lowest;
  135.  
  136. // See notes
  137. for(int i = 0; i < size - 1; i++) {
  138. lowest = i;
  139. for(int j = i; j < size; j++)
  140. if(array[j] < array[lowest])
  141. lowest = j;
  142. temp = array[i];
  143. array[i] = array[lowest];
  144. array[lowest] = temp;
  145. }
  146. }
  147.  
  148. void quickSort(int arr[], int left, int right) {
  149.  
  150. int i = left, j = right;
  151. int tmp;
  152. int pivot = arr[(left + right) / 2];
  153.  
  154. // See notes
  155. while (i <= j) {
  156. while (arr[i] < pivot)
  157. i++;
  158. while (arr[j] > pivot)
  159. j--;
  160. if (i <= j) {
  161. tmp = arr[i];
  162. arr[i] = arr[j];
  163. arr[j] = tmp;
  164. i++;
  165. j--;
  166. }
  167. }
  168.  
  169. if (left < j)
  170. quickSort(arr, left, j);
  171. if (i < right)
  172. quickSort(arr, i, right);
  173. }
  174.  
  175. void randomize(int array[], int size) {
  176. for(int i = 0; i < size; i++)
  177. array[i] = rand() % 10;
  178. }
  179.  
Success #stdin #stdout #stderr 0s 3472KB
stdin
Standard input is empty
stdout
3D Array: 
0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 
0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 
0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 
0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 
0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 
0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 
0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 
0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 
0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 
0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 

Array: 2 4 3 6 1 7 8 8 3 7 
Bubble Sort: 1 2 3 3 4 6 7 7 8 8 

Array: 1 5 2 3 1 4 1 2 7 2 
Selection Sort: 1 1 1 2 2 2 3 4 5 7 

Array: 9 3 6 9 2 9 3 5 6 2 
Quick Sort: 0 2 2 3 3 5 6 6 9 9 


stderr
sh: 1: pause: not found