Posted By

AlexanderRavikovich on 12/27/11


Tagged

c


Versions (?)

Work 3 in C


 / Published in: Objective C
 

Buld menu & use pinter & dynamic arrays

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  
  6. /* Setting matrix size NxN */
  7. #define N 5
  8.  
  9. /* Prototype */
  10. void my_menu(); // Printing menu options
  11. void my_fill(int arr[][N]); // fill 2d array with user entered data
  12. void my_print(int arr[][N]); // print 2d array that we filled
  13. int magic_board(int arr[][N], int k); // Processing array that we filled
  14. int * build_array(int n); // create dynamic array & return his pointer
  15. void print_dynamic(int * p, int n); // print array by pointer
  16. int checking(int * arr, int n, int * h); // cheerking array for number tha sum of elements before and after the same
  17. int * cleanning(int * arr, int k, int * n); // return pointer of new created array with values from original array, that not devides by k
  18.  
  19. void main()
  20. {
  21. int option; // option for menu
  22. int n; // number of elements in dynamic array
  23.  
  24. /* Matrix that we will working on */
  25. int math[N][N] = {0}; // static 2d array (matrix)
  26. int k; // used for getting one number form user
  27. int *p_array; // p_array is a pointer
  28. int h; // stores index of element in array, that contains number that devides array to 2 by sum of elements
  29.  
  30. while(1)
  31. {
  32. /* Print my menu options */
  33. my_menu();
  34.  
  35. scanf("%d", &option);
  36.  
  37. printf("The choice is %d\n\n", option);
  38.  
  39. switch(option)
  40. {
  41. case 1:
  42. /* Pass pointer to "math" to our function to fill it */
  43. my_fill(math);
  44. break;
  45.  
  46. case 2:
  47. /* Pass pointer to filled "math" array to function "my_print" for print it's data */
  48. my_print(math);
  49. break;
  50.  
  51. case 3:
  52. /* Promt user to enter positive number, for finding magic cube */
  53. printf("Enter number that less then %d\n", N);
  54. scanf("%d", &k);
  55. /* Check if function return 1 or 0 */
  56. if (magic_board(math, k))
  57. {
  58. printf("\nThere is a magic sub-array of size %d\n\n", k);
  59. }
  60. else
  61. {
  62. printf("\nThere is NO a magic sub-array of size %d\n\n", k);
  63. }
  64. break;
  65.  
  66. case 4:
  67. printf("Enter size of the array:\n");
  68. scanf("%d", &n);
  69. p_array = build_array(n);
  70. break;
  71.  
  72. case 5:
  73. print_dynamic(p_array, n);
  74. break;
  75.  
  76. case 6:
  77. /* p_array pointer (is auto converted), & we send adress of "h", not his value */
  78. if (checking(p_array, n, &h))
  79. {
  80. printf("The number %d with index %d divides the array in two equal parts.\n\n", p_array[h], h);
  81. }
  82. else
  83. {
  84. printf("There is no number that divides the array in two equal parts. Try another numbers.\n\n");
  85. }
  86. break;
  87.  
  88. case 7:
  89. printf("Enter a number for cleanning:\n");
  90. scanf("%d", &k);
  91.  
  92. p_array = cleanning(p_array, k, &n); // p_array takes address of new created array
  93. break;
  94.  
  95. case 8:
  96. exit(1); // finish our programm
  97. break;
  98. }
  99. }
  100. }
  101.  
  102. /* Function my_fill */
  103.  
  104. void my_fill(int arr[][N])
  105. {
  106. /* Setting local variables */
  107. int user_num, i, j;
  108.  
  109. printf("Enter numbers between 35-143\n");
  110.  
  111. /* Fill matrix NxN with numbers from user */
  112. for(i=0; i < N; i++)
  113. {
  114. for (j=0; j < N; j++)
  115. {
  116. /* Keep user in loop until he enter right number */
  117. do
  118. {
  119. /* Get entered number */
  120. scanf("%d", &user_num);
  121. /* User input validating */
  122. if (user_num < 143 && user_num > 35)
  123. {
  124. /* Fill array with entered number (we are really changing "math" array because "arr" is a pointer */
  125. arr[i][j] = user_num;
  126. }
  127. else
  128. {
  129. printf("Wrong number! Try again:\n");
  130. }
  131. } while(!(user_num < 143 && user_num > 35));
  132. }
  133. }
  134. }
  135.  
  136. /* Function my_print for printing 2d array */
  137.  
  138. void my_print(int arr[][N])
  139. {
  140. /* Setting local variables */
  141. int i, j;
  142.  
  143. /* Print out our data */
  144. for(i=0; i < N; i++)
  145. {
  146. for (j=0; j < N; j++)
  147. {
  148. /* Print fromatted data */
  149. printf("%3d\t", arr[i][j]);
  150. }
  151. printf("\n\n");
  152. }
  153. }
  154.  
  155. /* Find magic board in our math */
  156. int magic_board(int arr[][N], int k)
  157. {
  158. /* Local variables */
  159. int i,j,a,b,z, j_pos=0, row_sum=0, col_sum=0, diag_sum=0, ctr=0, magic_exists=0;
  160. int sum[N*2+2];
  161.  
  162. for (i=0; i <= N-k; i++)
  163. {
  164. if (magic_exists)
  165. {
  166. return magic_exists;
  167. }
  168.  
  169. for (j=0; j <= N-k; j++)
  170. {
  171. if (magic_exists)
  172. {
  173. return magic_exists;
  174. }
  175.  
  176. ctr = 0;
  177.  
  178. memset(sum, 0, sizeof(int)*(N*2+2));
  179.  
  180. /* Row sum */
  181. for (a=i; a<k+i; a++)
  182. {
  183. row_sum = 0;
  184. for (b=j; b<k+j; b++)
  185. {
  186. row_sum += arr[a][b];
  187. }
  188. /* Save sum of row */
  189. sum[ctr] = row_sum;
  190. ctr++;
  191. }
  192.  
  193. /* Cols sum */
  194. for (a=i; a<k+i; a++)
  195. {
  196. col_sum = 0;
  197. for (b=j; b<k+j; b++)
  198. {
  199. col_sum += arr[b][a];
  200. }
  201. /* Save sum of col */
  202. sum[ctr] = col_sum;
  203. ctr++;
  204. }
  205.  
  206. /* Diagonal sum 1 */
  207. diag_sum = 0;
  208. j_pos = j; // <-- remember position of LEFT TOP j (column), and make him ++
  209. for (a=i; a<k+i; a++)
  210. {
  211. diag_sum += arr[a][j_pos];
  212. j_pos++;
  213. }
  214. /* Save sum of diag 1 */
  215. sum[ctr] = diag_sum;
  216. ctr++;
  217.  
  218. /* Diagonal sum 2 */
  219. diag_sum = 0;
  220. j_pos = j+k-1; /* <-- calculate position of RIGHT TOP j (column), and make him -- (I do 2nd diagonal from RIGHT TOP to LEFT BOTTOM) */
  221. for (a=i; a<k+i; a++)
  222. {
  223. diag_sum += arr[a][j_pos];
  224. j_pos--;
  225. }
  226. /* Save sum of diag 2 */
  227. sum[ctr] = diag_sum;
  228.  
  229. /*
  230. Now we have array like
  231. sum = {15,15,15,15,15,15,15,15,15,15,15}
  232.  
  233. Validate that all numbers is the same.
  234.  
  235. If we have something like
  236. sum = {15,15,15,15,15,33,15,15,15,15,15}
  237. it's not magik cube
  238. */
  239.  
  240. /* Validating found sums */
  241. for(z=0; z < ctr; z++)
  242. {
  243. if(sum[z] == sum[z+1])
  244. {
  245. magic_exists = 1;
  246. }
  247. else
  248. {
  249. magic_exists = 0;
  250. break;
  251. }
  252. }
  253. }
  254. }
  255.  
  256. return magic_exists;
  257. }
  258.  
  259. /* Printing menu */
  260. void my_menu()
  261. {
  262. printf("Choose function:\n"
  263. "1-Filling array\n"
  264. "2-Printing array\n"
  265. "3-If Magic Board\n"
  266. "4-Build Array\n"
  267. "5-Printing array\n"
  268. "6-Checking Array\n"
  269. "7-Cleanning Array\n"
  270. "8-Exit\n");
  271. }
  272.  
  273. int * build_array(int n)
  274. {
  275. int i, user_num;
  276. /* Pointer to dynamic array with n elements */
  277. int * p_array;
  278.  
  279. p_array = (int*) malloc(n*sizeof(int));
  280.  
  281. /* Validating of memory */
  282. if(p_array == NULL)
  283. {
  284. fprintf(stderr, "Out of memory, exiting\n");
  285. exit(1);
  286. }
  287.  
  288. printf("Enter %d numbers between -30 to 30:\n", n);
  289.  
  290. for (i=0; i < n; i++)
  291. {
  292. do
  293. {
  294. /* Get entered number */
  295. scanf("%d", &user_num);
  296. if (user_num < 30 && user_num > -30)
  297. {
  298. p_array[i] = user_num;
  299. }
  300. else
  301. {
  302. printf("Wrong number, try again!\n");
  303. }
  304. }while(!(user_num < 30 && user_num > -30));
  305. }
  306.  
  307. printf("End of dyamic array.\n");
  308.  
  309. return p_array;
  310. }
  311.  
  312. void print_dynamic(int * p, int n)
  313. {
  314. int i;
  315.  
  316. printf("The dynamic array:\n");
  317.  
  318. for (i=0; i<n; i++)
  319. {
  320. printf("%d ", p[i]);
  321. }
  322. printf("\n\n");
  323. }
  324.  
  325. int checking(int * arr, int n, int * h)
  326. {
  327. int i, j;
  328. int sum_left = 0, sum_right = 0;
  329.  
  330. for (i = 0; i < n; i++)
  331. {
  332. sum_left += arr[i]; /* sum of numbers from left to right */
  333.  
  334. sum_right = 0;
  335.  
  336. for (j = n-1; j > i; j--)
  337. {
  338. sum_right += arr[j]; /* Sum of numbers from right ot left */
  339. }
  340.  
  341. if (sum_left == sum_right)
  342. {
  343. *h = i; /* "*h" point to the adress of "h" that in main function, so its changes value of "h" that in main func */
  344.  
  345. return 1;
  346. }
  347. }
  348.  
  349. return 0;
  350. }
  351.  
  352. /* Function for cleaning array */
  353. int * cleanning (int * arr, int k, int * n)
  354. {
  355. int i, members=0, t=*n, z=0;
  356. int * new_p_array;
  357.  
  358. /* Her i am just count elements for new array */
  359. for (i = 0; i < t; i++)
  360. {
  361. if (arr[i] % k)
  362. {
  363. members++;
  364. }
  365. }
  366.  
  367. /* Give variable n value of members, using pointer (insert instantly to the memory adress) */
  368. *n = members;
  369.  
  370. /* Creating new array */
  371. new_p_array = (int*) malloc(*n*sizeof(int));
  372.  
  373. /* Validating of memory */
  374. if(new_p_array == NULL)
  375. {
  376. fprintf(stderr, "Out of memory, exiting\n");
  377. exit(1);
  378. }
  379.  
  380. /* Here I copy elements from old array to new */
  381. for (i = 0; i < t; i++)
  382. {
  383. if (arr[i] % k != 0)
  384. {
  385. new_p_array[z] = arr[i];
  386. z++;
  387. }
  388. }
  389.  
  390. /* Deleting old array */
  391. free(arr);
  392.  
  393. /* Return array (pointer?, adress?) something like that, because function defined with star (int * cleanning bla bla bla */
  394. return new_p_array;
  395. }

Report this snippet  

You need to login to post a comment.