Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "StructAndSignatures.h"
  5.  
  6. parents_t* createParentsArray(int *size) // size is a pointer that will count the size of the array
  7. {
  8. // define array to input name
  9. char lastName[MAX_NAME];
  10. // define choice
  11. char choice;
  12. int physSize=1, numKids;
  13. // define empty parents array
  14. parents_t *arr=NULL;
  15. *size=0;
  16. do
  17. {
  18. // input choice
  19. printf("Do you wish to enter parents? (y/n) : ");
  20. scanf("%c",&choice);
  21. printf("\n");
  22. flushall();
  23. if(choice=='n')
  24. {
  25. break;
  26. }
  27. else if(choice=='y')
  28. {
  29. // input last name
  30. printf("Enter parent's last name: ");
  31. gets(lastName);
  32. // input number of kids
  33. printf("Enter number of kids: ");
  34. scanf("%d",&numKids);
  35. // increase parents array by 1
  36. arr=(parents_t*)realloc(arr,physSize*sizeof(parents_t));
  37. // check if allocation succeeded
  38. if(!arr)
  39. {
  40. printf("Memory allocation didn't succeed! \n");
  41. exit(1);
  42. }
  43. // allocate memory and put last name in array
  44. arr[physSize-1].lastName = strdup(lastName);
  45. // check if allocation succeeded
  46. if(!arr[physSize-1].lastName)
  47. {
  48. printf("Memory allocation didn't succeed! \n");
  49. exit(1);
  50. }
  51. // put number of kids in the parents array
  52. arr[physSize-1].numberOfKids=numKids;
  53. // increase physical size by 1
  54. physSize++;
  55. // increase size by 1
  56. (*size)++;
  57. printf("\n");
  58. flushall();
  59. }
  60. else
  61. {
  62. printf("Not an option, please choose a different option \n\n");
  63. }
  64. }while(1);
  65. return arr;
  66. }
  67.  
  68. // print the parents devided by catgorys
  69. void printParents(parents_t*** arr, int size)
  70. {
  71. int i, j;
  72. for(i=0;i<size;i++)
  73. {
  74. if(i==GROUP5)
  75. {
  76. printf("Parents with 5 kids or more: \n");
  77. }
  78. else if(i==GROUP4)
  79. {
  80. printf("Parents with 2 - 4 kids: \n");
  81. }
  82. else
  83. {
  84. printf("Parents with 2 kids or less: \n");
  85. }
  86. for(j=0;arr[i][j]!=NULL;j++)
  87. {
  88. printf("Last name: %s | Number Of kids: %d \n",arr[i][j]->lastName, arr[i][j]->numberOfKids);
  89. }
  90. printf("\n");
  91. }
  92. }
  93.  
  94. parents_t*** sort(parents_t *arr, int size)
  95. {
  96. // define sizes to increase sizes of internal arrays
  97. int i, size5=1, size4=1, size2=1;
  98. // define constants
  99. const int KIDS5=5, KIDS4=4, KIDS2=2;
  100. // aloocate memeory that conatins pointer to parents
  101. parents_t ***groups=(parents_t***)calloc(PARENTS_GROUP,sizeof(parents_t**));
  102. // check if the allcoation suecceeded
  103. if(!groups)
  104. {
  105. printf("Memory allocation didn't succeed! \n");
  106. exit(1);
  107. }
  108. for(i=0;i<size;i++)
  109. {
  110. if(arr[i].numberOfKids>=KIDS5)
  111. {
  112. // increase parents array in group 5 by 1
  113. groups[GROUP5]=(parents_t**)realloc(groups[GROUP5],sizeof(parents_t*)*size5);
  114. // check if allocation succeeded
  115. if(!groups[GROUP5])
  116. {
  117. printf("Memory allocation didn't succeed! \n");
  118. exit(1);
  119. }
  120. // put the parrent pointer in group 5
  121. groups[GROUP5][size5-1]=&arr[i];
  122. size5++;
  123. }
  124. else if(arr[i].numberOfKids>KIDS2 && arr[i].numberOfKids<=KIDS4)
  125. {
  126. // increase parents array in group 4 by 1
  127. groups[GROUP4]=(parents_t**)realloc(groups[GROUP4],sizeof(parents_t*)*size4);
  128. // check if allocation succeeded
  129. if(!groups[GROUP4])
  130. {
  131. printf("Memory allocation didn't succeed! \n");
  132. exit(1);
  133. }
  134. // put the parrent pointer in group 4
  135. groups[GROUP4][size4-1]=&arr[i];
  136. size4++;
  137. }
  138. else
  139. {
  140. // increase parents array in group 2 by 1
  141. groups[GROUP2]=(parents_t**)realloc(groups[GROUP2],sizeof(parents_t*)*size2);
  142. // check if allocation succeeded
  143. if(!groups[GROUP2])
  144. {
  145. printf("Memory allocation didn't succeed! \n");
  146. exit(1);
  147. }
  148. // put the parrent pointer in group 2
  149. groups[GROUP2][size2-1]=&arr[i];
  150. size2++;
  151. }
  152. }
  153. // increase parents array in group 5 by 1
  154. groups[GROUP5]=(parents_t**)realloc(groups[GROUP5],sizeof(parents_t*)*size5);
  155. // check if allocation succeeded
  156. if(!groups[GROUP5])
  157. {
  158. printf("Memory allocation didn't succeed! \n");
  159. exit(1);
  160. }
  161. // increase parents array in group 4 by 1
  162. groups[GROUP4]=(parents_t**)realloc(groups[GROUP4],sizeof(parents_t*)*size4);
  163. // check if allocation succeeded
  164. if(!groups[GROUP4])
  165. {
  166. printf("Memory allocation didn't succeed! \n");
  167. exit(1);
  168. }
  169. // increase parents array in group 2 by 1
  170. groups[GROUP2]=(parents_t**)realloc(groups[2],sizeof(parents_t*)*size2);
  171. // check if allocation succeeded
  172. if(!groups[GROUP2])
  173. {
  174. printf("Memory allocation didn't succeed! \n");
  175. exit(1);
  176. }
  177. // put nulls at the end of every array group
  178. groups[GROUP5][size5-1]=NULL;
  179. groups[GROUP4][size4-1]=NULL;
  180. groups[GROUP2][size2-1]=NULL;
  181. return groups;
  182. }
  183.  
  184. void updateLastName(parents_t*** arr,int size, int originalParentsArrSize) // originalParentsArrSize is the size of parrents array
  185. {
  186. // define indexes and flag to continue or stop inout
  187. int i, j, flag = 1;
  188. // define array to input name
  189. char name[MAX_NAME];
  190. // define array to input updated name
  191. char updatedName[MAX_NAME];
  192. // define array to input choice
  193. char choice[MAX_CHOICE];
  194. const int MIN_ARR_SIZE = 0;
  195. // if the parents array is empty exit the function
  196. if(originalParentsArrSize == MIN_ARR_SIZE)
  197. {
  198. printf("No families to display");
  199. return;
  200. }
  201. do
  202. {
  203. // input choice
  204. printf("Enter 'y' to update last name, enter 'end' to end the program: ");
  205. gets(choice);
  206. printf("\n");
  207. if(!strcmp(choice,"end"))
  208. {
  209. break;
  210. }
  211. else if(!strcmp(choice,"y"))
  212. {
  213. // input the name to chnage
  214. printf("Enter last name you want to update: ");
  215. gets(name);
  216. for(i=0;i<size;i++)
  217. {
  218. for(j=0;arr[i][j]!=NULL;j++)
  219. {
  220. if(!strcmp(arr[i][j]->lastName, name))
  221. {
  222. // input the updated name
  223. printf("Enter updated last name: ");
  224. gets(updatedName);
  225. // free the aloocation of the old name
  226. free(arr[i][j]->lastName);
  227. // callocate memory and copy the new name
  228. arr[i][j]->lastName = strdup(updatedName);
  229. printf("\n");
  230. // print the parents devided by categories
  231. printParents(arr,size);
  232. flag=0;
  233. break;
  234. }
  235. }
  236. }
  237. if(flag)
  238. {
  239. printf("\nLast name didn't found\n\n");
  240. }
  241. flag=1;
  242. }
  243. else
  244. {
  245. printf("Not an option, please choose a different option \n\n");
  246. }
  247. }
  248. while(1);
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement