Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with the implementation of Q1. #include <stdio.h> #include <string.h

ID: 671312 • Letter: I

Question

I need help with the implementation of Q1.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#pragma warning(disable : 4996)

typedef enum { action = 0, comedy, thriller } genre;// enumeration type genre

char* gen[] = { "action", "comedy", "thriller" };// used for easy of printing genre

struct movie {
   struct movie* next;
   char name[100];
   int rating;
   genre type;
} *list = NULL;

// forward declarations
void flush();
void branching(char c);
void helper(char c);
//These have been ordered in a way of suggested completion. You will need to complete search() before you start delete_one()
void add(struct movie* new_movie);                  
struct movie* search(struct movie* movie);           
struct movie* recommendation(struct movie* movie);   
void delete_one(struct movie* movie);              


int main()
{
   char ch = 'i';

   ungetc(' ', stdin);                   // inject input buffer with a return character

   printf("Welcome to the movie manager! ");

   do {
       printf("Please enter your selection ");
       printf(" a: add a movie ");
       printf(" d: delete a movie ");
       printf(" s: search for movie rating ");
       printf(" r: recommend a movie based on genre ");
       printf(" q: quit ");

       flush();                           // flush input buffer
       ch = tolower(getchar());
       branching(ch);
   } while (ch != 113);

   return 0;
}

// flush input buffer
void flush()
{
   int c;
   do {
       c = getchar();
   } while (c != ' ' && c != EOF);
}

// branch to different tasks
void branching(char c)
{
   switch (c) {
   case 'a':
   case 'd':
   case 's':
   case 'r':
       helper(c);
       break;
   case 'q':
       break;
   default:
       printf("Invalid input! ");
   }

   if (c == 's' || c == 'd'){
       ungetc(' ', stdin);
   }
}

// Q0. The helper function is used to determine how much information is needed, and which function to send that information to.
// It uses pointers that are returned from these functions to produce the correct ouput.
// There is no implementation needed here, but you should study this function and know how it works.
// It is always helpful to understand how the code works before implementing new features.
void helper(char c) {

   char* input_genre = (char*)malloc(100);
   int valid_genre = -1;

   // create new temporary pointers
   struct movie *ptr = (struct movie *)malloc(sizeof(struct movie));
   struct movie *temp;

   // if you are adding a movie or requesting a recommendation
   if (c == 'a' || c == 'r')
   {
       // stores the genre of the movie into pointer
       while (valid_genre < 0)
       {
           printf("What is the genre of the movie? (action/comedy/thiller): ");
           scanf("%s", input_genre);

           if (strcmp(input_genre, "action") == 0)
           {
               valid_genre++;
               ptr->type = (genre)0;// type casting
           }
           else if (strcmp(input_genre, "comedy") == 0)
           {
               valid_genre++;
               ptr->type = (genre)1;
           }
           else if (strcmp(input_genre, "thriller") == 0)
           {
               valid_genre++;
               ptr->type = (genre)2;
           }
           else
               printf("Please enter a valid genre (action/comedy/thiller). ");// error handling

           if (c == 'r' && valid_genre == 0)
           {
               temp = recommendation(ptr);// send genre to recommendation function
               if (temp == NULL) printf("There are no movies with that genre. ");
               else printf("The highest rated %s movie in your list is %s ", gen[temp->type], temp->name);
               return;// exit back to menu
           }
       }
   }

   //stores the name of the movie into pointer
   printf("Enter the name of the movie: ");
   flush();
   fgets(ptr->name, sizeof(ptr->name), stdin);

   // if you are searching
   if (c == 's')
   {
       temp = search(ptr);
       if (temp == NULL)
       {
           printf("Movie not found. ");
       }
       else
           printf("The rating for that movie is: %d ", temp->rating);
       return;// exit back to menu
   }// send name to search function

   // if you are deleting
   else if (c == 'd')
   {
       temp = search(ptr);// check to see if movie is in list
       if (temp == NULL)// movie is not in list
       {
           printf("Movie not found. ");
       }
       else
       {
           delete_one(temp);
           printf("Movie been deleted. ");
       }
       return;// exit back to menu
   }// send name to delete_one function if it exists

   int input = -1;

   //stores the rating of the movie into pointer
   while (input < 1 || input > 10)
   {
       printf("What is your rating of the movie? (1-10): ");
       scanf("%d", &input);

       if (input >= 1 && input <= 10)
           ptr->rating = input;
       else
           printf("Please enter a rating between 1 and 10. ");
   }

   // always set next equal to NULL before adding to a list so that if it is placed at the end of the list, you know where to stop traversing
   ptr->next = NULL;

   add(ptr);
}

// Q1:
// "list" is initialized as NULL. Use this as your 'head' of the list and insert into it accordingly.
// There are 4 possibilities for inserting into the list:
//   - inserting into an empty list
//   - inserting at the beginning of the list
//   - inserting inbetween 2 nodes in the list
//   - inserting at the end of the list
// The short segment of code is given to you to help you get started. You will need to define new variables and pointers as needed.
// If a movie with the same rating already exists in the list, you should replace that position with the new movie, and move that movie to the next position.
//
// For example, if you have the list that contains:
//
//   Movie: The Hunger Games
//   Rating: 8
//
//   Movie: Mission Impossible
//   Rating: 7
//
// And you were to add the movie "The Sound of Music" with a rating of 8, your new list should be:
//
//   Movie: The Sound of Music
//   Rating: 8
//
//   Movie: The Hunger Games
//   Rating: 8
//
//   Movie: Mission Impossible
//   Rating: 7
void add(struct movie* new_movie)
{
    struct node *temp; //creating temporary node
    temp = (struct movie*)malloc(sizeof(struct contact)); //avoid counting and padding
  
   if (list == NULL) //inserting into an empty list
    {
      
   }
    else if() //inserting at the beggining of a list.
    {
      
    }
    else if() //adds inbetween
    {
      
    }
    else //adds to end of list
    {
      
    }

}

Explanation / Answer

The temporary pointer is used to store the movie which is either rating or insert new movie and also specify the size of the movie.

Next check the condition which is belongs to rating or insert movie.

Depends on your decision

the movie stored at pointer,and agin check the conditions which are action,comedy,thillor.

you select you choice and movie has to follow the what are the reuirements you mention there.

this is for only insert movie into location.

and comming to the rating here also same thing will happen but you are giving rating to particular the movie.in rating the chioces are more and different like music,games,.....you can select the movie from the availble list.