Certainly! Here is another possible implementation using a std::map to efficiently…
- User
Certainly! Here is another possible implementation using a std::map to efficiently organize and process the movie ratings:
#include #include #include #include
// Function to read ratings from a file and calculate average ratings void analyzeRatings(const std::string& filename) { std::ifstream file(filename); if (!file) { std::cout << “Error opening file.” << std::endl; return; }
std::map<std::string, std::pair<int, double>> movieRatings; int totalRatings; file >> totalR - Assistant
Ratings;
while (file) { std::string movie; int rating; file >> movie >> rating;
// Check if movie is already in the map if (movieRatings.find(movie) != movieRatings.end()) { // Add to the total ratings and calculate new average movieRatings[movie].first += rating; movieRatings[movie].second = static_cast<double>(movieRatings[movie].first) / movieRatings[movie].second; } else { // Initialize new entry with the first rating movieRatings[movie] = std::make_pair(rating, rating); } } // Output the average ratings for each movie for (const auto& entry : movieRatings) { std::cout << "Movie: " << entry.first << ", Average Rating: " << entry.second.second << std::endl; } file.close(); - User
Write an consider using associative container(s) to efficiently organize the process the data and generate the output.
COMP.2010 HW 06 F23 - Dr. Lin Due date: 11-06-2023 (M), 9am Close date: 11-20-2023 (M), 9awhm Submission: a C++ file (hw6.cpp) on Blackboard Points: 40 Description: Movie Ratings Analysis You have collected a file of movie ratings where each movie is rated from 1 (bad) to 5 (excellent). The first line of the file is a number that identifies how many ratings are in the file. Each rating then consists of two lines: the name of the movie followed by the numeric rating from 1 to 5. Your task is to write a program that reads in a file in this format, calculates the average rating for each movie, and outputs the average along with the number of reviews. Here is a sample rating file with four unique movies and seven ratings: 7 Happy Feet 4 Happy Feet 5 Pirates of the Caribbean 3 Happy Feet 4 Pirates of the Caribbean 4 Flags of Our Fathers 5 Gigli 1 Here is the desired output for the sample data: Happy Feet: 3 reviews, average of 4.3 / 5 Pirates of the Caribbean: 2 reviews, average of 3.5 / 5 Flags of Our Father: 1 review, average of 5 / 5 Gigli: 1 review, average of 1 / 5 Suggestion: consider using associative container(s) to efficiently organize the process the data and generate the output. Rubric: • Please read GradingRubricGuidline.pdf on Blackboard • 80% for the correctness of the code • 20% for the documentation and the programming style, including but not limited to the following rules below. • Each function must be commented with a description of what it does, the role of its parameters and the meaning of its return value. • Indentation level of your code must correspond to the nesting level. - Assistant
- User
Write an consider using associative container(s) to efficiently organize the process the data (using an ordered map) and generate the output. COMP.2010 HW 06 F23 - Dr. Lin Due date: 11-06-2023 (M), 9am Close date: 11-20-2023 (M), 9awhm Submission: a C++ file (hw6.cpp) on Blackboard Points: 40 Description: Movie Ratings Analysis You have collected a file of movie ratings where each movie is rated from 1 (bad) to 5 (excellent). The first line of the file is a number that identifies how many ratings are in the file. Each rating then consists of two lines: the name of the movie followed by the numeric rating from 1 to 5. Your task is to write a program that reads in a file in this format, calculates the average rating for each movie, and outputs the average along with the number of reviews. Here is a sample rating file with four unique movies and seven ratings: 7 Happy Feet 4 Happy Feet 5 Pirates of the Caribbean 3 Happy Feet 4 Pirates of the Caribbean 4 Flags of Our Fathers 5 Gigli 1 Here is the desired output for the sample data: Happy Feet: 3 reviews, average of 4.3 / 5 Pirates of the Caribbean: 2 reviews, average of 3.5 / 5 Flags of Our Father: 1 review, average of 5 / 5 Gigli: 1 review, average of 1 / 5 Suggestion: consider using associative container(s) to efficiently organize the process the data and generate the output. Rubric: • Please read GradingRubricGuidline.pdf on Blackboard • 80% for the correctness of the code • 20% for the documentation and the programming style, including but not limited to the following rules below. • Each function must be commented with a description of what it does, the role of its parameters and the meaning of its return value. • Indentation level of your code must correspond to the nesting level.
- Assistant