Advertisement
Guest User

FUCK ME I THINK

a guest
Jan 22nd, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.92 KB | None | 0 0
  1. /* Open Notes, Pair Exercise:
  2.  
  3. The following program is intended to continuously get
  4. an input integer from the user until the sentinel value -999.
  5. Other features intended to manipulate the data (like
  6. determining the largest and the smallest values, reversing the
  7. contents, and rotating the contents of the dynamic list) are also
  8. included in the features of this program.
  9.  
  10. Your task is to complete the program given the skeleton and
  11. the comments.
  12.  
  13. Indicate your section: S17
  14. Indicate below your names:
  15. Zach MARASIGAN
  16. Kyle SANTIAGO
  17.  
  18. */
  19.  
  20. /* include the necessary libraries */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23.  
  24. /* This function will copy nElem number of entries starting from the
  25. given starting address stored in pSource to an destination dynamic
  26. list with the starting address stored in pDest.
  27.  
  28. Pre-condition:
  29. 1.) pSource is already pointing to a dynamic list containing
  30. nElem integer entries
  31. 2.) there is enough space allocated to store nElem
  32. entries at the address referred to by pDest
  33.  
  34. @param pSource starting address of the source dynamic list
  35. @param pDest starting address of the destination dynamic list
  36. @param nElem number of elements to copy from addresses pointed to
  37. by pSource to pDest
  38. */
  39. void
  40. copy (int *pSource, int *pDest, int nElem)
  41. { /* complete the requirements for this function */
  42. int i;
  43. for (i=0;i<nElem;i++)
  44. *(pDest+i)=*(pSource+i);
  45. }
  46.  
  47. /* This function will get input from the user as long as the
  48. input is not the sentinel value -999. Note that a larger
  49. capacity should be allocated for the list. You are required to
  50. meaningfully call and use the function copy() as part of your
  51. solution this function. Do not use realloc() for your
  52. solution. This function will also update the number of
  53. elements pointed to by pElem.
  54.  
  55. Pre-condition: *pList contains a valid address (i.e., NULL if
  56. not yet allocated any space and memory
  57. address of location allocated and stored
  58. with *pElem number of elements)
  59.  
  60. @param pList address where the pointer variable is stored
  61. @param pElem address where the number of elements have to be updated in
  62. */
  63. void
  64. getInputsV1(int **pList, int *pElem)
  65. {
  66. int *pTemp;
  67. int nInput;
  68.  
  69.  
  70. do {
  71. printf("Enter number: ");
  72. scanf("%d", &nInput);
  73. if (nInput != -999) {
  74. /* write your statement to allocate 1 space more than
  75. the current number of elements to be pointed to by
  76. pTemp */
  77. pTemp=malloc(sizeof(int)*((*pElem)+1));
  78.  
  79. if (pTemp != NULL) {
  80. if (*pList != NULL) {
  81. /* call function copy() to copy all previous contents
  82. of *pList to pTemp */
  83. copy (*pList,pTemp,*pElem);
  84. }
  85.  
  86. /* add / include the newest input nInput at the end
  87. of the list pointed to by pTemp */
  88. *(pTemp+*pElem)=nInput;
  89.  
  90. /* increment the number of elements */
  91. (*pElem)++;
  92.  
  93. /* deallocate the old space pointed to by *pList
  94. if *pList is previously pointing to a list*/
  95. if (*pList != NULL)
  96. free(*pList);
  97.  
  98. /* have *pList point to the same list as pTemp */
  99. *pList=pTemp;
  100. }
  101. }
  102. } while (nInput != -999);
  103. }
  104.  
  105. /* This function displays the contents of pList,
  106. given nElem number of elements. */
  107. void
  108. displayList (int *pList, int nElem)
  109. { /* complete the requirements of this function */
  110. int i;
  111. for (i=0;i<nElem;i++)
  112. printf("Value of %d is %d \n", i, *(pList+i));
  113. }
  114.  
  115. /* This function copies the contents of the list
  116. pointed to by pSource to pDest. */
  117. void
  118. reverseListV1 (int *pSource, int **pDest, int nElem)
  119. { /* complete the requirements of this function */
  120. *pDest = malloc(sizeof(int)*nElem);
  121. int i=0, temp=nElem-1;
  122. while(i<nElem){
  123. *(*pDest+i)=*(pSource+temp);
  124. temp--;
  125. i++;
  126. }
  127. }
  128.  
  129. /* This function updates via addresses in the parameter the
  130. largest and smallest values in the list pointed to
  131. by pList,given nElem number of elements. */
  132. void
  133. hiLow ( int *nHigh, int *nLow, int *pList, int nElem)
  134. { /* complete the requirements of this function */
  135. int i;
  136.  
  137. *nHigh = *pList;
  138. *nLow = *pList;
  139.  
  140.  
  141. for (i=0; i<nElem; i++){
  142. if (*(pList+i)>*nHigh)
  143. *nHigh=*(pList+i);
  144. else if (*(pList+i)<*nLow)
  145. *nLow=*(pList+i);
  146.  
  147. }
  148. }
  149.  
  150. /* Bonus 1: Create another version of a function that
  151. will get input from the user as long as the
  152. input is not the sentinel value -999. The difference of
  153. getInputsV1() with this is that, here, you are required
  154. to use realloc() for your solution. This function will still
  155. also update the number of elements pointed to by pElem.
  156.  
  157. Pre-condition: *pList contains a valid address (i.e., NULL if
  158. not yet allocated any space and memory
  159. address of location allocated and stored
  160. with *pElem number of elements)
  161.  
  162. @param pList address where the pointer variable is stored
  163. @param pElem address where the number of elements have to be updated in
  164. */
  165. void
  166. getInputsV2(int **pList, int *pElem)
  167. {
  168. int *pTemp;
  169. int nInput;
  170.  
  171. do {
  172. printf("Enter number: ");
  173. scanf("%d", &nInput);
  174. if (nInput != -999){
  175. /* write your statement to allocate 1 space more than
  176. the current number of elements to be pointed to by
  177. pTemp */
  178. pTemp=malloc(sizeof(int)*((*pElem)+1));
  179.  
  180. if (pTemp != NULL) {
  181. /* write statements to store the new nInput as part
  182. of the dynamic list
  183. */
  184.  
  185. if (*pList != NULL) {
  186. /* call function copy() to copy all previous contents
  187. of *pList to pTemp */
  188. copy (*pList,pTemp,*pElem);
  189. }
  190.  
  191.  
  192.  
  193. *pList = realloc(pTemp, sizeof(int)*((*pElem)+2));
  194. *(*pList+*pElem)=nInput;
  195. (*pElem)++;
  196.  
  197. }
  198. }
  199. } while (nInput != -999);
  200. }
  201.  
  202. /* Bonus 2: This RECURSIVE function copies the contents of the list
  203. pointed to by pSource to pDest in reverse order. */
  204. void
  205. reverseListV2 (int *pSource, int **pDest, int nElem,int i)
  206. { /* complete the requirements of this function */
  207. if (nElem>0){
  208. *(*pDest+(nElem-1))=*(pSource+i);
  209. reverseListV2 (pSource,pDest,nElem-1,i+1);
  210. }
  211. }
  212.  
  213.  
  214. int main()
  215. {
  216. int *pList, *pReverse;
  217. int nOpt, nElem = 0;
  218. float fAve;
  219. int nHigh, nLow, i=0;
  220.  
  221. pList = NULL;
  222.  
  223. do {
  224. printf("Menu: \n");
  225. printf("1 - Get Inputs\n");
  226. printf("2 - Display Contents of List\n");
  227. printf("3 - Get Highest and Lowest Values in List\n");
  228. printf("4 - Reverse Contents of List\n");
  229. printf("5 - Get Inputs, Realloc'd \n");
  230. printf("6 - Reverses, Recursively \n");
  231. /* add printf()'s for option 5 and 6 if the Bonus 1 and
  232. Bonus 2 are implemented.
  233.  
  234. */
  235.  
  236. printf("0 - Exit\n");
  237.  
  238. printf("\nEnter option: ");
  239. scanf("%d", &nOpt);
  240. switch (nOpt) {
  241. case 1 : getInputsV1 (&pList, &nElem); break;
  242. case 2 : displayList (pList,nElem); break;
  243. case 3 : hiLow (&nHigh, &nLow, pList, nElem);
  244. printf("Highest number is %d\n", nHigh);
  245. printf("Lowest number is %d\n", nLow); break;
  246. case 4 : reverseListV1 (pList, &pReverse, nElem);
  247. copy(pReverse, pList, nElem); break;
  248. case 5 : getInputsV2 (&pList, &nElem); break;
  249. case 6 : pReverse = malloc(sizeof(int)*nElem);
  250. reverseListV2 (pList, &pReverse, nElem, i);
  251. copy(pReverse, pList, nElem);
  252. break;
  253. default : exit(0);
  254. }
  255. /* provide the necessary statements to call the appropriate
  256. functions and to display the result depending on the
  257. option chosen. */
  258.  
  259.  
  260. } while (nOpt != 0);
  261.  
  262. /* deallocate any dynamically allocated memory */
  263. free(pList); free(pReverse);
  264.  
  265. return 0;
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement