Originally Posted by
dane771
I have worked on the program below for
countless hours. It is suppose to take the words from a file and count
the number of frequencies a word occurs, but I am unable to get the
program to iterate through the array. Any ideas?
Code:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*Heading - word.c This reads in data from a file entered by the user. *
* Then it selects the ten of the highest number of *
* occurrances of words in the file. *
* *
* *
* *
*Author - Mark R. Wilson *
* *
*Issues - The program only calculates the last word in the array. *
* *
*References - Workout C *
* The C Programming Language *
* http://cosmo.mc.edu/moodle/file.php/185/frequency.cpp *
* http://www.daniweb.com/forums/thread91918.html *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#include <stdio.h>
#include <string.h>
#define SIZE 100000
#define CHAR 31
#define NONALPHA "1 2 3 4 5 6 7 8 9 0 \v\f\n\t\r+=-*/\\,.;:'#~?<>|{}[]`!\"£$%^&()"
int frequency(int words_freq[SIZE][CHAR], int location);
int main (void) {
int i, fissureCount, wordCount, theSame, tallyCount[SIZE], count;
char file_name[50], accum[SIZE], array[SIZE], *p; *line NULL, *fissure;
char words[10000][CHAR];
FILE *file;
i = fissureCount = wordCount = theSame = count = 0;
for(i = 0; i < SIZE; i++)
tallyCount[i] = 0;
*p = *line = *fissure = NULL;
//Get the input for the file name and location
printf("Enter the file name to read.\n");
printf("It should look like C:\\location\\text.txt\n");
printf("Please enter no more than 50 characters.\n");
scanf("%50s", file_name);
//Opens the file name entered by the user.
file = fopen(file_name, "rt");
while(file == NULL) {
printf("\n\nError: can't open file.\n");
printf("\nPlease verify your file path and name.\n");
printf("\nYour path and filename should look like:\n");
printf("C:\\location\\text.txt\n");
printf("Please enter no more than 50 characters.\n");
scanf("%50s",file_name);
file = fopen(file_name, "r");
}
if (file !=NULL)
printf("\nYour file was opened succesfully.\n\n\n");
i = 0;
//The line below was used for testing
//file = fopen("C:\\Temp\\text-testfiles\\ex5.txt", "rt");
while(!feof(file)) {
fscanf(file, "%s", &array[i]);
i++; //should iterate through array adding words to the
//array, but it is not?
sprintf(array, "%s\n ");
//////////Used for testing\\\\\\\\\\\\\\\\\
//printf(" %s", array);
char *p = strchr(array,'\n');
if ( p )
*p = '\0';
}
fclose(file);
for (i=0; i<SIZE; i++) array[i] = tolower (array[i]);
fissure = strtok(array,NONALPHA);
for ( i = 0; i < SIZE; i++)
fissure = array;
while (fissure != NULL) {
theSame = 0;
count = 0;
while (count < wordCount && !theSame)
if (strcmp (fissure, words[count])==0) {
tallyCount[count]++;
theSame = 0;
}
else {
count++;
}
if (!theSame) {
strcpy( words[wordCount] , fissure);
tallyCount[wordCount]= 1;
wordCount++;
}
fissureCount++;
fissure = strtok(NULL,NONALPHA);
}
printf ("\n\n WORDS & FREQUENCY \n");
for (count = 0; count<wordCount; count++)
printf (" %-25s %d \n", words[count],tallyCount[count]);
z; //This variable holds the any key entered
printf("\n\n To exit, please press a key and then press enter.");
scanf("%c%c",&z,&z); //The %c is to scan in a single character
return 0;
} //end of main()
int frequency(int words_freq[SIZE][CHAR], int location) {
int temp1,temp0;
int i=location, j=0, k=0;
while(((i > 0) && (words_freq[i][1 ]>= words_freq[i-1][1]))) {
temp0=words_freq[i][0];
temp1=words_freq[i][1];
words_freq[i][0] = words_freq[i-1][0];
words_freq[i][1] = words_freq[i-1][1];
words_freq[i-1][0] = temp0;
words_freq[i-1][1]=temp1;
i--;
}
return 0;
} //end of frequency ()
Thanks for any help or pointers,
Well, better formatting is the first step.