subject

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. 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

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 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

use a map or multiple maps to generate the output. your map should index from a string representing each movie’s name to integers that store the number of reviews for the movie and the sum of the ratings for the movie. you will need to use the fstream library to read in file data. it may be to discard the newline character as you read the file data. you can do this using the following line: fin. ignore(2,'\n'); . use it after reading any movie ratings. you will also need to clean up the title as it will contain a linefeed character at the end of it, just remove it after reading the movie name. assume a file named "movies. txt" is used as the input file and it contains the list of movies above. this will be the test file for your program, make sure the output matches the output above.

this is in the programming language c++. i have already started with the code below. me finish this problem with the code i already started instead of starting from scratch. explain each added line of code well:

#include
#include
#include
#include
using namespace std;

int main()
{
int numberratings, rating;
string moviename;
map mapratings; //maps movie rating to total amount of specific movie ratings
map> mapreviews; //maps above map to movie name

ifstream input; //reads header file
input. open("movies. txt"); //opens movies. txt
input > > numberratings; //inputs the number of ratings
input. ignore(); //ignores next line

for (int i = 0; i < numberratings; ++i) {
getline(input, moviename); //inputs the movie name
input > > rating; //inputs that movie's rating
input. ignore(); //ignores next line
++mapreviews[moviename][rating]; //increments map which will store the movie name and rating
}

map: : iterator mapiter1;
map> : : iterator mapiter2;

for (mapiter2 = mapreviews. begin(); mapiter2 ! = mapreviews. end(); ++mapiter2) {
cout < < endl < < mapiter2-> first < < ": ";
for (mapiter1 = mapiter2-> second. begin(); mapiter1 ! = mapiter2-> second. end(); ++mapiter1) {
cout < < mapiter1-> first < < " reviews, average of " < < / mapiter-> first < < " / 5" < < endl;
}
}

return 0;
}

ansver
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 10:00, fnaflover8505
Businesses allocate resources for their best and most productive uses. the more a resource, the more costly it will be. a manufacturer that requires scarce and costly resources is likely to charge for its products.
Answers: 2
image
Computers and Technology, 22.06.2019 11:00, bombbomb2157
Eva has many contacts on the professional networking site she uses which contacts are considered second degree
Answers: 3
image
Computers and Technology, 22.06.2019 23:30, bri2008
Which of the following is not a symptom of chronic fatigue syndrome
Answers: 2
image
Computers and Technology, 23.06.2019 07:00, MissSmartyPants88
To produce a starlight effect in her photograph, lina should choose the filter for her camera.
Answers: 1
You know the right answer?
You have collected a file of movie ratings where each movie is rated from 1 (bad) to 5 (excellent)....

Questions in other subjects:

Konu
English, 23.05.2021 21:00
Konu
Mathematics, 23.05.2021 21:00