Write a program that reads a file in this format, calculates the average rating for each movie, and outputs the average along with the number of reviews.

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 the sample input file for the rating with four unique movies and seven ratings:

File: ratings.txt

7

Avengers: Endgame

4

Harry Potter and the Order of the Phoenix

5

The Bourne Ultimatum

3

Harry Potter and the Order of the Phoenix

4

The Bourne Ultimatum

4

Wall – E

4

Star Wars: Rise of Skywalker

1

Write a program that reads a file in this format, calculates the average rating for each movie, and outputs the average along with the number of reviews.

Star Wars: The Rise of Skywalker: 1 review, average of 1.0 / 5

Avengers: Endgame: 3 reviews, average of 4.3 / 5

Knives Out: 2 reviews, average of 3.5 / 5

Sonic the Hedgehog: 1 review, average of 4.0 / 5

Use a map or multiple maps to calculate the output. Your map(s) 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.

 

Summary

A set of movies is corresponding ratings are given from the question which is given which a file read input in the given. Then the average rating is given and gets printed as output to the console.

The main of this is written the code in C++.

 

Explanation 

Firstly, and if stream object is declared to open the file ratings. Then the number of inputs which is the first line of the input file is read and then the set of movies and corresponding rating are read into two arrays. Then, the arrays are passed through two loops to find the unique set of movies and how many times each is reviewed and its corresponding sum of ratings. Then using the sum of ratings and the number of reviews, the average rating of each movie Is found. Then these results are stored in a map by creating a map having strings as both the values. Then, from the map, the desired results are printed as output to the console.

Code

#include <iostream>
using namespace std;
#include <fstream> 
#include <map>
#include <sstream>
#include <iterator>
#include <string>
int main()
{
ifstream f("ratings.txt");
string s;
getline (f,s);
stringstream prav(s); 
int n;
prav>>n;
string movies [n]; 
string rat[n];
/* to get the movies and ratings from the input line*/
for(int i=0;i<n; i++)
{
    getline (f,movies[i]);
    getline (f,rat[i]);
}
int cc=0; 
string m[n]; 
int k[n]; 
float r[n];
for(int i=0;i<n; i++)
{
k[i]=0; r[i]=0;
}
/* to find the reviews and find the average */ 
for(int i=0;i<n;i++)
{
bool b=true; 
for(int j=0; j<cc; j++)
{
    if(movies[i]==m[j])
    {
        stringstream pp(rat[i]);
        int kk1; pp>>kk1;
        r[j]+=kk1;
        k[j]++;
        b=false;
    }
}
if(b==true)
{
     stringstream pp(rat[i]);
     m[cc]=movies[i]; 
     int kk1;
     pp>>kk1;
     r[cc]+=kk1;
     k[cc]++;
     cc++;

    }
}
for(int i=0;i<cc;i++)
{ 
    r[i]=(r[i]/k[i]); 
    r[i]=(r[i]/5.0)*k[i];
}
map<string, string> m1; 
for(int i=0;i<cc;i++)
{
string s1;
s1=m[i]; string s3=to_string(r[i]); 
m1.insert (pair<string, string>(s1, s3));
}
int i=0;
map<string, string>::iterator it;
for(it=m1.begin(); it!=m1.end(); ++it)
{
cout<<it->first<<endl; 
cout<<k[i]<<" reviews, ";
cout<<"average of "<<it->second<<" / 5"<<endl;
        i++;
    }
}

Output

average rating for each movie

 

Also, the read another blog which is to write a relational schema for the diagram.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *