// This homework exercise array of structures and their operations, flush, and f
ID: 670317 • Letter: #
Question
// This homework exercise array of structures and their operations, flush, and file
// READ BEFORE YOU START:
// This program begins by displaying a menu to the user with options to add a movie, display the list, or quit and save the list. Your job is to implement the
// add(), display(), save(), and load() functions. Each has specific instructions included in comments above the scope of the function. A few things to consider
// are that you are given a global integer named count and an integer max defined in main. Use count to keep track of the amount of movies are stored in your list.
// max is the maximum amount of movies that you can store in your list. It is important for you to use these accordingly. Calling save() and load() has already been
// implemented for you. You only need to complete the function. You should not change any of the code that you are given, only implement code where you are asked.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#pragma warning(disable: 4996)
struct movie {
char name[100];
int rating;
};
int count = 0; // The amount of movies currently stored in your list (initialized at 0)
struct movie list[30]; // initialize movie list
// forward declaration of functions
void flush();
void branching(char, int);
void read(int);
void add(char*, int, struct movie*, int);
void display();
void save(char* fileName);
void load(char* fileName);
int main()
{
int max = 5; // The maximum total amount of movies that you can store in your list
load("Favorite_Movie_List.txt");
char ch = 'i';
ungetc(' ', stdin); // inject input buffer with a return character
printf("Welcome to your favorite movie manager! ");
do
{
printf("Please enter your selection: ");
printf(" a: add a new movie to your list ");
printf(" d: display the movies in your list ");
printf(" q: quit and save your list ");
flush(); // flush input buffer
ch = tolower(getchar());
branching(ch, max);
} while (ch != 113);
save("Favorite_Movie_List.txt");
return 0;
}
void flush()
{
int c;
do
{
c = getchar();
} while (c != ' ' && c != EOF);
}
// branch to different tasks
void branching(char c, int max)
{
switch (c)
{
case 'a':
read(max);
break;
case 'd':
display();
break;
case 'q':
break;
default:
printf("Invalid input! ");
}
}
// This function is already implemented for you. It prompts for and stores a movie along with a rating for that movie.
// It then calls the add() function (which is to be implemented) sending over those parameters (along with list, and max).
void read(int max)
{
int input = -1;
int rating;
char movie_name[100];
printf("Enter the name of the movie: ");
flush();
fgets(movie_name, sizeof(movie_name), stdin);
while (input < 1 || input > 10)
{
printf("What is your rating of the movie? (1-10): ");
scanf("%d", &input);
if (input >= 1 && input <= 10)
rating = input;
else
printf("Please enter a rating between 1 and 10. ");
}
add(movie_name, rating, list, max);
}
// Q1: add (30)
// This function is used to insert a new movie name (char* movie_name) and rating (int rating) into your list.
// Your list of movies should be sorted by rating, so you need to search for the correct index to add into your list.
// (index 0 should be the highest rated movie, index 1 should be the 2nd highest rated movie, etc.)
// Use the global integer variable "count" to keep track of how many movies you have in your list.
//
// In main, 'max' is defined as the maximum total amount of movies that you can store in your list. 'max' has been passed to this function, use it accordingly.
// Use 'max' so that if you were to change the value of max, your program would work the same (graders will change max for testing your sort).
// You can change the value of 'max' in main in order to test your code.
//
// If a movie is entered with a rating that is too low to make the list, print: " This movie is not one of your favorites! ".
// (hint: you should increase count for each movie you add, but not if you are adding to a list that already contains the 'max' amount of movies)
//
// You are not required to use pointer operations for your list but you may do so if you'd like. list/max are only passed to this function for testing purposes.
//
// If a movie with the same rating already exists in the list, you should replace that index with the new movie, and move that movie to the next index.
//
// 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
//
// (Unless max is set to 2, then it would just contain the 2 highest rated movies)
void add(char* movie_name, int rating, struct movie* list, int max)
{
}
I need help with implementing this add function in C code. I'm having a lot of difficulties with getting everything syntactically correct.
Explanation / Answer
//add and insert methods are the required ones
#include <stdio.h>
#include <string.h>
#include <ctype.h>
struct movie {
char name[100];
int rating;
};
struct movie list[30]; // initialize movie list
int count = 0; // The amount of movies currently stored in your list (initialized at 0)
void insert(struct movie* list, struct movie curMov, int index) {
int i;
struct movie temp, cur;
// printf( " Inser -> Movie: %s Rating: %d ", curMov.name, curMov.rating);
for(i = count; i >= index; i--) {
list[i+1] = list[i];
// printf( "Insert->->Movie: %s Rating: %d ", list[i].name, list[i].rating);
}
list[index] = curMov;
//increase the count
count++;
}
int minMovRating =2; // This is the minimum moving rating required TO INSERT;
void add(char* movie_name, int rating, struct movie* list, int max) {
printf("Inserting movie : %s ",movie_name);
if(rating < minMovRating) {
printf(" This movie is not one of your favorites! ");
return;
}
if(count == max) {
printf(" Max limit is reached");
return;
}
struct movie curMov;
strcpy(curMov.name, movie_name);
curMov.rating = rating;
if(count == 0) {
list[count++] = curMov;
//printf( "Movie 0: %s Rating: %d ", list[0].name, list[0].rating);
} else {
// insert the new movie based on the descending order of movie rating
int index = 0; //0 th index contains highest rating
struct movie temp;
int curCount = count;
for(index = 0; index < curCount; index++) {
// printf(" index = %d, count = %d ",index, count);
temp = list[index];
if(rating == temp.rating) {
insert(list, curMov, index);
break;
} else if (rating > temp.rating) {
insert(list, curMov, index);
break;
} else if(rating < temp.rating) {
if(index == curCount-1) {
insert(list, curMov, index+1);
}
}
}
}
}
void printMovies(struct movie* list) {
int i =0;
for(i=0; i <count; i++)
printf( "Movie: %s Rating: %d ", list[i].name, list[i].rating);
}
int main()
{
int max = 8; // The maximum total amount of movies that you can store in your list
add("The Hunger Games1", 8, list, max);
add("The Hunger Games2", 8, list, max);
add("The Hunger Games3", 9, list, max);
add("Games3", 5, list, max);
add("Games4", 7, list, max);
add("Games5", 5, list, max);
add("Games6", 6, list, max);
add("Games7", 1, list, max);
printMovies(list);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.