Guest User

Untitled

a guest
Jan 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.26 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #define INPUT_FILE "input.txt"
  7.  
  8. /* The maximum name length and number of people and movies */
  9. #define MAX_NAME_LENGTH 200
  10. #define MAX_PEOPLE 700
  11. #define MAX_MOVIES 200
  12.  
  13. /* The only global variables permitted in this project are below */
  14. char movieData[MAX_MOVIES][MAX_NAME_LENGTH] = {0};
  15. char personData[MAX_PEOPLE][MAX_NAME_LENGTH] = {0};
  16. int  ratingsMatrix[MAX_PEOPLE][MAX_MOVIES] = {0};
  17. int  numberOfMovies = 0;
  18. int  numberOfPeople = 0;
  19.  
  20. void GetData (void);
  21. int  GetPersonId (char *person) ;
  22. int  GetMovieId (char *movie) ;
  23. void AlgorithmOne (int personId) ;
  24. void AlgorithmTwo (int personId);
  25.  
  26. int main(void)
  27. {
  28.     int  personId;
  29.     char person[MAX_NAME_LENGTH];
  30.  
  31.     GetData();
  32.  
  33.     printf("\nENGGEN 131 2011, Project 2\nCalculate Recommendations for: " );
  34.     scanf("%s", person);
  35.  
  36.     personId = GetPersonId(person);
  37.    
  38.     AlgorithmOne(personId);
  39.  
  40.     AlgorithmTwo(personId);
  41.  
  42.     return 0;
  43. }
  44.  
  45. void GetData (void)
  46. {
  47.     char person[MAX_NAME_LENGTH];
  48.     char movie[MAX_NAME_LENGTH];
  49.     int  personId;
  50.     int  movieId;
  51.     int  rating;
  52.  
  53.     FILE *fp;
  54.     fp = fopen(INPUT_FILE, "r");
  55.  
  56.     if (fp == NULL){
  57.         printf("Sorry, the file could not be opened");
  58.     } else {
  59.         while (fscanf(fp, "%s", person) != EOF){
  60.  
  61.             personId = GetPersonId(person);
  62.             fscanf(fp, "%s", movie);
  63.  
  64.             movieId = GetMovieId(movie);
  65.             fscanf(fp, "%d", &rating);
  66.  
  67.             ratingsMatrix[personId][movieId] = rating;
  68.         }
  69.     }
  70. }
  71.  
  72. int GetPersonId(char *person)
  73. {
  74.     int i;
  75.     int duplicate = 0;
  76.     int test = 0;
  77.  
  78.     for(i=0; i < numberOfPeople; i++){
  79.  
  80.         duplicate = strcmp(personData[i], person);
  81.  
  82.         if (duplicate == 0){
  83.             test = 1;
  84.             return (i);
  85.             break;
  86.         } else
  87.             test = 0;  
  88.     }
  89.     if (!test){
  90.         strcpy(personData[numberOfPeople], person);
  91.         numberOfPeople++;
  92.     }
  93.  
  94.     return (numberOfPeople - 1);
  95.  
  96. }
  97.  
  98. int GetMovieId(char *movie)
  99. {
  100.     int i;
  101.     int duplicate = 0;
  102.     int test = 0;
  103.  
  104.     for(i=0; i < numberOfMovies; i++){
  105.  
  106.         duplicate = strcmp(movieData[i], movie);
  107.  
  108.         if (duplicate == 0){
  109.             test = 1;
  110.             return (i);
  111.             break;
  112.         } else
  113.             test = 0;  
  114.     }
  115.     if (!test){
  116.         strcpy(movieData[numberOfMovies], movie);
  117.         numberOfMovies = numberOfMovies + 1;
  118.     }
  119.  
  120.     return (numberOfMovies - 1);
  121. }
  122.  
  123. void AlgorithmOne (int personId)
  124. {
  125.     double avg[MAX_MOVIES] = {0};
  126.     double seen[MAX_MOVIES] = {0};
  127.     double sum[MAX_MOVIES]  = {0};
  128.     double maximumAverage =  0;
  129.     char *recommendation = {0};
  130.     int i ;
  131.     int j ;
  132.  
  133.     for (i=0; i <numberOfMovies; i++){
  134.         seen[i] = 0;
  135.         for (j=0;j<numberOfPeople; j++){
  136.  
  137.             if(j==personId && ratingsMatrix[j][i]!=0){
  138.                 sum[i]=0;
  139.                 seen[i]= MAX_PEOPLE;
  140.                 break;
  141.             }
  142.  
  143.             else if (ratingsMatrix[j][i] !=0 && j != personId){
  144.                 sum[i] = sum[i] + (ratingsMatrix[j][i]);
  145.                 seen[i] = seen[i]+ 1;
  146.             }
  147.         }
  148.         avg[i] = sum[i]/seen[i];
  149.     }  
  150.  
  151.     for (i=0; i <numberOfMovies; i++){        
  152.         if (avg[i] > maximumAverage){
  153.             maximumAverage = avg[i];
  154.             recommendation = movieData[i];
  155.         }
  156.     }
  157.  
  158.     printf("\nAlgorithm 1:\nRecommended movie: %s\n",  recommendation);
  159.     printf("Average rating: %f \n\n" , maximumAverage);
  160.  
  161. }
  162.  
  163. void AlgorithmTwo (int personId)
  164. {
  165.     int similarity[MAX_PEOPLE] = {0};
  166.     double maximumSimilarity = 0;
  167.     int personPosition = 0;
  168.     int most_similar[MAX_PEOPLE] = {0};
  169.     int highestRankedMovie = 0;
  170.     int moviePosition = 0;
  171.     int i;
  172.     int j;
  173.  
  174.     for(i=0; i < numberOfPeople; i++){
  175.         for (j =0; j < numberOfMovies; j++){
  176.  
  177.             if (i == personId){
  178.                 similarity[i] = 0;
  179.             }
  180.             else{
  181.                 similarity[i] = (ratingsMatrix[personId][j]*ratingsMatrix[i][j]) + similarity[i];
  182.             }
  183.         }
  184.     }
  185.  
  186.     for (i=0; i <numberOfPeople; i++){        
  187.         if (similarity[i] > maximumSimilarity){
  188.             maximumSimilarity = similarity[i];
  189.             personPosition = i;  
  190.         }
  191.     }
  192.  
  193.     for(i=0; i <numberOfMovies; i++){                                          
  194.  
  195.         if (ratingsMatrix[personId][i] != 0){
  196.             most_similar[i] = 0;
  197.         }
  198.         else{
  199.             most_similar[i] = ratingsMatrix[personPosition][i];
  200.         }
  201.     }
  202.  
  203.     for (i=0; i <numberOfMovies; i++){        
  204.         if (most_similar[i] > highestRankedMovie){
  205.             highestRankedMovie = most_similar[i];
  206.             moviePosition = i;
  207.         }
  208.     }
  209.  
  210.     printf("Algorithm 2:\nRecommended movie: %s\n",movieData[moviePosition]);
  211.     printf("Similarity rating = ""%f with ""%s\n",maximumSimilarity, personData[personPosition]);
  212.  
  213. }
Add Comment
Please, Sign In to add comment