Guest User

Untitled

a guest
Jan 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdlib.h>
  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 movies[MAX_MOVIES][MAX_NAME_LENGTH] = {0};
  15. char people[MAX_PEOPLE][MAX_NAME_LENGTH] = {0};
  16. int ratings[MAX_PEOPLE][MAX_MOVIES] = {0};
  17. int numberOfMovies = 0;
  18. int numberOfPeople = 0;
  19.  
  20. void Data(void);
  21. void Algorithm1(char Name[MAX_NAME_LENGTH]);
  22. int main(void) {
  23.  
  24. char Name[MAX_NAME_LENGTH];
  25.  
  26.  
  27.  
  28. printf("Algorithm 1\n");
  29.  
  30.  
  31. printf("Calculate recommendations for:");
  32.  
  33. scanf("%s",Name);
  34.  
  35. Algorithm1(Name);
  36.  
  37.  
  38. return 0;
  39. }
  40. void Data(void)
  41. {
  42. char person[MAX_NAME_LENGTH];
  43. char movie_name[MAX_NAME_LENGTH];
  44. int rating;
  45. FILE*fp;
  46.  
  47. int GETPERSON(char *person);
  48. int count=0,i=0,repeat,j=0;
  49.  
  50. fp=fopen(INPUT_FILE,"r");
  51.  
  52. if(fp==0){
  53. printf("Can not find the file\n");
  54. exit(EXIT_FAILURE);
  55. }
  56. while (fscanf(fp,"%s %s %d",person,movie_name,&rating)!=EOF){
  57. repeat=0;
  58.  
  59. for (i=0;i<numberOfPeople;i++){
  60. if(strcmp(person,people[i])==0){
  61. repeat=1;
  62. break;
  63. }
  64. }
  65. if(repeat!=1){
  66. strcpy(people[numberOfPeople],person);
  67. numberOfPeople++;
  68. }
  69. repeat=0;
  70. for (j=0;j<numberOfMovies;j++){
  71. if(strcmp(movie_name,movies[j])==0){
  72. repeat=1;
  73. break;
  74. }
  75. }
  76. if(repeat!=1){
  77. strcpy(movies[numberOfMovies],movie_name);
  78. numberOfMovies++;
  79. }
  80. ratings[i][j]=rating;
  81. }
  82. }
  83.  
  84. void Algorithm1(char Name[MAX_NAME_LENGTH])
  85. {
  86. int i,j=0,UPI,maxIndex=0;
  87. double average, MaxAverage=0,count=0,sum=0;
  88.  
  89. UPI=Name[MAX_NAME_LENGTH];
  90.  
  91. for(i=0;i<numberOfPeople;i++){
  92. if(ratings[UPI][j]==0){
  93. for(j=0;j<numberOfMovies;j++){
  94. if ((ratings[i][j]!=0)){
  95. sum+=ratings[i][j];
  96. count++;
  97. }
  98. }
  99.  
  100. average=sum/count;
  101. }
  102.  
  103.  
  104. if(MaxAverage<average){
  105. MaxAverage=average;
  106. maxIndex=j;
  107. }
  108.  
  109. }
  110.  
  111.  
  112. printf("Recommended movie: %s\n",movies);
  113.  
  114. printf("Average rating:%f\n",MaxAverage);
  115. }
Add Comment
Please, Sign In to add comment