Advertisement
TheLegend12

Untitled

Feb 6th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. void extinction(Org** web, int* numOrgs, int index) {
  2. //TODO (Task 3): remove the organism associated with [index] from web.
  3. // Inputs:
  4. // web - a dynamically allocated array of Orgs
  5. // numOrgs - number of organisms = size of web[]
  6. // index - organism index in web[] to remove
  7. // Outputs:
  8. // web - pointer passed-by-pointer; memory address of web array changes due to reallocation
  9. // numOrgs - passed-by-pointer; must be decremented since web[] loses an organism
  10. //
  11. // Remember to do the following:
  12. // 1. remove organism at index from web[] - DO NOT use realloc(), instead...
  13. // (a) free any malloc'd memory associated with organism at index; i.e. its prey[] subitem
  14. // (b) malloc new space for the array with the new number of Orgs
  15. // (c) copy all but one of the old array elements to the new array,
  16. // some require shifting forward to overwrite the organism at index
  17. // (d) free the old array
  18. // (e) update the array pointer to the new array
  19. // (f) update numOrgs
  20. // 2. remove index from all organisms' prey[] array subitems - DO NOT use realloc(), instead...
  21. // (a) search for index in all organisms' prey[] arrays; when index is found:
  22. // [i] malloc new space for the array with the new number of ints
  23. // [ii] copy all but one of the old array elements to the new array,
  24. // keeping the same order some require shifting forward
  25. // [iii] free the old array
  26. // [iv] update the array pointer to the new array
  27. // [v] update the numPrey subitem accordingly
  28. // (b) update all organisms' prey[] elements that are greater than index,
  29. // which have been shifted forward in the web array
  30. //
  31. // Edge case: check the size array being malloc'ed;
  32. // for a web with only one organism and
  33. // that orgranism goes extinct,
  34. // instead of malloc'ing an empty array,
  35. // explicitly set the pointer to NULL;
  36. // see the web[] allocation in main() as an example
  37. ///TODO: should remove "index" from "web" and lower "numOrg" by one.
  38.  
  39. if(index < 0 || index >= *numOrgs){
  40. printf("Invalid index for extinction. Terminating program...\n");
  41. exit(1);
  42. }
  43.  
  44. Org* extinctOrg = &(*web)[index];
  45.  
  46. free(extinctOrg->prey);
  47.  
  48. Org* newWeb = (Org*)malloc((*numOrgs - 1) * sizeof(Org));
  49.  
  50. for(int i = 0, j = 0; i < *numOrgs; ++i){
  51. if(i != index){
  52. newWeb[j++] = (*web)[i];
  53. }
  54. }
  55.  
  56. free(*web);
  57.  
  58. *web = newWeb;
  59.  
  60. (*numOrgs)--;
  61.  
  62. for(int i = 0; i < *numOrgs; ++i){
  63. Org* predator = &(*web)[i];
  64.  
  65. for(int j = 0; j < predator->numPrey; ++j){
  66. if (predator->prey[j] == index){
  67. int* newPrey = (int*)malloc((predator->numPrey - 1) * sizeof(int));
  68.  
  69. for(int k = 0, l = 0; k < predator->numPrey; ++k){
  70. if(k != j){
  71. newPrey[l++] = predator->prey[k] > index ? predator->prey[k] - 1: predator->prey[k];
  72. }
  73. }
  74.  
  75. free(predator->prey);
  76.  
  77. predator->prey = newPrey;
  78.  
  79. predator->numPrey--;
  80.  
  81. break;
  82.  
  83. }
  84. }
  85.  
  86. for(int j = 0; j < predator->numPrey; ++j){
  87. if (predator->prey[j] > index){
  88. predator->prey[j]--;
  89. }
  90. }
  91. }
  92. }
  93.  
  94. 1: Task 3 - extinction() unit test case - basic (2 organisms)
  95. 0 / 4
  96. Feedback
  97. extinction() does not correctly update prey[] subitems for basic food web1
  98. 2: Task 3 - extinction() unit test case - small (3 organisms)
  99. 0 / 4
  100. Feedback
  101. extinction() does not correctly update prey[] subitems for basic food web1
  102. 3: Task 3 - extinction() unit test case - medium (10 organisms) - full extinction to numOrgs = 0 & web = NULL
  103. 0 / 4
  104. Feedback
  105. extinction() does NOT correctly update prey[] subitems
  106. 4: Task 3 - extinction() unit test case - large (100 organisms)
  107. 0 / 4
  108. Feedback
  109. extinction() does not correctly update prey[] subitems for large food web
  110.  
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement