Posted By

AlexanderRavikovich on 01/10/12


Tagged


Versions (?)

Work 4 in C


 / Published in: C
 

Work 4 in C

Pointers Recursion

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <ctype.h>
  6.  
  7. #define ARR_SIZE 10
  8. #define STR_SIZE 10
  9. #define H 4
  10. #define N 8
  11. #define M 6
  12.  
  13. /* Prototype */
  14. void menu(); /* Menu function */
  15. int check_sort(int * arr, int size); /* Check if array is sorted DESC */
  16. void reducing(char * string); /* Takes string and print out only digits, in reverse order */
  17. int slope_rec(int data[][H], int index); /* Recursive function for checking matrix for slope */
  18. int isPositive(int data[][H], int index); /*check for positive numbers */
  19. int isNegative(int data[][H], int index); /* check for negative numbers */
  20. int check_zone(int nums[M][N], int x, int y, int num);
  21. int checking(int nums[M][N], int x, int y);
  22. int countZeros(int nums[M][N]); /* Help function to count zeros */
  23.  
  24. void main()
  25. {
  26. int i=0, j=0, user_num=0, arr[ARR_SIZE], option, data[H][H]={0};
  27. char str[STR_SIZE];
  28. int res = 0;
  29. int x, y;
  30.  
  31. /* Copy temp to nums, to run item 4 many times, because we are setting zeros in array */
  32.  
  33. int temp[M][N] = {
  34. {4, 3 ,2 ,3 ,1 ,3 ,2 ,1},
  35. {5, 3, 3, 2, 2, 2, 1, 2},
  36. {1 ,1 ,3 ,3, 2, 1, 2, 1},
  37. {1 ,3 ,3 ,2 ,4 ,3 ,3 ,2},
  38. {5 ,5 ,3 ,4 ,4 ,4 ,3 ,2},
  39. {5 ,3 ,4 ,2 ,2 ,2 ,3 ,2}
  40. };
  41.  
  42. int nums[M][N] = {0};
  43.  
  44. while(1)
  45. {
  46. /* Copy values to nums array */
  47. for (i=0; i<M; i++)
  48. {
  49. for (j=0; j<N; j++)
  50. {
  51. nums[i][j] = temp[i][j];
  52. }
  53. }
  54.  
  55. menu();
  56.  
  57. /* Get value of option choosed by user */
  58. scanf("%d", &option);
  59. getchar(); /* Hack for eating last \n */
  60.  
  61. switch (option)
  62. {
  63. case 1:
  64. printf("Enter array size %d for check sorting:\n", ARR_SIZE);
  65.  
  66. for (i = 0; i < ARR_SIZE; i++)
  67. {
  68. scanf("%d", &arr[i]);
  69. }
  70.  
  71. printf("\nThe array is:\n");
  72.  
  73. for (i = 0; i < ARR_SIZE; i++)
  74. {
  75. printf("%d ", arr[i]);
  76. }
  77.  
  78. res = check_sort(arr, ARR_SIZE); /* call function and set res the value it return */
  79.  
  80. if (res)
  81. {
  82. printf("\nThe array is sorted!\n");
  83. }
  84. else
  85. {
  86. printf("\nThe array is NO sorted!\n");
  87. }
  88. break;
  89.  
  90. case 2:
  91. printf("Enter string for reducing:\n");
  92.  
  93. gets(str); /* Insert string to char variable (array) */
  94. printf("\nThe reduced string is:\n");
  95. reducing(str); /* Call for function */
  96. printf("\n\n");
  97. break;
  98.  
  99. case 3:
  100. printf("Enter array size %d X %d for check slope:\n", H, H);
  101.  
  102. /* Fill array with numbers */
  103. for (i=0; i < H; i++)
  104. {
  105. printf("Enter row %d:\n", i);
  106.  
  107. for (j=0; j<H; j++)
  108. {
  109. scanf("%d", &data[i][j]);
  110. }
  111. }
  112.  
  113. printf("\nThe array is:\n");
  114.  
  115. /* Print matrix */
  116. for (i=0; i < H; i++)
  117. {
  118. printf("\n");
  119.  
  120. for (j=0; j<H; j++)
  121. {
  122. printf("%3d\t", data[i][j]);
  123. }
  124. }
  125.  
  126. printf("\n");
  127.  
  128. /* Call for function that checks if matrix is slope */
  129. res = slope_rec(data, 0);
  130.  
  131. if (res)
  132. {
  133. printf("The array is slope!\n");
  134. }
  135. else
  136. {
  137. printf("The array is NOT slope!\n");
  138. }
  139. break;
  140.  
  141. case 4:
  142. /* Get coordinates */
  143. do
  144. {
  145. printf("Enter the coordinates for number:\n");
  146.  
  147. /* Get values from user */
  148. scanf("%d", &x);
  149. scanf("%d", &y);
  150.  
  151. if (x < 0 || x > M-1 || y < 0 || y > N-1)
  152. {
  153. printf("Wrong number!\n");
  154. }
  155. } while(x < 0 || x > M-1 || y < 0 || y > N-1);
  156.  
  157. /* Print value of place (x,y) in matrix */
  158. printf("The size of the zone is : %d\n", nums[x][y]);
  159.  
  160. /* Reset variable for saving result */
  161. res = 0;
  162.  
  163. /* Call for recursive function */
  164. res = checking(nums , x , y);
  165.  
  166. printf("The size of the zone is : %d\n", res);
  167. break;
  168.  
  169. case 5:
  170. exit(1);
  171. break;
  172. }
  173. }
  174. }
  175.  
  176. int countZeros(int nums[M][N])
  177. {
  178. int count = 0, i, j;
  179.  
  180. for (i=0; i<M; i++)
  181. {
  182. for (j=0; j<N; j++)
  183. {
  184. if (!nums[i][j])
  185. {
  186. count++;
  187. }
  188. }
  189. }
  190.  
  191. return count;
  192. }
  193.  
  194. int checking(int nums[][N], int x, int y)
  195. {
  196. return check_zone(nums, x, y, nums[x][y]);
  197. }
  198.  
  199. /* Function that check values of recursively by x,y
  200.  
  201. set 0 if value is checked
  202.  
  203. [x-1,y-1] [x+1,y] [x+1,y+1]
  204. [ x,y-1] [ x,y ] [ x,y+1]
  205. [x-1,y-1] [x+1,y] [x+1,y+1]
  206.  
  207. */
  208. int check_zone(int nums[M][N], int x, int y, int num)
  209. {
  210. if (x < M-1 && nums[x+1][y] == num)
  211. {
  212. nums[x+1][y] = 0;
  213. check_zone(nums, x+1, y, num);
  214. }
  215.  
  216. if (x > 0 && nums[x-1][y] == num)
  217. {
  218. nums[x-1][y] = 0;
  219. check_zone(nums, x-1, y, num);
  220. }
  221.  
  222. if (y > 0 && nums[x][y-1] == num)
  223. {
  224. nums[x][y-1] = 0;
  225. check_zone(nums, x, y-1, num);
  226. }
  227.  
  228. if (y < N-1 && nums[x][y+1] == num)
  229. {
  230. nums[x][y+1] = 0;
  231. check_zone(nums, x, y+1, num);
  232. }
  233.  
  234. /* Diagonal */
  235.  
  236. if (x < M-1 && y < N-1 && nums[x+1][y+1] == num)
  237. {
  238. nums[x+1][y+1] = 0;
  239. check_zone(nums, x+1, y+1, num);
  240. }
  241.  
  242. if (x > 0 && y > 0 && nums[x-1][y-1] == num)
  243. {
  244. nums[x-1][y-1] = 0;
  245. check_zone(nums, x-1, y-1, num);
  246. }
  247.  
  248. if (x > 0 && y < N-1 && nums[x-1][y+1] == num)
  249. {
  250. nums[x-1][y+1] = 0;
  251. check_zone(nums, x-1, y+1, num);
  252. }
  253.  
  254. if (x < M-1 && y > 0 && nums[x+1][y-1] == num)
  255. {
  256. nums[x+1][y-1] = 0;
  257. check_zone(nums, x+1, y-1, num);
  258. }
  259.  
  260. /* Now count zeros & return value */
  261. return countZeros(nums);
  262. }
  263. /* Checking if matrix is slope */
  264.  
  265. int slope_rec(int data[][H], int index)
  266. {
  267. if (index < H)
  268. {
  269. /* If value in diagonal not 0, return 0 - no reason to continue */
  270. if (data[index][index] != 0)
  271. {
  272. return 0;
  273. }
  274.  
  275. /* If not stopped before, checking numbers from left to right for num >= 0 */
  276. if (!isPositive(data, index))
  277. {
  278. return 0;
  279. }
  280.  
  281. /* If not stopped before, checking numbers from left to right for num < 0 */
  282. if (!isNegative(data, index))
  283. {
  284. return 0;
  285. }
  286.  
  287. /* Call recursively for funcion, to check next diagonal value */
  288. return slope_rec(data, index+1);
  289. }
  290. else
  291. {
  292. return 1;
  293. }
  294. }
  295.  
  296. /* If value is < 0 return 0, if not - 1 */
  297. int isPositive(int data[][H], int index)
  298. {
  299. int i = index;
  300.  
  301. for (i = index; i < H-index; i++)
  302. {
  303. if (data[index][i] < 0)
  304. {
  305. return 0;
  306. }
  307. }
  308.  
  309. return 1;
  310. }
  311.  
  312. /* If value is > -1 return 0, if not - return 1 */
  313. int isNegative(int data[][H], int index)
  314. {
  315. int i;
  316.  
  317. for (i = 0; i < index; i++)
  318. {
  319. if (data[index][i] > -1)
  320. {
  321. return 0;
  322. }
  323. }
  324.  
  325. return 1;
  326. }
  327.  
  328. /* Function that checks if value is a number, and prints it.
  329. access value of array by pointer, and incrising adress of array's keys
  330.  
  331. first we increase adress, and after all loops are ended,
  332. we print value from the end to begin
  333. */
  334.  
  335. void reducing(char *string)
  336. {
  337. if (*string != '\0')
  338. {
  339. reducing(string+1);
  340.  
  341. if (isdigit(*string))
  342. {
  343. printf("%c", *string);
  344. }
  345. }
  346. }
  347.  
  348. /* Function that checks if array is sorted DESC */
  349. int check_sort(int * arr, int size)
  350. {
  351. if (size > 0)
  352. {
  353. if (arr[size-1] > arr[size-2])
  354. {
  355. return check_sort(arr, size-1);
  356. }
  357. else
  358. {
  359. return 0;
  360. }
  361. }
  362. else
  363. {
  364. return 1;
  365. }
  366. }
  367.  
  368. /* Function that prints options */
  369. void menu()
  370. {
  371. printf("Choose function:\n"
  372. "1-Check sorting\n"
  373. "2-Reducing\n"
  374. "3-Sloping \n"
  375. "4-Check zone\n"
  376. "5-Exit\n");
  377. }

Report this snippet  

You need to login to post a comment.