answer04.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Please fill the functions in this file.
  3. * You can add additional functions.
  4. *
  5. * Hint:
  6. * You can write additonal functions.
  7. * You can test your functions with your own input file.
  8. * See details in README or typing command ./pa04 in terminal after make.
  9. * See output format examples in any of the files in directory expected.
  10. *
  11. * You may create additional arrays if needed. The maximum size
  12. * needed is specified by MAXLENGTH in pa04.h.
  13. */
  14. #include "pa04.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. /*
  18. * =================================================================
  19. * This function prints an array formatted as a partition.
  20. */
  21. void partPrint(int *part, int index)
  22. {
  23. int i;
  24. for(i = 0; i < index; i++)
  25. {
  26. if(i == 0)
  27. printf("= %d", part[i]); // print '=' in front of 0th index
  28. else
  29. printf(" + %d", part[i]); // print '+' in front of other indices
  30. }
  31. printf("\n");
  32. }
  33. /*
  34. * =================================================================
  35. * This function prints all partitions of a positive integer value
  36. * For example, if the value is 3:
  37. *
  38. * partitionAll 3
  39. * = 1 + 1 + 1
  40. * = 1 + 2
  41. * = 2 + 1
  42. * = 3
  43. */
  44. void partitionAllHelp(int *part, int value, int index)
  45. {
  46. if (value == 0)
  47. {
  48. partPrint(part, index); // print last (initial) value
  49. return;
  50. }
  51. int i;
  52. for (i = 1; i <= value; i++)
  53. {
  54. part[index] = i;
  55. partitionAllHelp(part, value - i, index + 1); // partition other values
  56. }
  57. }
  58. void partitionAll(int value)
  59. {
  60. printf("partitionAll %d\n", value);
  61. int part[MAXLENGTH]; // create array to store partition ints
  62. partitionAllHelp(part, value, 0); // call helper function
  63. }
  64. /*
  65. * =================================================================
  66. * This function prints the partitions that use increasing values.
  67. *
  68. * For example, if value is 5
  69. * 2 + 3 and
  70. * 1 + 4 are valid partitions
  71. *
  72. * 5 is a valid partition
  73. *
  74. * 1 + 1 + 3 and
  75. * 2 + 1 + 2 and
  76. * 3 + 2 are invalid partitions.
  77. *
  78. * The program should generate only valid partitions. Do not
  79. * generates invalid partitions and checks validity before printing.
  80. *
  81. */
  82. void partitionIncreasingHelp(int *part, int value, int index, int iIn)
  83. {
  84. if (value == 0)
  85. {
  86. partPrint(part, index);
  87. return;
  88. }
  89. int i;
  90. for (i = iIn++; i <= value; i++) // start with previous value of i
  91. {
  92. part[index] = i;
  93. partitionIncreasingHelp(part, value - i, index + 1, iIn++);
  94. }
  95. }
  96. void partitionIncreasing(int value)
  97. {
  98. printf("partitionIncreasing %d\n", value);
  99. int part[MAXLENGTH];
  100. partitionIncreasingHelp(part, value, 0, 1);
  101. }
  102. /*
  103. * =================================================================
  104. * This function prints the partitions that use Decreasing values.
  105. *
  106. * For example, if value is 5
  107. * 3 + 2 and
  108. * 4 + 1 are valid partitions
  109. *
  110. * 5 is a valid partition
  111. *
  112. * 1 + 1 + 3 and
  113. * 2 + 1 + 2 and
  114. * 2 + 3 are invalid partitions.
  115. *
  116. * The program should generate only valid partitions. Do not
  117. * generates invalid partitions and checks validity before printing.
  118. *
  119. */
  120. void partitionDecreasingHelp(int *part, int value, int index, int iIn)
  121. {
  122. if (value == 0)
  123. {
  124. partPrint(part, index);
  125. return;
  126. }
  127. int i;
  128. for (i = iIn--; i > 0; i--) // reverse partitionDecreasing
  129. {
  130. part[index] = i;
  131. partitionDecreasingHelp(part, value - i, index + 1, iIn--);
  132. }
  133. }
  134. void partitionDecreasing(int value)
  135. {
  136. printf("partitionDecreasing %d\n", value);
  137. int part[MAXLENGTH];
  138. partitionDecreasingHelp(part, value, 0, value);
  139. }
  140. /*
  141. * =================================================================
  142. * This function prints odd number only partitions of a positive integer value
  143. * For example, if value is 5
  144. * 1 + 1 + 1 + 1 + 1 and
  145. * 1 + 3 + 1 are valid partitions
  146. *
  147. * 5 is a valid partition
  148. *
  149. * 1 + 1 + 1 + 2 and
  150. * 2 + 1 + 2 and
  151. * 2 + 3 are invalid partitions.
  152. *
  153. * The program should generate only valid partitions. Do not
  154. * generates invalid partitions and checks validity before printing.
  155. */
  156. void partitionOddHelp(int *part, int value, int index)
  157. {
  158. if (value == 0)
  159. {
  160. partPrint(part, index);
  161. return;
  162. }
  163. int i;
  164. for (i = 1; i <= value; i = i + 2) // increment by 2 for only odd numbers
  165. {
  166. part[index] = i;
  167. partitionOddHelp(part, value - i, index + 1);
  168. }
  169. }
  170. void partitionOdd(int value)
  171. {
  172. printf("partitionOdd %d\n", value);
  173. int part[MAXLENGTH];
  174. partitionOddHelp(part, value, 0);
  175. }
  176. /*
  177. * =================================================================
  178. * This function prints even number only partitions of a positive integer value
  179. * For example, if value is 8
  180. * 2 + 2 + 2 + 2and
  181. * 2 + 4 + 2 are valid partitions
  182. *
  183. * 8 is a valid partition
  184. *
  185. * 2 + 1 + 1 + 2 + 2and
  186. * 2 + 1 + 2 + 3and
  187. * 5 + 3 are invalid partitions.
  188. *
  189. * if the value is 5, there will be no result generated
  190. *
  191. * The program should generate only valid partitions. Do not
  192. * generates invalid partitions and checks validity before printing.
  193. */
  194. void partitionEvenHelp(int *part, int value, int index)
  195. {
  196. if (value == 0)
  197. {
  198. partPrint(part, index);
  199. return;
  200. }
  201. int i;
  202. for (i = 2; i <= value; i = i + 2) // start with 2 and increment by 2 for only even numbers
  203. {
  204. part[index] = i;
  205. partitionEvenHelp(part, value - i, index + 1);
  206. }
  207. }
  208. void partitionEven(int value)
  209. {
  210. printf("partitionEven %d\n", value);
  211. int part[MAXLENGTH];
  212. partitionEvenHelp(part, value, 0);
  213. }
  214. /*
  215. * =================================================================
  216. * This function prints alternate odd and even number partitions of a positive integer value. Each partition starts from and odd number, even number, ood number again, even number again...etc.
  217. *
  218. * For example, if value is 6
  219. * 1 + 2 + 1 + 2 and
  220. * 3 + 2 + 1 are valid partitions
  221. *
  222. * 6 is not a valid partition
  223. *
  224. * 2 + 1 + 1 + 2 and
  225. * 2 + 1 + 3and
  226. * 5 + 1 are invalid partitions.
  227. *
  228. * The program should generate only valid partitions. Do not
  229. * generates invalid partitions and checks validity before printing.
  230. */
  231. void partitionOddAndEvenHelp(int *part, int value, int index, int iIn)
  232. {
  233. if (value == 0)
  234. {
  235. partPrint(part, index);
  236. return;
  237. }
  238. int i;
  239. for (i = iIn; i <= value; i = i + (iIn % 2) + 1) // start with odd (1)
  240. { // increment by opposite
  241. if(i % 2 == 0 && iIn % 2 == 0) // check if i and iIn are even
  242. {
  243. part[index] = i;
  244. partitionOddAndEvenHelp(part, value - i, index + 1, iIn - 1);
  245. }
  246. if(i % 2 == 1 && iIn % 2 == 1) // check if i and iIn are odd
  247. {
  248. part[index] = i;
  249. partitionOddAndEvenHelp(part, value - i, index + 1, iIn + 1);
  250. }
  251. }
  252. }
  253. void partitionOddAndEven(int value)
  254. {
  255. printf("partitionOddAndEven %d\n", value);
  256. int part[MAXLENGTH];
  257. partitionOddAndEvenHelp(part, value, 0, 1);
  258. }
  259. /*
  260. * =================================================================
  261. * This function returns 1 if a value is prime and 0 if it is not.
  262. */
  263. int isPrime(int value)
  264. {
  265. if (value <= 1)
  266. return 0; // return 0 if value is less than 2 (not prime)
  267. int i;
  268. for (i = 2; i * i <= value; i++) // start with 2, go until sqrt(value)
  269. {
  270. if (value % i == 0) // check value for divisibility
  271. return 0; // return 0 if evenly divisible value is found
  272. }
  273. return 1; // return 1 if value is prime
  274. }
  275. /*
  276. * =================================================================
  277. * This function prints prime number only partitions of a positive integer value
  278. * For example, if value is 6
  279. * 2 + 2 + 2 and
  280. * 3 + 3 are valid partitions
  281. *
  282. * 6 is not a valid partition
  283. *
  284. * 2 + 4 and
  285. * 1 + 5 are invalid partitions.
  286. *
  287. * The program should generate only valid partitions. Do not
  288. * generates invalid partitions and checks validity before printing.
  289. */
  290. void partitionPrimeHelp(int *part, int value, int index)
  291. {
  292. if (value == 0)
  293. {
  294. partPrint(part, index);
  295. return;
  296. }
  297. int i;
  298. for (i = 2; i <= value; i++)
  299. {
  300. if(isPrime(i)) // check for primeness
  301. {
  302. part[index] = i;
  303. partitionPrimeHelp(part, value - i, index + 1);
  304. }
  305. }
  306. }
  307. void partitionPrime(int value)
  308. {
  309. printf("partitionPrime %d\n", value);
  310. int part[MAXLENGTH];
  311. partitionPrimeHelp(part, value, 0);
  312. }