Question

Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

Need help for C program. Thx

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

// READ BEFORE YOU START:
// This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user 
// with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for 
// an actor/actress, delete all movies, display all movies, display a movie by index, and display a list of movies by genre. Your job is to 
// implement the delete_all(), print_by_index(), print_by_genre(), add_actor(), and find_actor() functions. Each has specific instructions 
// included in comments above of the function. A new struct has been inlcuded, which you will use to create a linked list of actors for each 
// movie in your list. You should begin by tracing the helper2 and print_helper functions that are already implemented for you. Not only will 
// this help you in understanding how to define and use pointers, it will help you understand what this program does. You should not change any 
// of the code that you are given, only implement code where you are asked. You may assume that no 2 movies will have the same name.

#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

// contains movie information
struct movie {
        struct movie* next;
        struct actor* actors; // added for hw07
        char name[100];
        int rating;
        genre type;
} *list = NULL;

// added for hw07
// contains actor information
struct actor {
        struct actor* next;
        char name[100];
};

// forward declarations
void flush();  
void branching(char c);
void helper(char c);
void add(struct movie* new_movie);

// added for hw07
void helper2(char);
void print_helper(struct movie*);
struct movie* print_all(struct movie*);
void delete_all(struct movie*);
struct movie* print_by_index(int);
struct movie* print_by_genre(int);
int add_actor(char*, char*);
struct movie* find_actor(char*);

const size_t line_size = 300; // global line_sized used in helper2() function to read input line

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

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

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

        do {
                printf("Please enter your selection\n");
                printf("\ta: add a movie\n");

                // added for hw07
                printf("\tb: add an actor/actress to a movie\n");
                printf("\tc: display a list of movies for an actor/actress\n");
                printf("\td: delete all movies\n");
                printf("\te: display all movies\n");
                printf("\tf: display a movie by index\n");
                printf("\tg: display a list of movies by genre\n");

                printf("\tq: quit \n");

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

        return 0;
}

// flush input buffer. You could use the predefined fflush()
void flush()
{
        int c;
        do {
                c = getchar();
        } while (c != '\n' && c != EOF);
}

// branch to different tasks
void branching(char c)
{
        switch (c) {
        case 'a':
                helper(c);
                break;
        case 'b': // add an actor/actress to a movie
        case 'c': // display a list of movies for an actor/actress
        case 'd': // delete all movies
        case 'e': // display all movies
        case 'f': // display a movie by index
        case 'g': // display a list of movies by genre
                helper2(c);
                break;
        case 'q':
                break;
        default:
                printf("Invalid input!\n");
        }

        if (c == 'b' || c == 'c'){
                ungetc('\n', stdin);
        }
}

// (Used for hw06 implementation)
// 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.
// Do not change anything in this function or the test cases will fail.
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')
        {
                // stores the genre of the movie into pointer
                printf("What is the genre of the movie? (action/comedy/thriller):\n");
                while (valid_genre < 0)
                {
                        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/thriller).\n");// error handling
                }
        }

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

        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):\n");
                scanf("%d", &input);

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

        // 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;
        // added for hw07
        ptr->actors = NULL;

        add(ptr);
}

// See hw06 for full functionality
void add(struct movie* new_movie)
{
        struct movie* follow = list;

        if (list == NULL) // inserting into an empty list
        {
                list = new_movie;
                return;
        }
        else if (list->rating <= new_movie->rating) // inserting at the beginning of the list
        {
                new_movie->next = list;
                list = new_movie;
                return;
        }

        struct movie* temp = list->next;

        while (temp != NULL) // inserting inbetween 2 nodes in the list
        {
                if (temp->rating <= new_movie->rating)
                {
                        new_movie->next = temp;
                        follow->next = new_movie;
                        return;
                }
                follow = temp;
                temp = temp->next;
        }
        follow->next = new_movie; // inserting at the end of the list
}

// used for hw07
// The helper2 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.
// Do not change anything in this function or the test cases will fail.
void helper2(char c)
{
        int index = 0; // used to print an index
        int genre = -1;
        char* movie_name;
        char* actor_name;
        movie_name = (char*)malloc(5000 * sizeof(char*));
        actor_name = (char*)malloc(5000 * sizeof(char*));

        if (c == 'b') // add an actor/actress to a movie
        {
                printf("Enter the actor/actress name:\n");
                flush();
                fgets(actor_name, line_size, stdin);

                printf("Enter the movie they were they in:\n");
                fgets(movie_name, line_size, stdin);

                int result = add_actor(movie_name, actor_name);

                if (result == 0)
                {
                        printf("That movie is not in your list, please add that movie first.\n\n");
                }
                else if (result == 1)
                {
                        printf("Actor added.\n\n");
                }
                else if (result == -1)
                {
                        printf("That actor is already listed for that movie.\n\n");
                }
        }

        if (c == 'c')  // display a list of movies for an actor/actress
        {
                printf("Enter the actor/actress name:\n");
                flush();
                fgets(actor_name, line_size, stdin);

                struct movie* temp = find_actor(actor_name);

                if (temp != NULL)
                {
                        printf("That actor/actress is in:\n");
                        print_helper(temp);
                }
                else printf("That actor is not in any movies in your list.\n\n", index);

        }

        if (c == 'd')  // delete all movies
        {
                delete_all(list);
                printf("All movies deleted.\n");
        }

        if (c == 'e')  // display all movies
                print_helper(print_all(list));

        if (c == 'f')  // display a movie by index
        {
                printf("Enter an index to print:\n");

                while (index < 1)
                {
                        scanf("%d", &index);
                        if (index < 1)
                                printf("Enter an index greater than 0:\n");
                }

                struct movie* temp = print_by_index(index);

                if (temp != NULL)
                {
                        struct actor* ptr = temp->actors;

                        printf("Movie at index %d:\n", index);
                        printf("Movie: %s", temp->name);
                        printf("Rating: %d\n", temp->rating);
                        printf("Genre: %s\n", gen[temp->type]);
                        printf("Actor/Actress: ");
                        if (ptr == NULL)
                        {
                                printf("no actor/actress listed.\n\n");
                        }
                        else
                        {
                                printf("\n");
                                while (ptr != NULL)
                                {
                                        printf("%s", ptr->name);
                                        ptr = ptr->next;
                                }
                                printf("\n");
                        }
                }
                else printf("There is no movie at index %d.\n\n", index);
        }

        if (c == 'g')  // display a list of movies by genre
        {
                printf("Select a genre to display\n0. action\n1. comedy\n2. thriller\n");

                while (genre < 0 || genre > 2)
                {
                        scanf("%d", &genre); // enter an integer only
                        if (genre < 0 || genre > 2)
                                printf("Select a valid option:\n"); 
                }

                struct movie* temp = print_by_genre(genre);

                if (temp != NULL)
                {
                        printf("%s movies:\n", gen[genre]);
                        print_helper(temp);
                }
                else printf("There are no %s movies in the list.\n\n", gen[genre]);
        }
}

// print_helper prints movie information from the parameter "moviesToPrint" in an organized format
// If moviesToPrint is NULL, it prints, "Your list is empty!"
// print_all and print_by_genre (which is to be implemented) use this function to print a desired list of movies
void print_helper(struct movie* moviesToPrint)
{
        struct actor* ptr;

        if (moviesToPrint == NULL)
        {
                printf("Your list is empty!\n");
                return;
        }

        while (moviesToPrint != NULL) // traverse list of movies
        {
                ptr = moviesToPrint->actors;
                printf("Movie: %s", moviesToPrint->name);
                printf("Rating: %d\n", moviesToPrint->rating);
                printf("Genre: %s\n", gen[moviesToPrint->type]);
                printf("Actor/Actress: ");
                if (ptr == NULL)
                {
                        printf("no actor/actress listed.\n\n");
                }
                else
                {
                        printf("\n");
                        while (ptr != NULL) // traverse list of actors
                        {
                                printf("%s", ptr->name);
                                ptr = ptr->next;
                        }
                        printf("\n");
                }
                moviesToPrint = moviesToPrint->next;
        }

}

// print_all simply returns the parameter that it is given to print all of the movies' information in the parameter.
struct movie* print_all(struct movie* movies)
{
        return movies;
}

// Q1 delete_all (5)
// Recursively delete the entire list of movies. The parameter movies is a pointer to your list.
// You MUST use recursion, or you will recieve a 0 for this part. You must must use comments to indicate the steps of the fantastic four step approach.
// (hint: don't forget to set your list back to NULL)
void delete_all(struct movie* movies)
{
        // add your code
        return;
}

// Q2 print_by_index (5)
// Traverse your list and return a pointer to a movie in your list at the idex of the given parameter.
// For example, if the index = 1, then you would return the first movie in the list. (At the head, and also, the highest rated movie).
// If the list does not contain the amount that is given by the index, return NULL.
//   For exmample, if you have a list of 1 movie, and index = 2, return NULL.
struct movie* print_by_index(int index)
{
        // add your code
        return NULL;
}

// Q3 print_by_genre (10)
// Traverse your list and return a new list filled with movies that are of the type given by the integer type parameter.
// Use type-casting to compare the integer value to the genre value associated with each pointer in the list. (see the helper() function for an example)
// The movies in the returned list should contain all of the information available for that movie (ex: name, rating, actors, etc.)
// The list of movies that you return should be in order of rating from highest to lowest (the head should be the highest rated movie.)
// (Keep in mind that your "list" is already sorted by rating!)
// If no movies in your list are of the genre type given by your parameter "type", then return NULL
struct movie* print_by_genre(int type)
{
        // add your code
        return NULL;
}

// Q4 add_actor (15)
// Traverse your list to find a movie in your list with the name that matches the parameter "movie_name"
// Then, add the actor/actress to linked list of "actors" in your linked list of "movies"
// The list of actor/actress should be in alphabetical order. Do this by using the strcmp() method
// For an example of how to access and modify actor information, refer to the helper2 and print_helper functions
//      For example: If John Cena is already in your list of actor/actress, and you add the actor Jon Cena, the list would print as:

//       Actor/Actress: 
//       John Cena
//       Jon Cena

// If the movie is in the list, add the actor/actress to the actor/actress list, and return 1
// If the movie is not in the list, return 0
// Do not add duplicate actor/actress. If John Cena is already in your list of actor/actress, and actor_name == John Cena, return -1;
// NOTE : You can assume that no 2 movies in the list will have the same name
int add_actor(char* movie_name, char* actor_name)
{
        // add your code
        return 0;
}

// Q5 find_actor (15)
// Traverse your list and return a new list filled with movies that include the actor name given by the parameter actor_name
// The movies in the returned list should contain all of the information available for that movie (ex: name, rating, other actors, etc.)
// The list of movies that you return should be in order of rating from highest to lowest (the head should be the highest rated movie.)
// (Keep in mind that your "list" is already sorted by rating!)
// If no movies in your list contain the actor name given by the parameter actor_name, then return NULL
// (hint: this is similar to your print_by_genre function)
struct movie* find_actor(char* actor_name)
{
        // add your code
        return NULL;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the completed code for the question. Sample output is shown at end. Please don't forget to rate the anwer if it helped.Thank you very much.

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

// READ BEFORE YOU START:
// This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user
// with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for
// an actor/actress, delete all movies, display all movies, display a movie by index, and display a list of movies by genre. Your job is to
// implement the delete_all(), print_by_index(), print_by_genre(), add_actor(), and find_actor() functions. Each has specific instructions
// included in comments above of the function. A new struct has been inlcuded, which you will use to create a linked list of actors for each
// movie in your list. You should begin by tracing the helper2 and print_helper functions that are already implemented for you. Not only will
// this help you in understanding how to define and use pointers, it will help you understand what this program does. You should not change any
// of the code that you are given, only implement code where you are asked. You may assume that no 2 movies will have the same name.

#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

// contains movie information
struct movie {
struct movie* next;
struct actor* actors; // added for hw07
char name[100];
int rating;
genre type;
} *list = NULL;

// added for hw07
// contains actor information
struct actor {
struct actor* next;
char name[100];
};

// forward declarations
void flush();
void branching(char c);
void helper(char c);
void add(struct movie* new_movie);

// added for hw07
void helper2(char);
void print_helper(struct movie*);
struct movie* print_all(struct movie*);
void delete_all(struct movie*);
struct movie* print_by_index(int);
struct movie* print_by_genre(int);
int add_actor(char*, char*);
struct movie* find_actor(char*);

const size_t line_size = 300; // global line_sized used in helper2() function to read input line

int main()
{
char ch = 'i';
  
ungetc('\n', stdin); // inject input buffer with a return character
  
printf("Welcome to the movie manager!\n\n");
  
do {
printf("Please enter your selection\n");
printf("\ta: add a movie\n");
  
// added for hw07
printf("\tb: add an actor/actress to a movie\n");
printf("\tc: display a list of movies for an actor/actress\n");
printf("\td: delete all movies\n");
printf("\te: display all movies\n");
printf("\tf: display a movie by index\n");
printf("\tg: display a list of movies by genre\n");
  
printf("\tq: quit \n");
  
flush(); // flush input buffer
ch = tolower(getchar());
branching(ch);
} while (ch != 113);
  
return 0;
}

// flush input buffer. You could use the predefined fflush()
void flush()
{
int c;
do {
c = getchar();
} while (c != '\n' && c != EOF);
}

// branch to different tasks
void branching(char c)
{
switch (c) {
case 'a':
helper(c);
break;
case 'b': // add an actor/actress to a movie
case 'c': // display a list of movies for an actor/actress
case 'd': // delete all movies
case 'e': // display all movies
case 'f': // display a movie by index
case 'g': // display a list of movies by genre
helper2(c);
break;
case 'q':
break;
default:
printf("Invalid input!\n");
}
  
if (c == 'b' || c == 'c'){
ungetc('\n', stdin);
}
}

// (Used for hw06 implementation)
// 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.
// Do not change anything in this function or the test cases will fail.
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')
{
// stores the genre of the movie into pointer
printf("What is the genre of the movie? (action/comedy/thriller):\n");
while (valid_genre < 0)
{
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/thriller).\n");// error handling
}
}
  
//stores the name of the movie into pointer
printf("Enter the name of the movie:\n");
flush();
fgets(ptr->name, sizeof(ptr->name), stdin);
  
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):\n");
scanf("%d", &input);
  
if (input >= 1 && input <= 10)
ptr->rating = input;
else
printf("Please enter a rating between 1 and 10.\n");
}
  
// 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;
// added for hw07
ptr->actors = NULL;
  
add(ptr);
}

// See hw06 for full functionality
void add(struct movie* new_movie)
{
struct movie* follow = list;
  
if (list == NULL) // inserting into an empty list
{
list = new_movie;
return;
}
else if (list->rating <= new_movie->rating) // inserting at the beginning of the list
{
new_movie->next = list;
list = new_movie;
return;
}
  
struct movie* temp = list->next;
  
while (temp != NULL) // inserting inbetween 2 nodes in the list
{
if (temp->rating <= new_movie->rating)
{
new_movie->next = temp;
follow->next = new_movie;
return;
}
follow = temp;
temp = temp->next;
}
follow->next = new_movie; // inserting at the end of the list
}

// used for hw07
// The helper2 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.
// Do not change anything in this function or the test cases will fail.
void helper2(char c)
{
int index = 0; // used to print an index
int genre = -1;
char* movie_name;
char* actor_name;
movie_name = (char*)malloc(5000 * sizeof(char*));
actor_name = (char*)malloc(5000 * sizeof(char*));
  
if (c == 'b') // add an actor/actress to a movie
{
printf("Enter the actor/actress name:\n");
flush();
fgets(actor_name, line_size, stdin);
  
printf("Enter the movie they were they in:\n");
fgets(movie_name, line_size, stdin);
  
int result = add_actor(movie_name, actor_name);
  
if (result == 0)
{
printf("That movie is not in your list, please add that movie first.\n\n");
}
else if (result == 1)
{
printf("Actor added.\n\n");
}
else if (result == -1)
{
printf("That actor is already listed for that movie.\n\n");
}
}
  
if (c == 'c') // display a list of movies for an actor/actress
{
printf("Enter the actor/actress name:\n");
flush();
fgets(actor_name, line_size, stdin);
  
struct movie* temp = find_actor(actor_name);
  
if (temp != NULL)
{
printf("That actor/actress is in:\n");
print_helper(temp);
}
else printf("That actor is not in any movies in your list.\n\n", index);
  
}
  
if (c == 'd') // delete all movies
{
delete_all(list);
printf("All movies deleted.\n");
}
  
if (c == 'e') // display all movies
print_helper(print_all(list));
  
if (c == 'f') // display a movie by index
{
printf("Enter an index to print:\n");
  
while (index < 1)
{
scanf("%d", &index);
if (index < 1)
printf("Enter an index greater than 0:\n");
}
  
struct movie* temp = print_by_index(index);
  
if (temp != NULL)
{
struct actor* ptr = temp->actors;
  
printf("Movie at index %d:\n", index);
printf("Movie: %s", temp->name);
printf("Rating: %d\n", temp->rating);
printf("Genre: %s\n", gen[temp->type]);
printf("Actor/Actress: ");
if (ptr == NULL)
{
printf("no actor/actress listed.\n\n");
}
else
{
printf("\n");
while (ptr != NULL)
{
printf("%s", ptr->name);
ptr = ptr->next;
}
printf("\n");
}
}
else printf("There is no movie at index %d.\n\n", index);
}
  
if (c == 'g') // display a list of movies by genre
{
printf("Select a genre to display\n0. action\n1. comedy\n2. thriller\n");
  
while (genre < 0 || genre > 2)
{
scanf("%d", &genre); // enter an integer only
if (genre < 0 || genre > 2)
printf("Select a valid option:\n");
}
  
struct movie* temp = print_by_genre(genre);
  
if (temp != NULL)
{
printf("%s movies:\n", gen[genre]);
print_helper(temp);
}
else printf("There are no %s movies in the list.\n\n", gen[genre]);
}
}

// print_helper prints movie information from the parameter "moviesToPrint" in an organized format
// If moviesToPrint is NULL, it prints, "Your list is empty!"
// print_all and print_by_genre (which is to be implemented) use this function to print a desired list of movies
void print_helper(struct movie* moviesToPrint)
{
struct actor* ptr;
  
if (moviesToPrint == NULL)
{
printf("Your list is empty!\n");
return;
}
  
while (moviesToPrint != NULL) // traverse list of movies
{
ptr = moviesToPrint->actors;
printf("Movie: %s", moviesToPrint->name);
printf("Rating: %d\n", moviesToPrint->rating);
printf("Genre: %s\n", gen[moviesToPrint->type]);
printf("Actor/Actress: ");
if (ptr == NULL)
{
printf("no actor/actress listed.\n\n");
}
else
{
printf("\n");
while (ptr != NULL) // traverse list of actors
{
printf("%s", ptr->name);
ptr = ptr->next;
}
printf("\n");
}
moviesToPrint = moviesToPrint->next;
}
  
}

// print_all simply returns the parameter that it is given to print all of the movies' information in the parameter.
struct movie* print_all(struct movie* movies)
{
return movies;
}

// Q1 delete_all (5)
// Recursively delete the entire list of movies. The parameter movies is a pointer to your list.
// You MUST use recursion, or you will recieve a 0 for this part. You must must use comments to indicate the steps of the fantastic four step approach.
// (hint: don't forget to set your list back to NULL)
void delete_all(struct movie* movies)
{
// add your code
if(movies == NULL) //base condition
{
list = NULL;
return;
}
else
{
delete_all(movies->next);
free(movies);
}
return;
}

// Q2 print_by_index (5)
// Traverse your list and return a pointer to a movie in your list at the idex of the given parameter.
// For example, if the index = 1, then you would return the first movie in the list. (At the head, and also, the highest rated movie).
// If the list does not contain the amount that is given by the index, return NULL.
// For exmample, if you have a list of 1 movie, and index = 2, return NULL.
struct movie* print_by_index(int index)
{
// add your code
int i = 1;
struct movie *m = list;
for(i = 1; i < index && m != NULL; i++)
m = m->next;
return m;
}

// Q3 print_by_genre (10)
// Traverse your list and return a new list filled with movies that are of the type given by the integer type parameter.
// Use type-casting to compare the integer value to the genre value associated with each pointer in the list. (see the helper() function for an example)
// The movies in the returned list should contain all of the information available for that movie (ex: name, rating, actors, etc.)
// The list of movies that you return should be in order of rating from highest to lowest (the head should be the highest rated movie.)
// (Keep in mind that your "list" is already sorted by rating!)
// If no movies in your list are of the genre type given by your parameter "type", then return NULL
struct movie* print_by_genre(int type)
{
// add your code
struct movie* list2 = NULL;
struct movie* curr = list;
struct movie* last = NULL;
  
  
while(curr != NULL)
{
if((int)curr->type == type)
{
if(list2 == NULL) //if list is not yet created
list2 = last = (struct movie *) malloc(sizeof(struct movie));
else
{
last->next = (struct movie *) malloc(sizeof(struct movie));
last = last->next;
}
//copy over the contents from curr
*last = *curr;
last->next = NULL;
}
curr = curr->next;
}
return list2;
}

// Q4 add_actor (15)
// Traverse your list to find a movie in your list with the name that matches the parameter "movie_name"
// Then, add the actor/actress to linked list of "actors" in your linked list of "movies"
// The list of actor/actress should be in alphabetical order. Do this by using the strcmp() method
// For an example of how to access and modify actor information, refer to the helper2 and print_helper functions
// For example: If John Cena is already in your list of actor/actress, and you add the actor Jon Cena, the list would print as:

// Actor/Actress:
// John Cena
// Jon Cena

// If the movie is in the list, add the actor/actress to the actor/actress list, and return 1
// If the movie is not in the list, return 0
// Do not add duplicate actor/actress. If John Cena is already in your list of actor/actress, and actor_name == John Cena, return -1;
// NOTE : You can assume that no 2 movies in the list will have the same name
int add_actor(char* movie_name, char* actor_name)
{
// add your code
struct movie* m = list;
struct actor* prev;
struct actor* curr;
struct actor* a;
  
//locate the movie
while(m != NULL)
{
if(strcmp(m->name, movie_name) == 0)
break;
m = m->next;
}
  
if(m == NULL) // not in list
return 0;
  
prev = NULL;
curr = m->actors;
//locate the corrct place to add actor
while(curr != NULL)
{
if(strcmp(actor_name, curr->name) == 0)//duplicate
return -1;
else if(strcmp(actor_name, curr->name) < 0)
break;
else
{
prev = curr;
curr = curr->next;
}
}
  
a = (struct actor*)malloc(sizeof(struct actor));
strcpy(a->name, actor_name);
if(prev == NULL) //actor should come in beginning of list
{
a->next = m->actors;
m->actors = a;
}
else //somewhere in between/end
{
a->next = curr;
prev->next = a;
}
return 1;
}

// Q5 find_actor (15)
// Traverse your list and return a new list filled with movies that include the actor name given by the parameter actor_name
// The movies in the returned list should contain all of the information available for that movie (ex: name, rating, other actors, etc.)
// The list of movies that you return should be in order of rating from highest to lowest (the head should be the highest rated movie.)
// (Keep in mind that your "list" is already sorted by rating!)
// If no movies in your list contain the actor name given by the parameter actor_name, then return NULL
// (hint: this is similar to your print_by_genre function)
struct movie* find_actor(char* actor_name)
{
// add your code
struct movie* list2 = NULL;
struct movie* curr = list;
struct movie* last = NULL;
struct actor* a;
int present;
  
while(curr != NULL)
{
present = 0;
a = curr->actors;
while(a != NULL)
{
if(strcmp(a->name, actor_name) == 0)
{
present = 1;
break;
}
a = a->next;
}
if(present)
{
if(list2 == NULL)
list2 = last = (struct movie *) malloc(sizeof(struct movie));
else
{
last->next = (struct movie *) malloc(sizeof(struct movie));
last = last->next;
}
//copy the contents from curr
*last = *curr;
last->next = NULL;
}
curr = curr->next;
}
return list2;
}

output


Welcome to the movie manager!
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
a
What is the genre of the movie? (action/comedy/thriller):
comedy
Enter the name of the movie:
movie1
What is your rating of the movie? (1-10):
6
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
a
What is the genre of the movie? (action/comedy/thriller):
thriller
Enter the name of the movie:
movie2
What is your rating of the movie? (1-10):
3
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
a
What is the genre of the movie? (action/comedy/thriller):
action
Enter the name of the movie:
movie3
What is your rating of the movie? (1-10):
6
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
a
What is the genre of the movie? (action/comedy/thriller):
comedy
Enter the name of the movie:
movie4
What is your rating of the movie? (1-10):
8
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
e
Movie: movie4
Rating: 8
Genre: comedy
Actor/Actress: no actor/actress listed.
Movie: movie3
Rating: 6
Genre: action
Actor/Actress: no actor/actress listed.
Movie: movie1
Rating: 6
Genre: comedy
Actor/Actress: no actor/actress listed.
Movie: movie2
Rating: 3
Genre: thriller
Actor/Actress: no actor/actress listed.
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
b
Enter the actor/actress name:
actor3
Enter the movie they were they in:
movie3
Actor added.
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
b
Enter the actor/actress name:
actor2
Enter the movie they were they in:
movie2
Actor added.
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
e
Movie: movie4
Rating: 8
Genre: comedy
Actor/Actress: no actor/actress listed.
Movie: movie3
Rating: 6
Genre: action
Actor/Actress:
actor3
Movie: movie1
Rating: 6
Genre: comedy
Actor/Actress: no actor/actress listed.
Movie: movie2
Rating: 3
Genre: thriller
Actor/Actress:
actor2
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
b
Enter the actor/actress name:
actor1
Enter the movie they were they in:
movie2
Actor added.
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
e
Movie: movie4
Rating: 8
Genre: comedy
Actor/Actress: no actor/actress listed.
Movie: movie3
Rating: 6
Genre: action
Actor/Actress:
actor3
Movie: movie1
Rating: 6
Genre: comedy
Actor/Actress: no actor/actress listed.
Movie: movie2
Rating: 3
Genre: thriller
Actor/Actress:
actor1
actor2
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
b
Enter the actor/actress name:
actor1
Enter the movie they were they in:
movie1
Actor added.
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
e
Movie: movie4
Rating: 8
Genre: comedy
Actor/Actress: no actor/actress listed.
Movie: movie3
Rating: 6
Genre: action
Actor/Actress:
actor3
Movie: movie1
Rating: 6
Genre: comedy
Actor/Actress:
actor1
Movie: movie2
Rating: 3
Genre: thriller
Actor/Actress:
actor1
actor2
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
c
Enter the actor/actress name:
actor1
That actor/actress is in:
Movie: movie1
Rating: 6
Genre: comedy
Actor/Actress:
actor1
Movie: movie2
Rating: 3
Genre: thriller
Actor/Actress:
actor1
actor2
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
f
Enter an index to print:
2
Movie at index 2:
Movie: movie3
Rating: 6
Genre: action
Actor/Actress:
actor3
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
f
Enter an index to print:
10
There is no movie at index 10.
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
g
Select a genre to display
0. action
1. comedy
2. thriller
1
comedy movies:
Movie: movie4
Rating: 8
Genre: comedy
Actor/Actress: no actor/actress listed.
Movie: movie1
Rating: 6
Genre: comedy
Actor/Actress:
actor1
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
c
Enter the actor/actress name:
actor4
That actor is not in any movies in your list.
Please enter your selection
a: add a movie
b: add an actor/actress to a movie
c: display a list of movies for an actor/actress
d: delete all movies
e: display all movies
f: display a movie by index
g: display a list of movies by genre
q: quit
q

Add a comment
Know the answer?
Add Answer to:
Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE...

    ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE ANSWERING CODING SECTIONS HW07 #Q1-Q5 HW08 #Q1-Q2 // READ BEFORE YOU START: // Please read the given Word document for the project description with an illustrartive diagram. // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, standard, and a linked list of absents. // Please read the instructions...

  • // READ BEFORE YOU START: // You are given a partially completed program that creates a...

    // READ BEFORE YOU START: // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, gender, class, standard, and roll_number. // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow the directions carefully. // If you modify any of the given code, the return types, or the parameters, you...

  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */...

    Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */ struct myWord{    char Word[21];    int Length; }; int tokenizeLine(char line[], struct myWord wordList[]); void printList(struct myWord wordList[], int size); void sortList(struct myWord wordList[], int size); /** * main function */ int main() {    struct myWord wordList[20];    char line[100];    printf("Enter an English Sentence:\n");    gets(line);    int size = tokenizeLine(line, wordList);    printf("\n");    printf("Unsorted word list.\n");    printList(wordList, size);...

  • I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves...

    I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves the array of structures to file. It is already implemented for you. // You should understand how this code works so that you know how to use it for future assignments. void save(char* fileName) { FILE* file; int i; file = fopen(fileName, "wb"); fwrite(&count, sizeof(count), 1, file); for (i = 0; i < count; i++) { fwrite(list[i].name, sizeof(list[i].name), 1, file); fwrite(list[i].class_standing, sizeof(list[i].class_standing), 1, file);...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • Based on this program modify the source code so that it will able to save the student record into a database txt and able to display and modify from a database txt as well. you will also need to able...

    Based on this program modify the source code so that it will able to save the student record into a database txt and able to display and modify from a database txt as well. you will also need to able to modify the code so it will accept name such as "john kenny " and the progrom should also ask for the student id number. #include<iostream> #include<stdlib.h> using namespace std; struct st { int roll; char name[50]; char grade; struct...

  • // C code // If you modify any of the given code, the return types, or...

    // C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList {    struct patient *patient;    struct patientList *next; } *list = NULL;  ...

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given...

    // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given a partially completed program that creates a list of patients, like patients' record. // Each record has this information: patient's name, doctor's name, critical level of patient, room number. // The struct 'patientRecord' holds information of one patient. Critical level is enum type. // An array of structs called 'list' is made to hold the list of patients. // To begin, you should trace...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT