Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Programmer: Nihcolas Canning
- Program: Assignment 3
- Course: IPC144A
- Instructor: Arta Kogan
- Date: December 6, 2013*/
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #define START 1921
- #define SIZE 18
- #define DIVIDE 5
- //function prototypes and structures
- struct NameRecord
- {
- char name[31];
- int year;
- int frequency;
- };
- void allCaps(char s[]);
- int getRawData(FILE* fp,struct NameRecord records[],int currSize);
- void setYearTotals(struct NameRecord records[], int size, int yearRangeTotal[]);
- void setNameYearTotals(char theName[],struct NameRecord records[], int size, int nameTotal[]);
- void getPerHundredThousand(int nameTotal[], int yearRangeTotal[], double perHundredThousand[]);
- void printData(double perHundredThousand[]);
- void graphPerHundredThousand(double perHundredThousand[]);
- int main()
- {
- int valid=1;
- char input=' ';
- char theName[100]={' '};
- FILE *fp;
- struct NameRecord records[150000];
- int currSize;
- int yearRangeTotal[SIZE]={0};
- int nameTotal[SIZE]={0};
- double perHundredThousand[SIZE]={0};
- printf("Welcome to the Name Popularity Checker\n======================================\n");
- while(valid)
- {
- currSize=0;
- printf("Please enter a name:");
- scanf("%s",theName);
- allCaps(theName);
- printf("Number of Babies named %s per 100,000 births\n============================================\n", theName);
- fp=fopen("femalebabynames.txt", "r");
- if(fp)
- {
- currSize=getRawData(fp,records,currSize);
- fclose(fp);
- fp=fopen("malebabynames.txt", "r");
- if(fp)
- {
- currSize=getRawData(fp,records,currSize);
- fclose(fp);
- setYearTotals(records,currSize,yearRangeTotal);
- setNameYearTotals(theName,records,currSize,nameTotal);
- getPerHundredThousand(nameTotal,yearRangeTotal,perHundredThousand);
- printData(perHundredThousand);
- graphPerHundredThousand(perHundredThousand);
- printf("Do you wish to check another name (Y/N)?");
- while((scanf("%c", &input))!='\n')
- {
- if(input=='n')
- {
- valid=0;
- }
- }
- }
- else
- {
- printf("Error opening file malebabynames.csv.\n Program shutting Down\n");
- valid=0;
- }
- }
- else
- {
- printf("Error opening file femalebabynames.csv.\n Program shutting Down\n");
- valid=0;
- }
- }
- return 0;
- }
- //function definitions
- void graphPerHundredThousand(double perHundredThousand[])
- {
- printf("\n\n Graph\n=====================================================\n\n");
- int a;
- int b=START;
- int c;
- int stars=0;
- double lowest=0;
- int remain=0;
- for(a=0;a<SIZE;a++)
- {
- if(perHundredThousand[a]>=0.01)
- {
- if(a==0 || lowest<perHundredThousand[a])
- {
- lowest=perHundredThousand[a];
- }
- }
- }
- for(a=0;a<SIZE;a++)
- {
- remain=(int)perHundredThousand[a]%(int)lowest;
- stars=0;
- if(remain!=0)
- {
- stars=1;
- }
- stars+=perHundredThousand[a]/lowest;
- printf("%d - %d |",b,b+=4);
- for(c=0;c<stars;c++)
- {
- printf("*");
- }
- printf("\n");
- }
- }
- void printData(double perHundredThousand[])
- {
- int a;
- int b=0;
- for(a=START;a<2011;a++)
- {
- printf("%d - %d: %.2lf\n",a, a+=4, perHundredThousand[b]);
- b++;
- }
- }
- void getPerHundredThousand(int nameTotal[], int yearRangeTotal[], double perHundredThousand[])
- {
- int a;
- for(a=0;a<SIZE;a++)
- {
- perHundredThousand[a]=10000*nameTotal[a]/yearRangeTotal[a];
- }
- }
- void setNameYearTotals(char theName[],struct NameRecord records[], int size, int nameTotal[])
- {
- int a;
- int b;
- int error=0;
- int check=0;
- for(a=0;a<size;a++)
- {
- if(strlen(theName)==strlen(records[a].name))
- {
- for(b=0;b<strlen(theName);b++)
- {
- if(theName[b]!=records[a].name[b])
- {
- error=1;
- }
- }
- if(error==0)
- {
- check=records[a].year;
- check-=START;
- check/=DIVIDE;
- nameTotal[check]+=records[a].frequency;
- }
- error=0;
- }
- }
- }
- void setYearTotals(struct NameRecord records[], int size, int yearRangeTotal[])
- {
- int a;
- int check=START;
- int check2;
- int valid=1;
- for(a=0;a<size;a++)
- {
- while(valid)
- {
- if(check==records[a].year)
- {
- check2=check;
- check2-=START;
- check2/=DIVIDE;
- yearRangeTotal[check2]+=records[a].frequency;
- valid=0;
- }
- else
- {
- check++;
- }
- }
- valid=1;
- check=START;
- }
- }
- int getRawData(FILE* fp,struct NameRecord records[],int currSize)
- {
- while((fscanf(fp, "%d,%[^,],%d\n", &records[currSize].year, records[currSize].name, &records[currSize].frequency))!=EOF)
- {
- currSize++;
- }
- return currSize;
- }
- void allCaps(char s[])
- {
- int num=strlen(s);
- int a;
- for(a=0;a<num;a++)
- {
- s[a]=toupper(s[a]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement