Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void extinction(Org** web, int* numOrgs, int index) {
- //TODO (Task 3): remove the organism associated with [index] from web.
- // Inputs:
- // web - a dynamically allocated array of Orgs
- // numOrgs - number of organisms = size of web[]
- // index - organism index in web[] to remove
- // Outputs:
- // web - pointer passed-by-pointer; memory address of web array changes due to reallocation
- // numOrgs - passed-by-pointer; must be decremented since web[] loses an organism
- //
- // Remember to do the following:
- // 1. remove organism at index from web[] - DO NOT use realloc(), instead...
- // (a) free any malloc'd memory associated with organism at index; i.e. its prey[] subitem
- // (b) malloc new space for the array with the new number of Orgs
- // (c) copy all but one of the old array elements to the new array,
- // some require shifting forward to overwrite the organism at index
- // (d) free the old array
- // (e) update the array pointer to the new array
- // (f) update numOrgs
- // 2. remove index from all organisms' prey[] array subitems - DO NOT use realloc(), instead...
- // (a) search for index in all organisms' prey[] arrays; when index is found:
- // [i] malloc new space for the array with the new number of ints
- // [ii] copy all but one of the old array elements to the new array,
- // keeping the same order some require shifting forward
- // [iii] free the old array
- // [iv] update the array pointer to the new array
- // [v] update the numPrey subitem accordingly
- // (b) update all organisms' prey[] elements that are greater than index,
- // which have been shifted forward in the web array
- //
- // Edge case: check the size array being malloc'ed;
- // for a web with only one organism and
- // that orgranism goes extinct,
- // instead of malloc'ing an empty array,
- // explicitly set the pointer to NULL;
- // see the web[] allocation in main() as an example
- ///TODO: should remove "index" from "web" and lower "numOrg" by one.
- if(index < 0 || index >= *numOrgs){
- printf("Invalid index for extinction. Terminating program...\n");
- exit(1);
- }
- Org* extinctOrg = &(*web)[index];
- free(extinctOrg->prey);
- Org* newWeb = (Org*)malloc((*numOrgs - 1) * sizeof(Org));
- for(int i = 0, j = 0; i < *numOrgs; ++i){
- if(i != index){
- newWeb[j++] = (*web)[i];
- }
- }
- free(*web);
- *web = newWeb;
- (*numOrgs)--;
- for(int i = 0; i < *numOrgs; ++i){
- Org* predator = &(*web)[i];
- for(int j = 0; j < predator->numPrey; ++j){
- if (predator->prey[j] == index){
- int* newPrey = (int*)malloc((predator->numPrey - 1) * sizeof(int));
- for(int k = 0, l = 0; k < predator->numPrey; ++k){
- if(k != j){
- newPrey[l++] = predator->prey[k] > index ? predator->prey[k] - 1: predator->prey[k];
- }
- }
- free(predator->prey);
- predator->prey = newPrey;
- predator->numPrey--;
- break;
- }
- }
- for(int j = 0; j < predator->numPrey; ++j){
- if (predator->prey[j] > index){
- predator->prey[j]--;
- }
- }
- }
- }
- 1: Task 3 - extinction() unit test case - basic (2 organisms)
- 0 / 4
- Feedback
- extinction() does not correctly update prey[] subitems for basic food web1
- 2: Task 3 - extinction() unit test case - small (3 organisms)
- 0 / 4
- Feedback
- extinction() does not correctly update prey[] subitems for basic food web1
- 3: Task 3 - extinction() unit test case - medium (10 organisms) - full extinction to numOrgs = 0 & web = NULL
- 0 / 4
- Feedback
- extinction() does NOT correctly update prey[] subitems
- 4: Task 3 - extinction() unit test case - large (100 organisms)
- 0 / 4
- Feedback
- extinction() does not correctly update prey[] subitems for large food web
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement