Posted By

AlexanderRavikovich on 12/16/11


Tagged


Versions (?)

Work 2 in C


 / Published in: C
 

Home work 2

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define ARR_SIZE 101
  5. #define M 3
  6. #define N 3
  7.  
  8. void main()
  9. {
  10. /*
  11. i, j, a, b, z, c - counters in loops
  12. */
  13.  
  14. int numbers_arr[ARR_SIZE];
  15. int user_input, i=0, j=0, a=0, b=0, z=0, p=0, c=0, count=0, average=0, sum=0, arr_size=0, t, again = 1;
  16. char str[81];
  17. int math[M][N];
  18. int max_sum=0, math_start_i=0, math_start_j=0, row_id=0, col_id=0;
  19. int avr[M], avr_sorted[M];
  20.  
  21. /******************************** ITEM 1 *******************************/
  22.  
  23. printf("Item 1\n");
  24.  
  25. do
  26. {
  27. count = 0; // reset for use in loop
  28. sum = 0;
  29. i = 0;
  30.  
  31. printf("Enter maximum 100 positive integers and negative one to end input:\n");
  32.  
  33. do
  34. {
  35. /*
  36. Get positive numbers from user, maximum 100 to array. To end input, write an negative number.
  37. */
  38. scanf("%d", &user_input);
  39.  
  40. if (user_input >= 0)
  41. {
  42. numbers_arr[i] = user_input;
  43. i++;
  44.  
  45. sum += user_input;
  46. }
  47. else
  48. {
  49. printf("Array finish!\n");
  50. }
  51. }
  52. while(!(user_input < 0));
  53.  
  54. average = sum/(i);
  55.  
  56. arr_size = i;
  57.  
  58. printf("\nAverage is:%d\n", average);
  59.  
  60. for (i=0; i < arr_size; i++)
  61. {
  62. if (numbers_arr[i] > average)
  63. {
  64. c = 0;
  65. for (j = 2; j < numbers_arr[i]; j++)
  66. {
  67. p = numbers_arr[i] % j;
  68. if (p == 0)
  69. {
  70. c = 1;
  71. break;
  72. }
  73. }
  74.  
  75. if (c == 0)
  76. {
  77. printf("\nNum is:%d\n", numbers_arr[i]);
  78. }
  79. }
  80. }
  81.  
  82. /* Find numbers that n-1 < n < n+1*/
  83. for (i=1; i < arr_size; i++)
  84. {
  85. if (numbers_arr[i+1] > numbers_arr[i] && numbers_arr[i-1] > numbers_arr[i])
  86. {
  87. count++;
  88. }
  89. }
  90.  
  91. printf("\nThe number of little numbers is:%d\n", count);
  92.  
  93. printf("Item 1 again ? (yes = 1 / no = 0)\n");
  94. scanf("%d", &again);
  95. getchar(); //flush the keyboard buffer of the '\n' key
  96. }
  97. while (again);
  98.  
  99. again = 1; // reset for the next use
  100.  
  101.  
  102. /******************************** ITEM 2 *******************************/
  103. /* Remove extra backspaces from string */
  104.  
  105. printf("Item 2\n");
  106.  
  107. do
  108. {
  109. /* Promt user to enter data */
  110. printf("Enter string to 80 characters\n\n");
  111. /* Put user's data to str var */
  112. gets(str);
  113. /* reset i for counting in next while */
  114. i = 0;
  115. /* Go over array untill we get 0 (end of entred string) */
  116. while(str[i] != 0)
  117. {
  118. /* If char is not backspace, print him */
  119. if (str[i] != ' ')
  120. {
  121. printf("%c", str[i]);
  122. }
  123. else if (i != 0 && str[i-1] != ' ')
  124. {
  125. /* If char is backspace, check if previous char in array wasnt backspace too. If not - print it */
  126. printf("%c", str[i]);
  127. }
  128. i++;
  129. }
  130.  
  131. printf("\nItem 2 again ? (yes = 1 / no = 0)\n");
  132. scanf("%d", &again);
  133. getchar(); //flush the keyboard buffer of the '\n' key
  134. }
  135. while (again);
  136.  
  137. again = 1; // reset for the next use
  138.  
  139. /************** ITEM 3 **************/
  140.  
  141. printf("Item 3\n");
  142.  
  143. do
  144. {
  145. sum = 0;
  146.  
  147. /* Prom to user enter data */
  148. printf("Enter positive integer smaller than 2000:\n");
  149. /* Loop twice to fill our matrix */
  150. for (i=0; i < M; i++)
  151. {
  152. for (j=0; j < N; j++)
  153. {
  154. /* If user entered wrong number, kepp him in the while until he enter right number */
  155. do
  156. {
  157. /* Get what user entered */
  158. scanf("%d", &user_input);
  159. if (user_input >= 0 && user_input < 2000)
  160. {
  161. /* Put number to the matrix */
  162. math[i][j] = user_input;
  163. }
  164. else
  165. {
  166. printf("Wrong number entered! Try again!\n");
  167. }
  168. }
  169. while(!(user_input >= 0 && user_input < 2000));
  170. }
  171. }
  172. /* Promt user to give number that smaller then N and M*/
  173. printf("Enter positive integer small than %d and %d:\n", N, M);
  174. /* Keep user in loop until he enter right number */
  175. do
  176. {
  177. /* Get user entered data */
  178. scanf("%d", &user_input);
  179.  
  180. if (!(user_input < N && user_input < M))
  181. {
  182. printf("Wrong number! Try again!\n");
  183. }
  184. }while(!(user_input < N && user_input < M));
  185.  
  186. /* Loop out matrix to find possible combinations of matrix KxK & max sum of it's numbers */
  187. for (i=0; i <= M-user_input; i++)
  188. {
  189. for (j=0; j <= N-user_input; j++)
  190. {
  191. for (a=i; a<user_input+i; a++)
  192. {
  193. if (z == 0)
  194. {
  195. row_id = i;
  196. col_id = j;
  197. }
  198. for (b=j; b<user_input+j; b++)
  199. {
  200. sum += math[a][b];
  201. }
  202. z++;
  203. }
  204.  
  205. if (sum > max_sum)
  206. {
  207. math_start_i = row_id;
  208. math_start_j = col_id;
  209. max_sum = sum;
  210. }
  211. z = 0;
  212. sum = 0;
  213. }
  214. }
  215.  
  216. /* Print original math */
  217. for (i=0; i < M; i++)
  218. {
  219. for (j=0; j < N; j++)
  220. {
  221. printf("%d\t", math[i][j]);
  222. }
  223. printf("\n\n");
  224. }
  225.  
  226. printf("\n\n");
  227.  
  228. /* Printing sub math with max sum of numbers from top left ij saved in privious steps */
  229. for (a=math_start_i; a<user_input+math_start_i; a++)
  230. {
  231. for (b=math_start_j; b<user_input+math_start_j; b++)
  232. {
  233. printf("%d\t", math[a][b]);
  234. }
  235. printf("\n\n");
  236. }
  237.  
  238. printf("\nItem 3 again ? (yes = 1 / no = 0)\n");
  239. scanf("%d", &again);
  240. getchar(); //flush the keyboard buffer of the '\n' key
  241. }
  242. while (again);
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249. ///******************* ITEM 4 *************/
  250. again = 1; // reset for the next use
  251.  
  252. /************** ITEM 4 **************/
  253.  
  254. printf("Item 4\n");
  255.  
  256. do
  257. {
  258.  
  259.  
  260. /* Promt to user enter data */
  261. printf("Enter integers between -800 to 800:\n");
  262. /* Loop for twice to fill our matrix */
  263. for (i=0; i < M; i++)
  264. {
  265. sum = 0;
  266. for (j=0; j < N; j++)
  267. {
  268. /* If user entered wrong number, keep him in the while until he enter right number */
  269. do
  270. {
  271. /* Get what user entered */
  272. scanf("%d", &user_input);
  273. if (user_input >= -800 && user_input < 800)
  274. {
  275. /* Put number to the matrix */
  276. math[i][j] = user_input;
  277. }
  278. else
  279. {
  280. printf("Wrong number entered! Try again!\n");
  281. }
  282. }
  283. while(!(user_input >= -800 && user_input < 800));
  284. sum += user_input;
  285. }
  286. avr[i] = sum/N;
  287. }
  288.  
  289. /* Put math to the screen */
  290. printf("\n\n");
  291. /* Loop our matrix */
  292. for (i=0; i < M; i++)
  293. {
  294. /* Row by row printing */
  295. for (j=0; j < N; j++)
  296. {
  297. printf("%d\t", math[i][j]);
  298. }
  299. printf("\n");
  300. }
  301.  
  302. printf("\n");
  303. /* Loop for print evrage */
  304. for (i=0; i < M; i++)
  305. {
  306. /* Row by row printing */
  307. printf("%d\t", avr[i]);
  308.  
  309. }
  310. printf("\n\n\n");
  311.  
  312. /* Create copy of avr array */
  313. for (i=0; i<M; i++)
  314. {
  315. avr_sorted[i]=avr[i];
  316. }
  317.  
  318. /* Bubble sorting "avr_sorted" array math */
  319. for (i=0; i < M; i++)
  320. {
  321. for(j = M-1; j >= i; --j)
  322. {
  323. /* compare elements */
  324. if(avr_sorted[j-1] > avr_sorted[j])
  325. {
  326. /* exchange elements */
  327. t = avr_sorted[j-1];
  328. avr_sorted[j-1] = avr_sorted[j];
  329. avr_sorted[j] = t;
  330. }
  331. }
  332. }
  333.  
  334. /* Printing math row by avrage, not very well solution, but works =)) */
  335. /* Loop for "avr_sorted" array */
  336. for (i=0; i < M; i++)
  337. {
  338. /* Loop for oroginal "avr" array */
  339. for (z=0; z < M; z++)
  340. {
  341. /* If we found average from sorted array in original array, so
  342. print math row, that have index of math array (row) the same as index of avr array,
  343. that have the same average in sorted array */
  344. if (avr_sorted[i] == avr[z])
  345. {
  346. for (j=0; j < N; j++)
  347. {
  348. /* Row by row printing */
  349. printf("%d\t", math[z][j]);
  350. }
  351. printf("\n");
  352. break;
  353. }
  354. }
  355. }
  356.  
  357. printf("\nItem 4 again ? (yes = 1 / no = 0)\n");
  358. scanf("%d", &again);
  359. getchar(); //flush the keyboard buffer of the '\n' key
  360. }
  361. while (again);
  362. }

Report this snippet  

You need to login to post a comment.