Question
I have segmentations problems in this assignment, I have everything that I know but I can't get it to work. #include #include #include #include #include
I have segmentations problems in this assignment, I have everything that I know but I can't get it to work.
#include
#define MAX 51
typedef struct monster { char *name; char *element; int population; } monster;
typedef struct region { char *name; int monster_cnt; int total_population; monster **monsters; } region;
typedef struct itinerary { int region_cnt; region **regions; int captures; } itinerary;
typedef struct trainer { char *name; itinerary *visits; } trainer;
monster *makeMonster(char *name, char *element, int population) { monster *m = (monster *)malloc(sizeof(monster));
//Allocating memory for name, element, and population m->name = (char *)malloc(strlen(name) + 1 * sizeof(char)); m->element = (char *)malloc(strlen(name) + 1 * sizeof(char)); strcpy(m->name, name); strcpy(m->element, element); m->population = population;
return m; }
monster **readMonsters(int *monsterCount) { monster **monsterList = (monster **)malloc(*monsterCount * sizeof(monster *));
char n[MAX]; char e[MAX]; int p;
//Reading in the monster names and elements, and transfering data to the monsterList for (int i = 0; i < *monsterCount; i++) { scanf("%s %s %d", n, e, &p); monsterList[i] = makeMonster(n, e, p); }
return monsterList; }
region **readRegions(int *countRegions, monster **monsterList, int monsterCount) { region **regionList = (region **)malloc(*countRegions * sizeof(region *));
char mname[MAX]; char rname[MAX];
//Reading and allocating memory for name and regions, and transfering data to the regionList for (int i = 0; i < *countRegions; i++) { regionList[i] = (region *)malloc(*countRegions * sizeof(region)); regionList[i]->name = (char *)malloc((strlen(mname) + 1) * sizeof(char)); scanf("%s", rname); strcpy(regionList[i]->name, rname); scanf("%d", ®ionList[i]->monster_cnt); //Allocating memory an array of monster type pointers regionList[i]->monsters = (monster **)malloc(regionList[i]->monster_cnt * sizeof(monster *));
//Reading in the monster names, plus comparing the names in each block of monsters int found = -1; for (int j = 0; j < regionList[i]->monster_cnt; j++) { scanf("%s", mname); for (int k = 0; k < monsterCount; k++) { if (strcmp(mname, monsterList[k]->name) == 0) { found = k; break; } }
if (found != -1) { regionList[i]->monsters[j] = monsterList[found]; regionList[i]->total_population += monsterList[found]->population; } else { printf("Could not find monster with name '%s' ", mname); }
found = -1; }
} return regionList; }
/*void printRegion(region **regionList, int countRegions) { for (int i = 0; i < countRegions; i++) { printf("Region name: %s ", regionList[i]->name); printf("Number of Monsters: %d ", regionList[i]->monster_cnt); printf("Total population of Monsters: %d ", regionList[i]->total_population); for (int j = 0; j < regionList[i]->monster_cnt; j++) { printf("\tMonster name: %s ", regionList[i]->monsters[j]->name); printf("\tMonster population: %d ", regionList[i]->monsters[j]->population); } } } */ trainer *loadTrainers(int *trainerCount, region **regionList, int countRegions) { trainer *trainerList = (trainer *)malloc(*trainerCount * sizeof(trainer));
char tname[MAX]; char rname[MAX];
//Reading in the trainer names, and transfering data to the trainerList for (int i = 0; i < *trainerCount; i++) { scanf("%s", tname); trainerList[i].name = (char *)malloc((strlen(tname)+1) * sizeof(char)); strcpy(trainerList[i].name, tname);
trainerList[i].visits = (itinerary *)malloc(countRegions * sizeof(itinerary));
//Allocating memory an array of ragion type pointers trainerList[i].visits->regions = (region **)malloc(trainerList[i].visits->region_cnt * sizeof(region *)); scanf("%d", &trainerList[i].visits->captures);
scanf("%d", &trainerList[i].visits->region_cnt);
//Reading in the regions names, plus comparing the names in each block of monsters int found = -1; for (int j = 0; j < trainerList[i].visits->region_cnt; j++) { scanf("%s", rname); for (int k = 0; k < countRegions; k++) { if (strcmp(regionList[k]->name, rname) == 0){ found = k; break; if (found!= -1) { trainerList[i].visits->regions[j] = regionList[found]; } else printf("Could not find region with name '%s' ", rname); } } } } return trainerList; }
/*void printTrainers(trainer *trainerList, int trainerCount) { for (int i = 0; i < trainerCount; i++) { printf("Trainer: %s ", trainerList[i].name); printf("Captures: %d ", trainerList[i].visits->captures); printf("Visited Regions: "); for (int j = 0; j < trainerList[i].visits->region_cnt; j++) { printf("%s ", trainerList[i].visits->regions[j]->name); for (int k = 0; k < trainerList[i].visits->regions[j]->; k++) { } } } */
//This function will calculate and return the estimated captures int num_captures(int rel_p, int t_p, int t_c){ return round((double)rel_p / t_p * t_c); }
//This function is used to print the results of the program void processInputs(monster **monsterList, int monsterCount, region **regionList, int countRegions, trainer *trainerList, int trainerCount) { for (int i = 0; i < trainerCount; i++) { trainer tr = trainerList[i]; itinerary *tin = tr.visits; int t_c = tin->captures;
printf("%s ", tr.name); for (int j = 0; j < tin->region_cnt; j++) { region *rg = tin->regions[j]; printf("%s ", rg->name); for (int k = 0; k < rg->monster_cnt; k++) { monster *mon = rg->monsters[k]; int capt = num_captures(rg->total_population, mon->population , t_c); if (capt >= 0) { t_c += mon->population; mon->population = 0; } else { t_c += capt; mon->population -= capt; } printf("%d-%s ", capt, trainerList[i].visits->regions[j]->monsters[k]->name); } } } }
//This function will free the memory allocated for the entire program void releaseMemory(monster * *monsterList, int monsterCount, region **regionList, int countRegions, trainer *trainerList, int trainerCount) { for (int i = 0; i < monsterCount; i++) { free(monsterList[i]->name); free(monsterList[i]->element); free(monsterList); }
for (int i = 0; i < countRegions; i++) { free(regionList[i]->name); free(regionList[i]->monsters); free(regionList); }
for (int i = 0; i < trainerCount; i++) { free(trainerList[i].visits->regions); free(trainerList[i].visits); free(trainerList); } }
int main() { /* atexit(report_mem_leak); char * ptr1 = (char *)malloc(10); int * ptr2 = (int *)calloc(10, sizeof(int)); float * ptr3 = (float *) calloc(15, sizeof(float)); free(ptr2); //free(ptr1); //run the code with commenting and uncommenting this line for testing! */
//Read the input from the user and calling the function to process the input int monsterCount; scanf("%d", &monsterCount); monster **ml = readMonsters(&monsterCount);
int countRegions; scanf("%d", &countRegions); region **rl = readRegions(&countRegions, ml, monsterCount); //printRegion(rl, countRegions);
int trainerCount; scanf("%d", &trainerCount); trainer *lt = loadTrainers(&trainerCount, rl, countRegions); //printTrainers(lt, trainerCount);
processInputs(ml, monsterCount, rl, countRegions, lt, trainerCount);
releaseMemory(ml, monsterCount, rl, countRegions, lt, trainerCount);
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started