hw06
cpp
keyboard_arrow_up
School
Dartmouth College *
*We aren’t endorsed by this school
Course
51
Subject
Information Systems
Date
Nov 24, 2024
Type
cpp
Pages
3
Uploaded by ProfWildcatMaster683
/***********************************************
Author: Shreya Mishra
Date: November 5th, 2023
Purpose: It analyzes movie ratings stored inside the file ratings.txt.
Consists of an associative container (map) to store and
process the ratings.After reading the ratings from the file and populating the
movieRatings map,
the program calculates and displays the average rating for each movie by
dividing the sum of ratings by the total number of reviews for that movie.
Sources of Help: Lectures 28-31, cplusplus.com,
Time Spent: 3-4 hours
***********************************************/
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <vector>
using namespace std;
// Function to calculate the average rating for a movie
// Input: vector of integers representing the movie ratings
// Output: average of the ratings
double calAvgRating(vector<int> ratings) {
int sum = 0.0;
for (int rating : ratings) {
sum += rating;
}
return (sum / (double)ratings.size());
}
// Function to display movie ratings
// Input: map of <string, vector<int>> where string is movie and vector
holds individual ratings
void displayMovieRatings(map<string, vector<int>> movieRatings) {
cout << "Movie Ratings: " << endl;
for (auto& it : movieRatings) {
string movieName = it.first;
vector<int> ratings = it.second;
double averageRating = calAvgRating(ratings);
int numReviews = ratings.size();
cout << movieName << ": " << numReviews << " reviews, average of "
<< averageRating << " / 5" << endl;
}
}
int main(int argc, char* argv[]) {
map<string, vector<int>> movieRatings; //string: name of movie
vector<int>: ratings
ifstream inputFile("ratings.txt"); // Replace 'ratings.txt' with the actual file
name
if (!inputFile.is_open()) {
cout << "Error opening file." << endl;
return 1;
}
int numRatings;
inputFile >> numRatings;
//first line of the file contains the #ratings to
read
for (int i = 0; i < numRatings; ++i) {
string movieName;
int rating;
inputFile.ignore(); // Ignore the newline character
getline(inputFile, movieName);
inputFile >> rating;
movieRatings[movieName].push_back(rating);
}
inputFile.close();
displayMovieRatings(movieRatings);
return 0;
}
/*
Computing III -- COMP.2010 Honor Statement
The practice of good ethical behavior is essential for maintaining
good order in the classroom, providing an enriching learning
experience for students, and as training as a practicing computing
professional upon graduation. This practice is manifested in the
Universitys Academic Integrity policy. Students are expected to
strictly avoid academic dishonesty and adhere to the Academic
Integrity policy as outlined in the course catalog. Violations will
be dealt with as outlined therein.
All programming assignments in this class are to be done by the
student alone. No outside help is permitted except the instructor and
approved tutors.
I certify that the work submitted with this assignment is mine and was
generated in a manner consistent with this document, the course
academic policy on the course website on Blackboard, and the UMass
Lowell academic code.
Date: November 5th, 2023
Name: Shreya Mishra
*/
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help