Advertisement
TheLegend12

Untitled

Feb 3rd, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5.  
  6. typedef struct Org_struct {
  7. char name[20];
  8. int* prey; //dynamic array of indices
  9. int numPrey;
  10. } Org;
  11.  
  12.  
  13. void buildWeb(Org* web, int numOrg, int predInd, int preyInd) {
  14. char* organismNames[] = {"Grass", "Grasshopper", "Hawk", "Lizard", "Rabbit", "Snake", "Mouse"};
  15.  
  16. for(int i = 0; i < numOrg; ++i){
  17. switch(i){
  18. case 0: sprintf(web[i].name, "Grass"); break;
  19. case 1: sprintf(web[i].name, "Grasshopper"); break;
  20. case 2: sprintf(web[i].name, "Hawk"); break;
  21. case 3: sprintf(web[i].name, "Lizard"); break;
  22. case 4: sprintf(web[i].name, "Rabbit"); break;
  23. case 5: sprintf(web[i].name, "Snake"); break;
  24. case 6: sprintf(web[i].name, "Mouse"); break;
  25. }
  26. web[i].prey = NULL;
  27. web[i].numPrey = 0;
  28. }
  29.  
  30.  
  31. //TODO (Task 1): build the web by adding the predator-prey relation to the food web.
  32. // Inputs:
  33. // web - a dynamically allocated array of Orgs
  34. // numOrgs - number of organisms = size of web[]
  35. // predInd - predator index in web[]; an entry to its prey[] subitem will be added
  36. // preyInd - prey index in web[]; will be added to predator's prey[] subitem
  37. // Outputs:
  38. // web - array is updated and implicitly returned (previously allocated on heap)
  39. //
  40. // For the predator-prey relation...
  41. // (1) if the predator's prey[] array is empty, allocate memory for one index
  42. // otherwise, reallocate predator's prey[] array to allow one more index
  43. // (2) append the prey index as the last element of the predator's prey[] array
  44. // (3) update the numPrey subitem for the predator appropriately
  45.  
  46.  
  47. for(int i = 0; i < numOrg; ++i){
  48. printf(web[i].name, "%s", organismNames[i]);
  49. web[i].prey = NULL;
  50. web[i].numPrey = 0;
  51. }
  52.  
  53. if (predInd < 0 || predInd >= numOrg || preyInd < 0 || preyInd >= numOrg) {
  54. printf("Invalid indices. Predator and prey indices must be within the range [0, %d).\n", numOrg);
  55. return;
  56. }
  57.  
  58. Org* predator = &web[predInd];
  59.  
  60. if (predator->numPrey == 0) {
  61. predator->prey = (int*)malloc(sizeof(int));
  62. } else {
  63. predator->prey = (int*)realloc(predator->prey, (predator->numPrey + 1) * sizeof(int));
  64. }
  65.  
  66. predator->prey[predator->numPrey] = preyInd;
  67.  
  68. predator->numPrey++;
  69.  
  70. }
  71.  
  72.  
  73. void extinction(Org** web, int* numOrgs, int index) {
  74. //TODO (Task 3): remove the organism associated with [index] from web.
  75. // Inputs:
  76. // web - a dynamically allocated array of Orgs
  77. // numOrgs - number of organisms = size of web[]
  78. // index - organism index in web[] to remove
  79. // Outputs:
  80. // web - pointer passed-by-pointer; memory address of web array changes due to reallocation
  81. // numOrgs - passed-by-pointer; must be decremented since web[] loses an organism
  82. //
  83. // Remember to do the following:
  84. // 1. remove organism at index from web[] - DO NOT use realloc(), instead...
  85. // (a) free any malloc'd memory associated with organism at index; i.e. its prey[] subitem
  86. // (b) malloc new space for the array with the new number of Orgs
  87. // (c) copy all but one of the old array elements to the new array,
  88. // some require shifting forward to overwrite the organism at index
  89. // (d) free the old array
  90. // (e) update the array pointer to the new array
  91. // (f) update numOrgs
  92. // 2. remove index from all organisms' prey[] array subitems - DO NOT use realloc(), instead...
  93. // (a) search for index in all organisms' prey[] arrays; when index is found:
  94. // [i] malloc new space for the array with the new number of ints
  95. // [ii] copy all but one of the old array elements to the new array,
  96. // keeping the same order some require shifting forward
  97. // [iii] free the old array
  98. // [iv] update the array pointer to the new array
  99. // [v] update the numPrey subitem accordingly
  100. // (b) update all organisms' prey[] elements that are greater than index,
  101. // which have been shifted forward in the web array
  102. //
  103. // Edge case: check the size array being malloc'ed;
  104. // for a web with only one organism and
  105. // that orgranism goes extinct,
  106. // instead of malloc'ing an empty array,
  107. // explicitly set the pointer to NULL;
  108. // see the web[] allocation in main() as an example
  109.  
  110. }
  111.  
  112. void printFoodWeb(Org* web, int numOrg){
  113. printf("Food Web Organisms with Prey:\n");
  114.  
  115. for(int i = 0; i < numOrg; ++i){
  116. printf("%s", web[i].name);
  117.  
  118. for(int j = 0; j < web[i].numPrey; ++j){
  119. printf(" %s", web[web[i].prey[i]].name);
  120. if(j < web[i].numPrey - 1){
  121. printf(", ");
  122. }
  123. }
  124.  
  125. printf(")\n");
  126. }
  127. }
  128.  
  129. void printApexPredators(Org* web, int numOrgs){
  130. printf("Apex Predators:\n");
  131. for(int i = 0; i < numOrgs; ++i){
  132. if (web[i].numPrey == 0){
  133. printf("%s\n", web[i].name);
  134. }
  135. }
  136. }
  137.  
  138. void printProducers(Org* web, int numOrgs){
  139. printf("Producers:\n");
  140. for(int i = 0; i < numOrgs; ++i){
  141. if(web[i].numPrey == 0){
  142. printf("%s\n", web[i].name);
  143. }
  144. }
  145. }
  146.  
  147. void printMostFlexibleEaters(Org* web, int numOrgs){
  148. printf("Most Flexible Eaters:\n");
  149. int maxPrey = -1;
  150. for(int i = 0; i < numOrgs; ++i){
  151. if(web[i].numPrey > maxPrey){
  152. maxPrey = web[i].numPrey;
  153. }
  154. }
  155. for(int i = 0; i < numOrgs; ++i){
  156. if(web[i].numPrey == maxPrey){
  157. printf("%s\n", web[i].name);
  158. }
  159. }
  160. }
  161.  
  162. void printTastiestFood(Org* web, int numOrgs){
  163. printf("Tastiest Food:\n");
  164. int maxEatenByOthers = -1;
  165. int tastiestFoodIndex = -1;
  166. for(int i = 0; i < numOrgs; ++i){
  167. int eatenByOthers = 0;
  168. for(int j = 0; j < numOrgs; ++j){
  169. if(i != j){
  170. for(int k = 0; k < web[j].numPrey; ++k){
  171. if (web[j].prey[k] == i){
  172. eatenByOthers++;
  173. break;
  174. }
  175. }
  176. }
  177. }
  178. if(eatenByOthers > maxEatenByOthers){
  179. maxEatenByOthers = eatenByOthers;
  180. tastiestFoodIndex = i;
  181. }
  182. }
  183. if (tastiestFoodIndex != -1){
  184. printf("%s\n", web[tastiestFoodIndex].name);
  185. }
  186. }
  187.  
  188. int main(int argc, char* argv[]) {
  189.  
  190. bool quietMode = false;
  191. bool extinctMode = true;
  192.  
  193. //TODO (Task 0): process command-line arguments & update quietMode and extinctMode
  194. // - default values: quietMode = FALSE, extinctMode = TRUE
  195. // - if quietMode = FALSE, then print user-input prompt messages
  196. // - if extinctMode = TRUE, then perform the extinction step
  197. //
  198. // valid command-line arguments are "-q" and "-x" (and can only appear once)
  199. // - set quietMode = TRUE if "-q" is present
  200. // - set extinctMode = FALSE if "-x" is present
  201. // - if an invalid command-line argument is present, print
  202. // "Invalid command-line argument. Terminating program..."
  203. // and end the program immediately
  204. //
  205. // once command-line arguments are processed, print the program settings
  206. // - Ex: if the program is run as "./a.out -q -x", then print
  207. // Program Settings:
  208. // quiet mode = ON
  209. // extinction mode = OFF
  210. // - Ex: if the program is run as "./a.out", then print
  211. // Program Settings:
  212. // quiet mode = OFF
  213. // extinction mode = ON
  214.  
  215. for(int i = 1; i < argc; ++i){
  216. if(strcmp(argv[i], "-q") == 0){
  217. if(quietMode){
  218. printf("Invalid command-line argument. Terminating program...\n");
  219. return 1;
  220. }
  221. quietMode = true;
  222. } else if(strcmp(argv[i], "-x") == 0){
  223. if(!extinctMode){
  224. printf("Invalid command-line argument. Terminating program...\n");
  225. return 1;
  226. }
  227. extinctMode = false;
  228. } else{
  229. printf("Invalid command-line argument. Terminating program...\n");
  230. return 1;
  231. }
  232. }
  233.  
  234.  
  235.  
  236.  
  237. printf("Program Settings:\n");
  238. printf(" quiet mode = %s\n", quietMode ? "ON": "OFF");
  239. printf(" extinction mode = %s\n", extinctMode ? "ON": "OFF");
  240.  
  241. int numOrgs;
  242.  
  243.  
  244.  
  245. printf("Welcome to the Food Web Application\n");
  246. printf("--------------------------------\n");
  247. if (!quietMode) printf("Enter number of organisms:\n");
  248. scanf("%d",&numOrgs);
  249.  
  250. Org* web = (Org*)malloc(numOrgs * sizeof(Org));
  251.  
  252. buildWeb(web, numOrgs, 0, 1);
  253. buildWeb(web, numOrgs, 1, 2);
  254. buildWeb(web, numOrgs, 2, 3);
  255.  
  256. for(int i = 0; i < numOrgs; ++i){
  257. printf("Predator %s has %d prey.\n", web[i].name, web[i].numPrey);
  258. }
  259.  
  260. // for(int i = 0; i < numOrgs; ++i){
  261. // free(web[i].prey);
  262. // }
  263. // free(web);
  264.  
  265. printf("Organisms:\n");
  266. for(int i = 0; i < numOrgs; ++i){
  267. printf("%ls", web[i].prey);
  268. }
  269. free(web);
  270.  
  271. // Org* web = NULL;
  272. if(numOrgs > 0) { //Do not malloc an empty array, leave it pointing to NULL
  273. web = (Org*)malloc(numOrgs*sizeof(Org));
  274. }
  275.  
  276. if (!quietMode) printf("Enter names for %d organisms:\n", numOrgs);
  277. for (int i = 0; i < numOrgs; ++i) {
  278. scanf("%s",web[i].name);
  279. web[i].prey = NULL;
  280. web[i].numPrey = 0;
  281. }
  282.  
  283. if (!quietMode) printf("Enter number of predator/prey relations:\n");
  284. int numRels;
  285. scanf("%d",&numRels);
  286.  
  287. if (!quietMode) printf("Enter the pair of indices for the %d predator/prey relations\n",numRels);
  288. if (!quietMode) printf("the format is [predator index] [prey index]:\n");
  289.  
  290. int predInd, preyInd;
  291. for (int i = 0; i < numRels; ++i) {
  292. scanf("%d %d",&predInd, &preyInd);
  293. buildWeb(web,numOrgs,predInd,preyInd);
  294. }
  295. if (!quietMode) printf("--------------------------------\n\n");
  296.  
  297. printf("Food Web Predators & Prey:\n");
  298. //TODO (Task 2): print the Food Web Organisms with what they eat (i.e. prey)
  299. printFoodWeb(web, numOrgs);
  300. printf("\n");
  301.  
  302. printf("Apex Predators:\n");
  303. //TODO (Task 2): identify and print the organisms not eaten by any others
  304. printApexPredators(web, numOrgs);
  305. printf("\n");
  306.  
  307. printf("Producers:\n");
  308. //TODO (Task 2): identify and print the organisms that eat no other organisms
  309. printProducers(web, numOrgs);
  310. printf("\n");
  311.  
  312. printf("Most Flexible Eaters:\n");
  313. //TODO (Task 2): identity and print the organism(s) with the most prey
  314. printMostFlexibleEaters(web, numOrgs);
  315. printf("\n");
  316.  
  317. printf("Tastiest Food:\n");
  318. //TODO (Task 2): identity and print organism(s) eaten by the most other organisms
  319. printTastiestFood(web, numOrgs);
  320. printf("\n");
  321.  
  322. printf("Food Web Heights:\n");
  323. //TODO (Task 2): calculate and print the length of the longest chain from a
  324. // producer to each organism
  325. printf("\n");
  326.  
  327. printf("Vore Types:\n");
  328. //TODO (Task 2): classify all organisms and print each group
  329. // (producers, herbivores, omnivores, & carnivores)
  330. printf("\n");
  331.  
  332. if (extinctMode) {
  333. printf("--------------------------------\n");
  334. int extInd;
  335. printf("Enter extinct species index:\n");
  336. scanf("%d",&extInd);
  337. printf("Species Extinction: %s\n", web[extInd].name);
  338. extinction(&web,&numOrgs,extInd);
  339. printf("--------------------------------\n\n");
  340.  
  341. printf("UPDATED Food Web Predators & Prey:\n");
  342. //TODO (Task 3): print the UPDATED Food Web Organisms with what they eat (i.e. prey), AFTER THE EXTINCTION
  343. printf("\n");
  344.  
  345. printf("UPDATED Apex Predators:\n");
  346. //TODO (Task 3): AFTER THE EXTINCTION, identify and print the organisms not eaten by any other
  347. printf("\n");
  348.  
  349. printf("UPDATED Producers:\n");
  350. //TODO (Task 3): AFTER THE EXTINCTION, identify and print the organisms that eat no other organisms
  351. printf("\n");
  352.  
  353. printf("UPDATED Most Flexible Eaters:\n");
  354. //TODO (Task 3): AFTER THE EXTINCTION, identity and print the organism(s) with the most prey
  355. printf("\n");
  356.  
  357. printf("UPDATED Tastiest Food:\n");
  358. //TODO (Task 3): AFTER THE EXTINCTION, identity and print organism(s) eaten by the most other organisms
  359. printf("\n");
  360.  
  361. printf("UPDATED Food Web Heights:\n");
  362. //TODO (Task 3): AFTER THE EXTINCTION, calculate and print the length of the longest chain from a
  363. // producer to each organism
  364. printf("\n");
  365.  
  366. printf("UPDATED Vore Types:\n");
  367. //TODO (Task 3): AFTER THE EXTINCTION, classify all organisms and print each group
  368. // (producers, herbivores, omnivores, & carnivores)
  369. printf("\n");
  370. printf("--------------------------------\n");
  371. }
  372.  
  373. //TODO (Task 4): make sure to free all malloc'd memory to prevent potential leaks
  374.  
  375. return 0;
  376. }
  377.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement