In C++ language: You have collected reviews from four movie reviewers where the
ID: 3686032 • Letter: I
Question
In C++ language:
You have collected reviews from four movie reviewers where the reviewers are numbered 0-3. Each reviewer has rated six movies where the movies are numbered 100-105. The ratings range from 1 (terrible) to 5 (excellent). The reviews are shown in the following table:
100 101 102 103 104 105
0 3 1 5 2 1 5
1 4 2 1 4 2 4
2 3 1 2 4 4 1
3 5 1 4 2 4 2
Write a program that stores this data using a 2D array. Based on this information your program should allow the user to enter ratings for any three movies. The program should then find the reviewer whose ratings most closely match the ratings input by the user. It should then predict the user’s interest in the other movies by outputting the ratings by the reviewer for the movies that were not rated by the user. Use the Cartesian distance as the metric to determine how close the reviewer’s movie ratings are to the ratings input by the user. This technique is a simple version of the nearest neighbor classification algorithm. For example, if the user inputs a rating of 5 for movie 102, 2 for movie 104, and 5 for movie 105, then the closest match is reviewer 0 with a distance of sqrt((5-5)^2 + (2-1)^2 + (5-5)^2) = 1. The program would then predict a rating of 3 for movie 100, a rating of 1 for movie 101, and a rating of 2 for movie 103. Note: To store the data in a 2D array the movie ID’s must be mapped to 0-5. Here is the sample runs:
Enter a movie to rate (100-105). Enter 0 to exit and get recommendations. 101
Enter rating (1-5) for this movie. 4
Enter a movie to rate (100-105). Enter 0 to exit and get recommendations.
105 Enter rating (1-5) for this movie. 3
Enter a movie to rate (100-105). Enter 0 to exit and get recommendations. 0
The closest reviewer is number 1
Predictions for movies you have not yet seen:
Movie 100 : Predicted Rating = 4
Movie 102 : Predicted Rating = 1
Movie 103 : Predicted Rating = 4
Movie 104 : Predicted Rating = 2
Explanation / Answer
#include < iostream >
#include < cmath >
using namespace std ;
const int count_movie =6 ;
const int count_reviewer =4 ;
double getReviwrDistance ( int * reviws ,int * client )
{
int total =0 ;
for ( int i = 0 ;i < count_movie ;i++ )
{
if( client[i] != 0 )
{
// skip the data that we were entered
// cout<<client[i] <<" * "<<reviws[i] <<endl ;
int n =client[i] -reviws[i] ;
total + =( n * n ) ;
}
}
return sqrt ( total ) ;
}
int main( )
{
int reviws[count_reviewer][count_movie] =
{
{ 3, 1, 5, 2, 1, 5 },
{ 4, 2, 1, 4, 2, 4 },
{ 3, 1, 2, 4, 4, 1 },
{ 5, 1, 4, 2, 4, 2 }
} ;
// For exmp, if the client i/p's a rating of 5 for movie 102 ,2 for movie 104 , & 5 for movie 105
int exmp[count_movie] ={ 0, 0, 5, 0, 2, 5 } ;
for ( int i = 0 ;i < count_reviewer ;i++ )
{
cout <<i <<' ' <<getReviwrDistance ( reviws[i] ,exmp ) <<endl ;
}
return 0 ;
}