Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "q2.h"
  5.  
  6. int main()
  7. {
  8.  
  9. FILE *fp;
  10.  
  11. fp = fopen("test.txt", "r");
  12.  
  13. if (fp == NULL)
  14. {
  15.  
  16. printf("FILE COULD NOT BE OPENED\n");
  17. return 0;
  18. }
  19.  
  20. char *string = calloc(500, 1);
  21.  
  22. WordStruct structArray[5000];
  23. int arraySize = 0;
  24.  
  25. while (fscanf(fp, "%s", string) != EOF)
  26. {
  27.  
  28. strcpy(structArray[arraySize].word, string);
  29.  
  30. // printf("%s ", string);
  31. arraySize++;
  32. }
  33.  
  34. int i = 0;
  35. int j = 0;
  36.  
  37. for (i = 0; i < arraySize; i++)
  38. {
  39. for (j = 0; j < arraySize; j++)
  40. {
  41. if (strcmp(structArray[i].word, structArray[j].word) < 0)
  42. {
  43. WordStruct temp = structArray[i];
  44. structArray[i] = structArray[j];
  45. structArray[j] = temp;
  46. }
  47. }
  48. }
  49.  
  50. WordStruct newArray[3000];
  51.  
  52. float wordCount = 1;
  53. float highestFrequency = 0;
  54. int freqIndex = 0;
  55. j = 0;
  56.  
  57. for (i = 0; i < arraySize - 1; i++)
  58. {
  59. while ((strcmp(structArray[i].word, structArray[i + 1].word) == 0))
  60. {
  61. wordCount++;
  62. i++;
  63. }
  64. structArray[i].frequency = wordCount / 10; //change
  65. if (structArray[i].frequency > highestFrequency)
  66. {
  67. highestFrequency = structArray[i].frequency;
  68. freqIndex = j;
  69. }
  70. newArray[j] = structArray[i];
  71. wordCount = 1;
  72. j++;
  73. }
  74.  
  75. q2(newArray, j);
  76.  
  77. fclose(fp);
  78.  
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement