Advertisement
Guest User

ihateeverything.c

a guest
Dec 6th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. /*Programmer: Nihcolas Canning
  2. Program: Assignment 3
  3. Course: IPC144A
  4. Instructor: Arta Kogan
  5. Date: December 6, 2013*/
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #define START 1921
  11. #define SIZE 18
  12. #define DIVIDE 5
  13.  
  14. //function prototypes and structures
  15.  
  16. struct NameRecord
  17. {
  18. char name[31];
  19. int year;
  20. int frequency;
  21. };
  22.  
  23. void allCaps(char s[]);
  24.  
  25. int getRawData(FILE* fp,struct NameRecord records[],int currSize);
  26.  
  27. void setYearTotals(struct NameRecord records[], int size, int yearRangeTotal[]);
  28.  
  29. void setNameYearTotals(char theName[],struct NameRecord records[], int size, int nameTotal[]);
  30.  
  31. void getPerHundredThousand(int nameTotal[], int yearRangeTotal[], double perHundredThousand[]);
  32.  
  33. void printData(double perHundredThousand[]);
  34.  
  35. void graphPerHundredThousand(double perHundredThousand[]);
  36.  
  37. int main()
  38. {
  39. int valid=1;
  40. char input=' ';
  41. char theName[100]={' '};
  42. FILE *fp;
  43. struct NameRecord records[150000];
  44. int currSize;
  45. int yearRangeTotal[SIZE]={0};
  46. int nameTotal[SIZE]={0};
  47. double perHundredThousand[SIZE]={0};
  48.  
  49. printf("Welcome to the Name Popularity Checker\n======================================\n");
  50. while(valid)
  51. {
  52. currSize=0;
  53. printf("Please enter a name:");
  54. scanf("%s",theName);
  55. allCaps(theName);
  56. printf("Number of Babies named %s per 100,000 births\n============================================\n", theName);
  57. fp=fopen("femalebabynames.txt", "r");
  58. if(fp)
  59. {
  60. currSize=getRawData(fp,records,currSize);
  61. fclose(fp);
  62. fp=fopen("malebabynames.txt", "r");
  63. if(fp)
  64. {
  65. currSize=getRawData(fp,records,currSize);
  66. fclose(fp);
  67. setYearTotals(records,currSize,yearRangeTotal);
  68. setNameYearTotals(theName,records,currSize,nameTotal);
  69. getPerHundredThousand(nameTotal,yearRangeTotal,perHundredThousand);
  70. printData(perHundredThousand);
  71. graphPerHundredThousand(perHundredThousand);
  72. printf("Do you wish to check another name (Y/N)?");
  73. while((scanf("%c", &input))!='\n')
  74. {
  75. if(input=='n')
  76. {
  77. valid=0;
  78. }
  79. }
  80. }
  81. else
  82. {
  83. printf("Error opening file malebabynames.csv.\n Program shutting Down\n");
  84. valid=0;
  85. }
  86.  
  87. }
  88. else
  89. {
  90. printf("Error opening file femalebabynames.csv.\n Program shutting Down\n");
  91. valid=0;
  92. }
  93. }
  94. return 0;
  95. }
  96.  
  97. //function definitions
  98.  
  99. void graphPerHundredThousand(double perHundredThousand[])
  100. {
  101. printf("\n\n Graph\n=====================================================\n\n");
  102. int a;
  103. int b=START;
  104. int c;
  105. int stars=0;
  106. double lowest=0;
  107. int remain=0;
  108. for(a=0;a<SIZE;a++)
  109. {
  110. if(perHundredThousand[a]>=0.01)
  111. {
  112. if(a==0 || lowest<perHundredThousand[a])
  113. {
  114. lowest=perHundredThousand[a];
  115. }
  116. }
  117. }
  118. for(a=0;a<SIZE;a++)
  119. {
  120. remain=(int)perHundredThousand[a]%(int)lowest;
  121. stars=0;
  122. if(remain!=0)
  123. {
  124. stars=1;
  125. }
  126. stars+=perHundredThousand[a]/lowest;
  127. printf("%d - %d |",b,b+=4);
  128. for(c=0;c<stars;c++)
  129. {
  130. printf("*");
  131. }
  132. printf("\n");
  133. }
  134. }
  135.  
  136. void printData(double perHundredThousand[])
  137. {
  138. int a;
  139. int b=0;
  140. for(a=START;a<2011;a++)
  141. {
  142. printf("%d - %d: %.2lf\n",a, a+=4, perHundredThousand[b]);
  143. b++;
  144. }
  145. }
  146.  
  147. void getPerHundredThousand(int nameTotal[], int yearRangeTotal[], double perHundredThousand[])
  148. {
  149. int a;
  150. for(a=0;a<SIZE;a++)
  151. {
  152. perHundredThousand[a]=10000*nameTotal[a]/yearRangeTotal[a];
  153. }
  154. }
  155.  
  156. void setNameYearTotals(char theName[],struct NameRecord records[], int size, int nameTotal[])
  157. {
  158. int a;
  159. int b;
  160. int error=0;
  161. int check=0;
  162. for(a=0;a<size;a++)
  163. {
  164. if(strlen(theName)==strlen(records[a].name))
  165. {
  166. for(b=0;b<strlen(theName);b++)
  167. {
  168. if(theName[b]!=records[a].name[b])
  169. {
  170. error=1;
  171. }
  172. }
  173. if(error==0)
  174. {
  175. check=records[a].year;
  176. check-=START;
  177. check/=DIVIDE;
  178. nameTotal[check]+=records[a].frequency;
  179. }
  180. error=0;
  181. }
  182. }
  183. }
  184.  
  185. void setYearTotals(struct NameRecord records[], int size, int yearRangeTotal[])
  186. {
  187. int a;
  188. int check=START;
  189. int check2;
  190. int valid=1;
  191. for(a=0;a<size;a++)
  192. {
  193. while(valid)
  194. {
  195. if(check==records[a].year)
  196. {
  197. check2=check;
  198. check2-=START;
  199. check2/=DIVIDE;
  200. yearRangeTotal[check2]+=records[a].frequency;
  201. valid=0;
  202. }
  203. else
  204. {
  205. check++;
  206. }
  207. }
  208. valid=1;
  209. check=START;
  210. }
  211. }
  212.  
  213. int getRawData(FILE* fp,struct NameRecord records[],int currSize)
  214. {
  215.  
  216.  
  217. while((fscanf(fp, "%d,%[^,],%d\n", &records[currSize].year, records[currSize].name, &records[currSize].frequency))!=EOF)
  218. {
  219. currSize++;
  220. }
  221. return currSize;
  222. }
  223.  
  224. void allCaps(char s[])
  225. {
  226. int num=strlen(s);
  227. int a;
  228. for(a=0;a<num;a++)
  229. {
  230. s[a]=toupper(s[a]);
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement