/* The program prompts user, a 'training data' file that contains
   sample set of classified films. For each film, data has percentage
   of hits of the director, percentage of hits of leading members of the cast,
   and the code 0 (flop) or 1 (hit). Program then reads, atmost MAX_NO_FILMS,
   all this data and stores in array of structures. Program then prompts the
   user, till EOF is typed, percentage of hits of director and that of
   leading cast; it then finds nearest neighbor distance, which is a squared
   distance to all pre-classified data points. Minimum distance is used
   to determine whether the queried film is going to be hit or flop.
*/
   

#include <stdio.h>
#include <float.h>
#include <stdlib.h>

#define MAX_NO_FILMS 100 /* max number of films - can be changed depending on available data */
#define HIT 1

main()
{
int numb_films = 0;
int i;
int nearest;
int eof_flag;
double sdlist;
double min_dist;
FILE *fp;
char file_name[80];

struct hit_flop{
	double dir_hits;
	double cast_hits;
	int code;
};

struct hit_flop film[MAX_NO_FILMS];

double unknown_dir_hits;
double unknown_cast_hits;

printf("Enter filename: ");
scanf("%s",file_name);
fp=fopen(file_name,"r");

/* Read data into an array of structures */

while (numb_films < MAX_NO_FILMS && fscanf(fp,"%lf",&film[numb_films].dir_hits) != EOF){
fscanf(fp,"%lf",&film[numb_films].cast_hits);
fscanf(fp,"%d",&film[numb_films++].code);
}

/* Input value for unknown films and classify each using the nearest neighbor algorithm */
  
do {
  printf("Enter the percent of director hits: ");
  if ((eof_flag = scanf("%lf",&unknown_dir_hits)) != EOF)
{
printf("Enter percent of cast hits: ");
scanf("%lf",&unknown_cast_hits);
min_dist=1000.0;

for (i=0; i<numb_films;i++)
{
sdlist=(unknown_dir_hits - film[i].dir_hits)
*(unknown_dir_hits - film[i].dir_hits)
+ (unknown_cast_hits - film[i].cast_hits)*
(unknown_cast_hits - film[i].cast_hits);

if (sdlist<min_dist) {min_dist=sdlist;
                      nearest=i;
}
}

printf("The films will be a ");

if (film[nearest].code == HIT)
   printf("Hit! :) \n \n");
else
   printf("Flop :( \n \n");
}
}
while (eof_flag!=EOF);
}


