Guest User

Untitled

a guest
Dec 12th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. #define clear() printf("\033[H\033[J")
  8.  
  9. // Checks if the array contains the number from the input variable
  10. int duplicate(int array[100], int number){
  11. int answer = 0, i;
  12.  
  13. for (i = 0; i < 100; i++) {
  14. if (array[i] == number) {
  15. answer = 1;
  16. }
  17. }
  18. return answer;
  19. }
  20.  
  21. // Prints the array of number in the correct format
  22. void print(int array[]){
  23. int i;
  24. int rowsum = 0, val = 0, col, columsum[10] = {0}, row;
  25. for (row = 0; row < 10; row++) {
  26. for (col = 0; col < 10; col++) {
  27. printf("%i ", array[val]);
  28. rowsum += array[val];
  29. columsum[col] += array[val];
  30.  
  31. val++;
  32. }
  33. printf(" :: Rowsum of row %i is %i\n",row + 1,rowsum);
  34. rowsum = 0;
  35. }
  36.  
  37. printf("Kolumnsum: \n");
  38. for (i = 0; i < 10; i++)
  39. {
  40. printf("Kolumn %i %i\n", i + 1, columsum[i]);
  41. }
  42. }
  43.  
  44. // Sorts the array from lowest number to the highest
  45. int * sort(int array[]) {
  46. int i, j, temp;
  47.  
  48. for (i = 0; i < 100 - 1; i++) {
  49. for (j = 0; j < 100 - 1; j++)
  50. {
  51. if (array[j]>array[j+1])
  52. {
  53. temp = array[j];
  54. array[j] = array[j + 1];
  55. array[j + 1] = temp;
  56. }
  57. }
  58. }
  59. return array;
  60. }
  61.  
  62. // Quicksort compare function
  63. /*
  64. int cmpfunc (const void * a, const void * b) {
  65. return (* (int*) a - *(int*) b);
  66. }
  67. */
  68.  
  69. // Calculates and prints array values
  70. void count(int array[])
  71. {
  72. int i, sum = 0;
  73. for (i = 0; i < 100; i++)
  74. {
  75. sum += array[i];
  76. }
  77. sum /= 100;
  78. printf("Average value: %i\n", sum);
  79. printf("Median: %i\n", (array[49] + array[50]) / 2);
  80. printf("Max value: %i\n", array[99]);
  81. printf("Minimum value: %i\n", array[0]);
  82. }
  83.  
  84. // Binary seach function
  85. void search (int array[], int comp)
  86. {
  87. int high = 100, low = 0, mid;
  88.  
  89. while ((high - low) > 1) {
  90.  
  91. mid = (high + low) / 2;
  92.  
  93. if (comp >= array[mid]) {
  94. low = mid;
  95. }
  96.  
  97. else {
  98. high = mid;
  99. }
  100. }
  101.  
  102. // Returns the searched value if it exist
  103. if (comp == array[low]) {
  104. printf("Number searched: %i found on position %i\n", comp, low + 1);
  105. }
  106.  
  107. //Returns a faulty value telling the code that the searched number does not exist
  108. else {
  109. printf("Number not found: %i\n", comp);
  110. }
  111. }
  112.  
  113. // Clears the console after you pressed a menuchoice
  114. void clrscr()
  115. {
  116. system("@cls||clear");
  117. }
  118.  
  119. int main(){
  120.  
  121. // Initialzing some variables
  122. int number, max = 800, min = 100, input, value = 0;
  123.  
  124. // Array for numbers
  125. int unsorted_numbers[100], sorted_numbers[100];
  126.  
  127. // Check values
  128. int check1 = 0, check2 = 0;
  129.  
  130. do {
  131. // Menu
  132. printf(": : : : : : : : : : : : \n");
  133. printf(": : : : M E N U : : : : \n");
  134. printf("1. Print out 100 random numbers between 100 and 900.\n");
  135. printf("2. Sort the randomized numbers and print them.\n");
  136. printf("3. Print out calculations based on these 100 randomized numbers.\n");
  137. printf("4. Search for a number: \n");
  138. printf("Enter '0' to exit\n");
  139. printf("Enter your menuchoice: ");
  140. scanf("%i", &input);
  141. printf("\n");
  142.  
  143. switch (input) {
  144. case 1:
  145. // Seed for the random number generator
  146. srand((int)time(NULL));
  147.  
  148. // Adds random number between min and max, excludes duplicates
  149. int i, j;
  150. for (i = 0; i < 100; i++) {
  151. number = rand() % max + min;
  152.  
  153. while (duplicate(unsorted_numbers, number) == 1) {
  154. number = rand() % max + min;
  155. }
  156. unsorted_numbers[i] = number;
  157. }
  158.  
  159. // Printing unsorted array (1)
  160. printf("Unsorted array: \n");
  161. print(unsorted_numbers);
  162. printf("\n");
  163.  
  164. printf("\n");
  165. check1=1;
  166. break;
  167.  
  168. case 2:
  169. if (check1 == 1){
  170.  
  171. // Copying the unsorted list before modifying
  172. for (j = 0; j < sizeof(unsorted_numbers)/sizeof(int); j++) {
  173. sorted_numbers[j] = unsorted_numbers[j];
  174. }
  175.  
  176. // Sorting and printing the array (2)
  177. printf("Sorted array: \n");
  178. sort(sorted_numbers);
  179. //qsort(sorted_numbers, 100, sizeof(int), cmpfunc);
  180. print(sorted_numbers);
  181. printf("\n");
  182. check2=1;
  183. }
  184. printf("Enter another number case 2\n");
  185. break;
  186.  
  187. case 3:
  188. if (check2 == 1){
  189.  
  190. // Prints out Calculations (3)
  191. count(sorted_numbers);
  192. printf("\n");
  193. }
  194. printf("Enter another number case 3\n");
  195. break;
  196.  
  197. case 4:
  198. // Binary search for numbers (4)
  199. if (check2 == 1){
  200. printf("Search for a number: ");
  201. scanf("%i", &value);
  202. search(sorted_numbers, value);
  203. printf("\n");
  204. }
  205. printf("Enter another number case 4\n");
  206. break;
  207.  
  208. default:
  209. if (input <= 4 && input != 0){
  210. printf("No menu choice for that number\n");
  211. printf("\n");
  212. }
  213. else if (input > 4){
  214. printf("You have to enter a number between 1-4.\n");
  215. printf("\n");
  216. }
  217. else if (input == 0){
  218. printf("Exiting program.......\n");
  219. printf("\n");
  220. }
  221.  
  222. break;
  223. }
  224.  
  225. } while (input != 0);
  226.  
  227. return 0;
  228. }
Add Comment
Please, Sign In to add comment