SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Standard Template Library
Exercise:
Programming Projects
Question:10 | ISBN:9780132846813 | Edition: 5

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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <algorithm>
#include <cctype>
using namespace std;
#include "Map.h"
int main()
{

    ifstream input;
    int num, rate;
    string name;
    map<int, int> mapMovie;
    map<string, map<int, int>> mapReview;
    input.open("movies.txt");
    input >> num;
    input.ignore();

    for (int temp = 0; temp < num; temp++)
	{
        getline(input, name);
        input >> rate;
        input.ignore();
        ++mapReview[name][rate];

    }
	map<int, int>::iterator tempItr1;
	map<string, map<int, int>>::iterator tempItr2;
    for (tempItr2 = mapReview.begin(); tempItr2 != mapReview.end(); tempItr2++)
    {

      cout << "\n " << tempItr2->first << endl;

        for (tempItr1 = tempItr2->second.begin(); tempItr1 != tempItr2->second.end(); tempItr1++)
        {
			cout <<  "\n" <<  tempItr1->first << endl;
        }
    }
	system("pause");
    return(0);
}
#include "Map.h"
struct Rating
{
    int number;
    int totalRating;
};
map<string, Rating> reviewMap;

map.txt:

 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

Output:

 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

 

0 0

Discussions

Post the discussion to improve the above solution.