Like Tree1Likes
  • 1 Post By itsme86

need help finding the first and second most frequent letter in an array

This is a discussion on need help finding the first and second most frequent letter in an array within the C Programming forums, part of the General Programming Boards category; so far the programming i have finds the percentage at which the letters occur however i need help finding the ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    need help finding the first and second most frequent letter in an array

    so far the programming i have finds the percentage at which the letters occur however i need help finding the first and second most frequent that occur the code was from a book with a little modification i know i need to use a bubble sort but im having trouble understanding them

    heres what i have:
    Code:
    #include <stdio.h>
    #include <string.h>
    
        void getthestring(char string[]);
        void countcharacters(char string[],int array[],int length);
        void fixcase(char string[]);
        void initialize(int array[], int numberelements);
    
        main()
        {
            char
                mystring[500];
            int
                countarray[26],x[26],i,j,k,length,max,sec;
            initialize(countarray,26);
            getthestring(mystring);
            length = strlen(mystring);
            printf("\nyou entered %d characters \n\n",length);
            fixcase(mystring);
            countcharacters(mystring,countarray,length);
    
            for(i=0;i<26;i=i+1)
            {
                printf("%c   %5.2f \% ",i+97,((countarray[i]*100.0)/length));
                if ((i+1)%5 == 0)
                    printf("\n");
            }      
            printf("\n");
        getchar();
        getchar();
        }
    
        void getthestring(char string[])
        {
        printf("please enter a sentence,up to 500 characters long\n\n");
        gets(string);
    }
    void countcharacters(char string[],int array[],int length)
    {
        int
            i,j;
        for(i=0;i<length;i=i+1)
            for(j=97;j<123;j=j+1)
                if(string[i] == j)
                {
                    array[j-97] = array[j-97] + 1;
                    j=123;
                }
    }
    
    void fixcase(char string[])
    {
        int
            i=0;
        while(string[i] != '\0')
        {
            if((string[i] >= 65) && (string[i] <=90))
                string[i] = string[i] + 32;
            i = i + 1;
        }
    }
    void initialize(int array[], int numberelements)
    {
        int
            i;
        for(i=0;i<numberelements;i = i + 1)
            array[i] = 0;    
    }
    i have 3 variables unused at all because my profesor said to use them to bubble sort for the most frequent letters in the array and to set x[26] = countarray[26] max is the first letter sec is the 2nd most occuring the he also said use for(i=0;i<26;i++) and if(countarray[i]==x) something similar to that,max=x[0] and max[1] and prinf to show the results
    where do i place this stuff though?
    Last edited by fefnirfang; 11-18-2011 at 04:46 PM. Reason: left out information

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Well, one way would be to sort and then choose the 2 at the end. For the data you're working with, an insertion sort would probably suit you well:
    Code:
    for(i = 0;i < length - 1;++i)
    {
      int next_value = countarray[i + 1];
      int insertion_point = 0;
      // Figure out where to insert the value
      while(insertion_point <= i)
      {
        if(next_value < countarray[insertion_point])
          break;
        insertion_point++;
      }
      
      // Make room for the value
      for(j = i;j >= insertion_point;--j)
        countarray[j + 1] = countarray[j];
      countarray[insertion_point] = next_value;
    }
    ... or something like that.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    where would that go in the program and what variables are the ones that would display the 2 most frequent letters

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Well, after you count the characters and fill the countarray... Where did you steal the code from that you posted?
    rags_to_riches likes this.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    its from my class book my profesor told us to mod like 3 parts then said figure out the rest on our own this is my first programming class i went to my schools programming lab and the person who runs it knows nothing of programming

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Quote Originally Posted by fefnirfang View Post
    i have 3 variables unused at all because my profesor said to use them to bubble sort for the most frequent letters in the array and to set x[26] = countarray[26] max is the first letter sec is the 2nd most occuring the he also said use for(i=0;i<26;i++) and if(countarray[i]==x) something similar to that,max=x[0] and max[1] and prinf to show the results
    where do i place this stuff though?
    I'm confused that you're supposed to sort the array. countarray is 26 elements, representing 26 letters of the alphabet. Each time a letter is seen the corresponding array entry is incremented.

    So suppose you gave the input "aaabbbbbbbcc", the array would contain
    countarray[0] = 3
    countarray[1] = 7
    countarray[2] = 2
    countarray[others] = 0

    Now suppose you sorted it to have largest number first, you'd have
    countarray[0] = 7
    countarray[1] = 3
    countarray[2] = 2
    countarray[others] = 0

    So you know that the most amount of recurring letters was 7, but you don't know what that letter was any more! I suppose you could initialise another array with the letters, or with 0-25, then sort it alongside the count array. I dunno, seems really over complicated!

    Code:
    ......
            fixcase(mystring);
            countcharacters(mystring,countarray,length);
    
            // I would insert your code here 
            /*
     
            for(i=0;i<26;i=i+1)
            {
                printf("%c   %5.2f \% ",i+97,((countarray[i]*100.0)/length));
                if ((i+1)%5 == 0)
                    printf("\n");
            }      
            printf("\n");*/
        getchar();
        getchar();
        }
    .....
    I'd start by writing a function getmax() to get the most used letter. Remember the letter is the array index not the value. Here's an outline:

    Code:
    int getmax(int arr[], int numberelements)
    {
       int max = 0;
       int maxindex; 
       for all the elements in arr
           if the value of the arr element > max
               max = value
               maxindex = index of arr element
    
       return maxindex;       
          
    }
    Maxindex will be a number between 0 and 25. Of course, C doesn't encode a-z as 0-25, so you'll need to convert the index to a char. You should be able to figure out how by looking at the rest of the code, and checking Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    To get the second largest, I'd probably set the largest value to 0 then call getmax again. I'd either preserve the previous value of max in the array and restore it, or make a copy of the whole array for modification.

    Actually thinking about it it might be better to just sort it. To do that, I'd initialise "x" with a-z, then sort countarray. In bubble sort there's just a swap of two elements. When you swap the countarray elements swap the x elements too. I think that might be what your professor means, kinda guessing though

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Holy shades of Rube Goldberg, Batman... (Example HERE)

    Stop trying to write code... turn off your computer and start thinking about the problem itself...

    Exactly what is being asked of you?
    What do you already know? (example: You know how many letters are in the alphabet)

    Understand the problem down to little tiny blobs, every fact is important.

    Now... once you understand the problem, begin working out a step by step plan to solve the problem...
    What has to happen first?
    Then what?

    Armed with an actual plan, you may now turn on your computer and begin roughing out the code based on your plan.
    Work in sections, get one blob working before you move on to the next, compile and test often.

    Then once the program is complete, test the code, fix any problems, simplify what you can... make sure it's working as expected.

    Voila ... one working program.

    (FWIW... an experienced programmer can probably do your task in less than 50 lines.)
    Last edited by CommonTater; 11-18-2011 at 07:10 PM. Reason: typo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-17-2011, 08:40 AM
  2. Replies: 16
    Last Post: 01-28-2010, 02:50 PM
  3. Pet Peeve: Frequent Formatting
    By lightatdawn in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 05-28-2002, 11:48 PM
  4. Finding Letter Grade and Marital Status
    By ProgrammingDlux in forum C++ Programming
    Replies: 7
    Last Post: 03-27-2002, 11:05 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21