/*
	Given a year displays the winner of the
	NCAA Division I basketball tournament
	Author Bill Kraynek
	Created November 15, 2006
*/
#include <stdio.h>
#include <string.h>
void copywinner(char*,char*);
int main() {
	char year[5];
	printf("Enter the year ");
	scanf("%s",year);
	int intyear = atoi(year);
	if ( intyear < 1939 || intyear > 2007 ) {
		printf("Year %s is out of range!\n",year);
		return;
	}// end if
	FILE *ncaa;
	ncaa = fopen("ncaa2007.data","r");
	int number = 34;
	char line[81];
	char teams[number][31];
	int wins[number];
	int i;
	for( i = 0; i < number; i++ ) {
		strcpy(teams[i],"");
		wins[i] = 0;
	}// end for
	while( fgets(line,80,ncaa) != NULL ) {
		if( strncmp(line,year,4) == 0 ) { 
			char team[31];
			copywinner(team,line);
			i = 0;			
			while ( strcmp(teams[i],"") !=0 && strcmp(teams[i],team) !=0 ) i++;
			if( strcmp(teams[i],"") == 0 ) {
				strcpy(teams[i],team);
			} // end if
			wins[i] += 1;
		}// end if
	}// end while
	i = 0;
	int maxwins = 0;
	char winner[31];
	while( strcmp(teams[i],"") != 0 ) {
		if ( maxwins < wins[i] ) {
			maxwins = wins[i];
			strcpy(winner,teams[i]);
		}// end if
		i++;
	}// end while
	printf("The winner for the year %s is %s\n",year,winner);
}// end main
void copywinner(char* team,char* line) {
	int start = 5;
	int i = 0;
	while ( line[start] != ':' ) {
		char ch = line[start++];
		team[i++] = ch;
	} // end while
	team[i] = '\0';
}// end copywinner

