Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(****IN C) Incorrect output on some test cases. I'm using a linked list to take in information about a person and using structs to find

(****IN C) Incorrect output on some test cases.

I'm using a linked list to take in information about a person and using structs to find the oldest and youngest person in the list; however, I am having trouble with some test cases. For the most part, The code seems to work the way I want it to, but certain test cases do have wrong outputs.

For example:

Input File Has Four Entries(2 Youngest are in the Same Month)

Here is the text file:

Smith,Bob,July,31,2002,CS/BS,Junior Joe,Average,February,3,2002,CS/BS,Freshman Doe,Jane,July,27,2003,CS/BS,Sophomore Doe,John,July,12,2003,CS/BS,Sophomore:

Output:

Oldest - John Doe & Youngest Jane Doe.

John is clearly not the oldest in the list so please help, below is my code.

//add birthday struct here

struct birthday

{

int day, year;

char month[50];

}; typedef struct birthday bday;

//add node_t struct here (this struct will represent a node in the linked list)

struct node

{

char firstName[50], lastName[50], major[50], classStanding[50];

bday bd;

struct node* next;

};

typedef struct node node_t;

struct stats

{

// node pointer pointing at the oldest and youngest person in the list

node_t* oldest;

node_t* youngest;

// holding the number of birthdays in each month (0 = Jan. & 11 = Dec.)

int numBD[12];

}; typedef struct stats list_stats_t;

void add(node_t **node, node_t **head)

{

(*node)->next = *head;

*head = *node;

}

node_t* readNodeInfo(FILE* input)

{

node_t *node;

node = (node_t*)malloc(sizeof(node_t)); // allocates memory

char newLine[1]; // reads new line character

// uses scanset to read information between commas

fscanf(input, "%[^,], %[^,], %[^,], %d, %d, %[^,], %[^ ] %[ ]",

node->lastName, node->firstName, node->bd.month,

&node->bd.day, &node->bd.year, node->major,

node->classStanding, newLine);

return node;

}

node_t* createList(FILE* in, node_t** head)

{

node_t *temp;

while(!feof(in)) // loops until end of file in

{

temp = readNodeInfo(in); // calls function and assigns it to temp

add(&temp, head); // adds to linked list

}

return *head;

}

void printList(FILE* out, node_t* head) // prints data

{

// if there is no data in the linked list, print error code to terminal

if (head == NULL)

{

fprintf(stderr, "LIST IS EMPTY ");

return;

}

node_t *temp = head;

printBorder(out);

fprintf(out, " List Info: ");

while (temp != NULL)

{

fprintf(out, "Name:\t%s %s ", temp->firstName, temp->lastName);

fprintf(out, "Date of Birth:\t%s %d, %d ", temp->bd.month,

temp->bd.day,

temp->bd.year);

fprintf(out, "Major:\t%s ", temp->major);

fprintf(out, "Year:\t%s ", temp->classStanding);

temp = temp->next; // next element

}

printBorder(out);

}

void printBorder(FILE* out)

{

int i = 0;

for(i = 0; i < 80; i++)

fprintf(out,"*");

fprintf(out, " ");

}

void deleteList(node_t** head) // deletes all nodes within the linked list

{

// deref head to get the real head

node_t* current = *head;

node_t* temp = NULL;

while (current != NULL)

{

temp = current->next;

free(current);

current = temp;

}

// deref head reference to affect the real head back in the caller.

*head = NULL;

}

int monthStringToInt(node_t* node) // assumes month is correct

{

if (strcmp(node->bd.month, "January") == 0)

{ return 1; }

else if (strcmp(node->bd.month, "February") == 0)

{ return 2; }

else if (strcmp(node->bd.month, "March") == 0)

{ return 3; }

else if (strcmp(node->bd.month, "April") == 0)

{ return 4; }

else if (strcmp(node->bd.month, "May") == 0)

{ return 5; }

else if (strcmp(node->bd.month, "June") == 0)

{ return 6; }

else if (strcmp(node->bd.month, "July") == 0)

{ return 7; }

else if (strcmp(node->bd.month, "August") == 0)

{ return 8; }

else if (strcmp(node->bd.month, "September") == 0)

{ return 9; }

else if (strcmp(node->bd.month, "October") == 0)

{ return 10; }

else if (strcmp(node->bd.month, "November") == 0)

{ return 11; }

else { return 12; } // december

}

bool isOlder(node_t* temp)

{

if (temp->bd.year < temp->next->bd.year) // if older year

{ return true; }

else if (temp->bd.year == temp->next->bd.year) // if same year

{

// if older month

if (monthStringToInt(temp) < monthStringToInt(temp->next))

{ return true; }

// if same month

else if (monthStringToInt(temp) == monthStringToInt(temp->next))

{

if (temp->bd.day < temp->next->bd.day) // if older day

{ return true; }

}

}

return false; // if not older

}

// assumes month is correct

void countBirthdays(node_t* temp, list_stats_t* stat)

{

if (monthStringToInt(temp) == 1)

{ (*stat).numBD[0] += 1; }

else if (monthStringToInt(temp) == 2)

{ (*stat).numBD[1] += 1; }

else if (monthStringToInt(temp) == 3)

{ (*stat).numBD[2] += 1; }

else if (monthStringToInt(temp) == 4)

{ (*stat).numBD[3] += 1; }

else if (monthStringToInt(temp) == 5)

{ (*stat).numBD[4] += 1; }

else if (monthStringToInt(temp) == 6)

{ (*stat).numBD[5] += 1; }

else if (monthStringToInt(temp) == 7)

{ (*stat).numBD[6] += 1; }

else if (monthStringToInt(temp) == 8)

{ (*stat).numBD[7] += 1; }

else if (monthStringToInt(temp) == 9)

{ (*stat).numBD[8] += 1; }

else if (monthStringToInt(temp) == 10)

{ (*stat).numBD[9] += 1; }

else if (monthStringToInt(temp) == 11)

{ (*stat).numBD[10] += 1; }

else { (*stat).numBD[11] += 1; } // december

}

list_stats_t getListStats(node_t *head)

{

node_t* temp = head;

// creates object to be returned and initializes it to the first node in a list

list_stats_t stat = {temp, temp, {0,0,0,0,0,0,0,0,0,0,0,0}};

while (temp != NULL)

{

// if current node isn't the oldest, set the oldest to the next node

if (temp->next != NULL && isOlder(stat.oldest) == false)

{ stat.oldest = temp->next; }

// if current node isn't the youngest, set the youngest to the next node

if (temp->next != NULL && isOlder(stat.youngest) == true)

{ stat.youngest = temp->next; }

countBirthdays(temp, &stat);

temp = temp->next; // next value

}

return stat;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Moving Objects Databases

Authors: Ralf Hartmut Güting, Markus Schneider

1st Edition

0120887991, 978-0120887996

More Books

Students also viewed these Databases questions

Question

2. Discuss the steps in preparing a manager to go overseas.

Answered: 1 week ago

Question

8. Measure the effectiveness of the succession planning process.

Answered: 1 week ago