Question: #include #include #include #include typedef struct failfish { int sequence_number; struct failfish * next; struct failfish * prev; } failfish; typedef struct failfish_queue { char
#include
#include
#include
#include
typedef struct failfish
{
int sequence_number;
struct failfish * next;
struct failfish * prev;
} failfish;
typedef struct failfish_queue
{
char * pondname;
int n;
int e;
int th;
failfish * head;
failfish * tail;
} failfish_queue;
//This struct will deal with each pond
typedef struct fail_pond
{
int pond_number;
struct failfish_queue * queue; //each pond has a queue
} fail_pond;
//this creates the circular linked list
failfish * create_failfish(int sequence_number)
{
failfish * f = malloc(sizeof(failfish));
f -> sequence_number = sequence_number;
f -> next = NULL;
return f;
}
//this creates the queue
failfish_queue * create_failfish_queue(char * pondname, int n, int e, int th)
{
failfish_queue * q = malloc(sizeof(failfish_queue));
q -> head = NULL;
q -> tail = NULL;
q -> pondname = strdup(pondname);
q -> n = strdup(n);
q -> e = strdup(e);
q -> th = strdup(th);
return q;
}
//Get first line of file, number of failgroups
int get_num_failgroups(FILE * ifp)
{
char scan[100];
char * num_groups_string;
int num_groups;
num_groups_string = fgets(scan, 99, ifp); // chegg_check check the value obtained here is correct
num_groups = atoi(num_groups_string); // chegg_check check the value obtained here is correct
printf("There are %d groups of failfish ", num_groups);
return num_groups;
}
//create an array of ponds
fail_pond * create_pond_array(int num_groups)
{
char * pondname;
int pond_number;
int n;
int e;
int th;
fail_pond * fp = malloc(num_groups * sizeof(fail_pond)); // chegg_check check value of num_group here for the correctness
//fp->queue = malloc(sizeof(fp->queue));
fp -> queue = malloc(sizeof(failfish_queue));
fp -> queue -> pondname = strdup(pondname); //it doesn't like when I use strdup
fp -> pond_number = strdup(pond_number);
// chegg_check check n, e, th are not initialized fp -> queue = create_failfish_queue(pondname, n, e, th);
return fp;
}
void read_input(FILE * ifp, int num_groups, failfish_queue * q, fail_pond * fp)
{
int pond_number;
int i;
int n;
int e;
int th;
char scan[100];
// chegg_check check the num_groups value for the correctness for (i = 0; i < (num_groups - 1); i++)
{
fscanf(ifp, "%d", & fp[i].pond_number);
printf("pond number: %d", fp[i].pond_number);
//fgets(scan, 99, ifp);
q[i].pondname = malloc(sizeof(char));
//read in other pond info
// chegg_check check the below fscanf and file formatting fscanf(ifp, "%s", q[i].pondname);
printf(" name: %s", q[i].pondname);
fscanf(ifp, "%d", & q[i].n);
printf(" %d", q[i].n);
fscanf(ifp, "%d", & q[i].e);
printf(" %d", q[i].e);
fscanf(ifp, "%d", & q[i].th);
printf(" %d", q[i].th);
fp[i].queue = & q[i];
printf(" ");
}
}
void print_pond(fail_pond * fp, int num_groups)
{
int i;
for (i = 0; i < num_groups; i++)
{
printf("NEW POND: ");
printf("number: %d", fp[i].pond_number);
printf(" name: %s", fp[i].queue -> pondname);
printf(" number of fish: %d", fp[i].queue -> n);
printf(" eaten: %d", fp[i].queue -> e);
printf(" threshold: %d", fp[i].queue -> th);
printf(" ");
}
printf(" ");
}
void link(failfish * f, fail_pond * fp)
{
if (fp -> queue -> head == NULL)
{
fp -> queue -> head = f;
fp -> queue -> tail = f;
f -> prev = f;
f -> next = f;
} else
{
printf("you done messed up A-aron ");
f -> prev = fp -> queue -> tail;
f -> next = fp -> queue -> head;
fp -> queue -> head -> prev = f;
fp -> queue -> tail -> next = f;
fp -> queue -> head = f;
}
}
void sort_ponds(fail_pond * fp, int num_groups)
{
int i;
int j;
int min_index;
struct fail_pond temp;
//sort array one value at a time // chegg_check check the value of num_groups for (i = 0; i < (num_groups - 1); i++)
{
min_index = i;
for (j = 0; j < (num_groups - 1); j++)
{ // chegg_check check whether the fp[j] is below the size of fp if (fp[j].pond_number < fp[min_index].pond_number)
{
min_index = j;
temp = fp[min_index];
fp[min_index] = fp[i];
fp[i] = temp;
}
}
}
print_pond(fp, num_groups);
}
void initial_status(failfish * f, fail_pond * fp, int num_groups)
{
int i;
int j;
printf("Initial pond status ");
// chegg_check check the value of num_groups for (i = 0; i < (num_groups - 1); i++)
{
printf("%d", fp[i].pond_number);
printf(" %s", fp[i].queue -> pondname);
for (j = 1; j < fp[i].queue -> n + 1; j++)
{
//f = create_failfish(j);
//printf(" %d ", j); // chegg_check check f[i] exist, i is less than the size of f f[i].sequence_number = j;
printf("% d ", j);
}
printf(" ");
}
printf(" ");
}
void dispose_fish(failfish * f)
{
free(f);
}
//set links to null or free fish_eaten
void clear_links(failfish * fish_eaten, int dispose)
{
if (dispose != 0)
{
dispose_fish(fish_eaten);
} else
{
fish_eaten -> next = NULL;
fish_eaten -> prev = NULL;
}
printf("executed successfully ");
}
//"eat" a fish
void delete_node(failfish * f, failfish * fish_eaten, fail_pond * fp, int dispose)
{
f = fp -> queue -> head;
if (fish_eaten -> next == fish_eaten)
{
clear_links(fish_eaten, dispose);
fp -> queue -> head = NULL;
fp -> queue -> tail = NULL;
return;
//original program had segfaults before exiting this statement
}
//repair head and tail pointers
if (fp -> queue -> head == fish_eaten)
{
fp -> queue -> head = fish_eaten -> next;
}
if (fp -> queue -> tail == fish_eaten)
{
fp -> queue -> tail = fish_eaten -> prev;
}
fish_eaten -> prev -> next = fish_eaten -> next;
fish_eaten -> next -> prev = fish_eaten -> prev;
clear_links(fish_eaten, dispose);
}
//conduct first course
void first_course(failfish * f, fail_pond * fp, int num_groups)
{
int i;
int j;
int k;
int dispose;
failfish * fish_eaten;
failfish * head;
failfish * tail;
failfish * traverse = fp -> queue -> head;
printf("First Course ");
// chegg_check check value of num_groups for (i = 0; i < (num_groups - 1); i++)
{ // chegg_check check fp[i] exists , i is less then the size of fp printf("Pond %d: %s ", fp[i].pond_number, fp[i].queue -> pondname);
printf("Pond %d: fp[i].queue->n %i ", fp[i].pond_number, fp[i].queue -> n);
printf("Pond %d: fp[i].queue->th %i ", fp[i].pond_number, fp[i].queue -> th);
if (fp[i].queue -> n != fp[i].queue -> th)
{
for (j = fp[i].queue -> n; j > fp[i].queue -> th; j--)
{
for (k = 0; k < (fp[i].queue -> e - 1); k++)
{
printf("this worked "); // chegg_check check if error occurs here, definately the queue->e is not equal to size of linked list traverse = traverse -> next; //segmentation fault here
printf("made it here "); //this is never printed out
}
}
}
//if(fp[i].queue->n != fp[i].queue->th)
//{
//for(j = fp[i].queue->n; j > fp[i].queue->th; j--)
//{
//this for loop is not being entered
/*
for(k = 0; k < (fp[i].queue->e-1); k++)
{
// **** todo ****
//traverse = traverse->next;
//you dispose after --- look at the todos
//this is my traverse failfish *traverse = pond->failfish_queue->head;
// *******
// delete_node(f, fish_eaten, fp, dispose);
// fp[i].queue->n = fp[i].queue->n-1;
// printf("Failfish %d eaten ", f[j].sequence_number);
}
*/
//**** todo ****
printf(" Failfish %i eaten", traverse -> sequence_number);
//printf(" Failfish %i eaten", traverse->sequence_number);
//printf(" Failfish %i eaten", traverse->sequence_number);
if (head == traverse)
{
head = head -> next;
}
delete_node(f, traverse, fp, dispose);
/*
if(pointer to head == transverse){
pointer to head = pointer to head->next;
}
//dispose the eaten fish
dispose_failfish(&transverse);
*/
//******
//fp[i].queue->n = fp[i].queue->n - 1;
//}
/*
*/
//*** to do ***
// ptr to tail = ptr to tail->prev;
//******
//}
}
}
int main(void)
{
FILE * ifp;
FILE * ofp;
int num_groups;
int sequence_number;
char pondname[128];
int n;
int e;
int th;
int i;
int highest_val;
failfish * ffish;
//failfish_queue *fqueue[10];
failfish_queue * fqueue;
fail_pond * fpond;
ifp = fopen("input.txt", "r");
ofp = fopen("output.txt", "w");
// chegg_check check sequence_number is not initialized ffish = create_failfish(sequence_number);
// chegg_check check whether the value returned by the below function is correct num_groups = get_num_failgroups(ifp);
fpond = create_pond_array(num_groups);
read_input(ifp, num_groups, fqueue, fpond);
sort_ponds(fpond, num_groups);
initial_status(ffish, fpond, num_groups);
first_course(ffish, fpond, num_groups);
return 0;
} Can you help with the issues with //chegg_check and the input.txt is as given as a txt
5
4 mishap7 3 3
6 futility5 2 3
1 failure10 3 2
7 sisyphean9 2 4
3 doomed8 2 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
