Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You have collected a file of movie ratings where each movie is rated from 1 (bad

ID: 3918429 • Letter: Y

Question

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 helpful to discard the newline character as you read the file data. You can do this using the following line: fin.ignore(2,' '); . 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 data for your program, make sure the output matches the output above. You can use the code below to get started.

#include <fstream>

#include <string>

#include <iostream>

#include <cstdlib>

#include <map>

using std::ifstream;

using std::cout;

using std::endl;

using std::string;

using std::cin;

using std::map;

int main()

{

ifstream fin; // Input file

fin.open("movies.txt"); // Name of the text file with ratings, has to be within the same directory as this code

if (fin.fail())

{

cout << "Input file opening failed. ";

exit(1);

}

//YOUR CODE FOR READING THE TEXT FILE GOES HERE

fin.close( );

//YOUR CODE FOR CALCULATING/OUTPUTTING THE RATINGS GOES HERE

return 0;

}

Explanation / Answer

#include <fstream>

#include <string>

#include <iostream>

#include <cstdlib>

#include <map>

using std::ifstream;

using std::cout;

using std::endl;

using std::string;

using std::cin;

using std::map;

int main()

{

ifstream fin; // Input file

fin.open("movies.txt"); // Name of the text file with ratings, has to be within the same directory as this code

if (fin.fail())

{

cout << "Input file opening failed. ";

exit(1);

}

//YOUR CODE FOR READING THE TEXT FILE GOES HERE

int i = 0;

string delimiter = " ";

if (fin.is_open())

{

getline (fin,line);

string noofReview = line.substr(0, line.find(delimiter));

int noofRev;

sscanf(noofReview, "%d", &noofRev);

input[i++] = line.substr(line.find(delimiter)+1, strlen(line));

while ( getline (fin,line) )

{

//cout << line << ' ';

intput[i++] = line;

}

}

fin.close( );

//YOUR CODE FOR CALCULATING/OUTPUTTING THE RATINGS GOES HERE

map <string, int> movieReview;

map <string, int> movieoccurences;

map <string, int> :: iterator itr;

int j,k;

for(j=0; j<i/2; j++){

if(movieReview.find(input[j])){

for (itr = movieReview.begin(); itr != movieReview.end(); ++itr)

{

if(itr->first.equals(input[j])){

movieReview.insert(input[j], itr.second + input[j+1]);

}

}

for (itr = movieOccurences.begin(); itr != movieOccurences.end(); ++itr)

{

if(itr->first.equals(input[j])){

movieOccurences.insert(input[j], itr.second + 1);

}

}

} else{

movieReview.insert(input[j], input[j+1]);

movieOccurences.insert(input[j], 1);

k=0;

}

}

for (itr = movieOccurences.begin(); itr != movieOccurences.end(); ++itr){

tempItr = movieReview.begin();

cout<< itr.first << ":" << itr.second << ", average" << tempItr.second / itr.second << "/ 5";

tempItr++;

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote