Question

With the following structures defined: struct monster_struct {       char *name;       int commonality;       int...

With the following structures defined:

struct monster_struct {

      char *name;

      int commonality;

      int weight;

      struct monster_struct *next;

};

typedef struct monster_struct monster;

typedef struct monster_list {

      monster *head;

}

  • Write functions to return the second-most-common monster and the third-lightest monster in the list.
  • Don’t worry about headers.
  • Don’t worry about which way to resolve ties.
  • The list will always have three monsters in it.

Homework Answers

Answer #1

The approach we will be using will require us to traverse the list multiple times. It will have a time complexity of O(n), which is better than the time that will be required if we sort the list.

(1) char* SecondMostCommon(monster* head)
{
int mostcommon=0;
int secmostcommon=0;
monster* r = head; // declaring another pointer for traversal
while(r!=NULL) //finding the most common monster
{
if(r->commonality>mostcommon)
mostcommon=r->commonality;

head = head->next
}
char* ans;
r = head;
  
while(r!=NULL) //finding the second-most common monster
{
if(r->commonality>secmostcommon && secmostcommon<mostcommon)
{
secmostcommon = r->commonality;
ans = r->name;
}
}
  
return ans;
}

(2) char* ThirdLightestMonster(monster* head) //function to find third-lightest monster
{
int lightest = INT_MAX;
int seclightest = INT_MAX;
int thirdlightest = INT_MAX;
monster* r =head;
while(r!=NULL) //finding the lightest monster
{
if(r->weight<lightest)
lightest = r->weight;
}
r = head;
while(r!=NULL) //finding the second-lightest monster
{
if(r->weight<seclightest && seclightest>lightest)
seclightest = r->weight;
}
r = head;
char* ans;
  
while(r!=NULL) //finding the third-lightest monster
{
if(r->weight<thirdlightest && thirdlightest>seclightest)
{
thirdlightest = r->weight;
ans = r->name;
}
}
return ans;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write an implementation file to implement the namespace declared in the attached CSCI361Proj5.h. Name the implementation file as YourNameProj5.cpp and add it to the project. Run the project to see your grade. .h file: // Provided by: ____________(your name here)__________ // Email Address: ____________(your email address here)________ // FILE: link.h // PROVIDES: A toolkit of 14 functions for manipulating linked lists. Each // node of...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
TOMS SHOES –Case Study Founded in 2006 by BLAKE MYCOSKIE, TOMS Shoes was an American footwear...
TOMS SHOES –Case Study Founded in 2006 by BLAKE MYCOSKIE, TOMS Shoes was an American footwear company based in Santa Monica, California. Although TOMS Shoes was a for-profit business, its mission was more like that of a not-for-profit organization. The firm’s reason for existence was to donate to children in need one new pair of shoes for every pair of shoes sold. Blake Mycoskie referred to it as the company’s “One for One” business model. While vacationing in Argentina during...
There are two reflective essays from MED students during their third year internal medicine clerkship. One...
There are two reflective essays from MED students during their third year internal medicine clerkship. One student sees each connection to a patient as like the individual brush strokes of an artist and the other sees gratitude in a patient with an incurable illness and is moved to gratitude in her own life. (WORD COUNT 500) Reflect on both essays and then choose one and describe how the student grew from the experience. Then explain what you learned as a...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT