Thread: Program Help

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    1

    Program Help

    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 occurances 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 = 0;
    int fissureCount = 0;
    int wordCount = 0;
    int theSame = 0;
    int tallyCount[SIZE];
    int count = 0;
     
    char file_name[50];
    char accum[SIZE];
    char array[SIZE];
    char *p;
    char *line = NULL; 
    char *fissure;
    char words[10000][CHAR];
    FILE *file;
    
    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;
    }
    
      //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, "r"); 
        
        while(file==NULL){      
        	     printf("\n\nError: can't open file.\n");
        		 printf("\nPlease verify your file path and name.\n");
        printf("\nWhen you enter the file path and name it 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", "r"); 
          
       while(!feof(file)){
           fscanf(file, "%s", &array[i]);
                         i++;  //this part should iterate through the array to add 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]);
            }
    	
     	  char 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;
    }
    Thanks for any help or pointers,

    Dane771

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    For starters:

    Code:
       while(!feof(file)){
           fscanf(file, "%s", &array[i]);
    Here you are trying to put a string into a single character.
    Code:
            i++;
            sprintf(array, "%s\n ");
    Something is missing from this sprintf line! Also, you are overwriting whatever it is you just meant to do.
    Code:
           char *p = strchr(array,'\n'); 
           if ( p ) *p = '\0';
           }
    Is incorrect. It should look like this:
    Code:
    if ((p=strchr(array,'\n'))!=NULL) p[0]='\0';
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by dane771 View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread