Thread: charracter array help

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    44

    charracter array help

    I had this assignment where it searches a text file and prints the min and maximum number of occurrences. what its supposed to do is print the letter or letters associated with min and max and that's where im stumped. Any help would be appreciated.

    Code:
        printf("Number of Letter Occurrences:\n");
        for(i=0;i<26;i++){
            printf("%d\t",input_c[i]);
    }
        printf("\n\n");
        printf("Percentage of Letter Occurrences:\n");
        for(i=0;i<26;i++){
            total=(input_c[i]/26.0)*100;
            array[i]=total;
            printf("%.2f%%\t",total);
        
    }
     printf("\n\n");
     for(i = 0; i < 26; i++)
    { 
        if(input_c[i] > max)
            {
                max = input_c[i];
    }
        else if(input_c[i] < min)
            {
                min = input_c[i];
    }
    }  
     printf("Min number =  %d\n",min);
     printf("Least Frequent Characters: %c\n", minchar);
     printf("Max number =  %d\n",max);
     printf("Most Frequent Characters: %c\n", maxchar);

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Where is the problem? I notice your code is in pieces but you don't seem to have it arranged in that way. Consider this organization

    1. get the input counts into input_c
    2. print the counts as integers
    3. print the counts as doubles (In percentage)
    4. print the min and max counts

    so which part is not working? It's better if you write each piece as independent as possible, then you know exactly what to fix.

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    I can't even read that, as simple as it is, sorry. The braces are not even consistent in their placement yet alone alignment. Choose a style and use it consistently **always**, and "speed reading" code (seeing the overall structure) becomes much easier; you don't have to gaze intently at every single line and wonder where blocks start and end!

    astyle -A1 -p snippet.c
    Code:
    for(i = 0; i < 26; i++)
    {
        printf("%d\t", input_c[i]);
    }
    printf("\n\n");
    printf("Percentage of Letter Occurrences:\n");
    for(i = 0; i < 26; i++)
    {
        total = (input_c[i] / 26.0) * 100;
        array[i] = total;
        printf("%.2f%%\t", total);
    }
    printf("\n\n");
    for(i = 0; i < 26; i++)
    {
        if(input_c[i] > max)
        {
            max = input_c[i];
        }
        else if(input_c[i] < min)
        {
            min = input_c[i];
        }
    }
    or

    astyle -A2 -p snippet.c
    Code:
    printf("Number of Letter Occurrences:\n");
    for(i = 0; i < 26; i++) {
        printf("%d\t", input_c[i]);
    }
    printf("\n\n");
    printf("Percentage of Letter Occurrences:\n");
    for(i = 0; i < 26; i++) {
        total = (input_c[i] / 26.0) * 100;
        array[i] = total;
        printf("%.2f%%\t", total);
    }
    printf("\n\n");
    for(i = 0; i < 26; i++) {
        if(input_c[i] > max) {
            max = input_c[i];
        } else if(input_c[i] < min) {
            min = input_c[i];
        }
    }
    Or whatever you happen to prefer this week. At least make it readable. Please?

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    44
    The problem im having is getting the letters associated with min and max to print. Basically print the letters with the lowest and highest number of occurrences in the file. Ill try and clean up my brackets. Does look a little hard to read


    Code:
    minchar=maxchar=input_c[0];
    min=max=count[(int)minchar];
      for(i=0;input_c[i]!='';i++){
          int ch = input[i];
          if((count[ch]max) && ((char)ch != ' '))
            {
              max = count[ch];
              maxchar = (char)ch;
    }
    }

  5. #5
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    What are max and min intialized to? I'm kind of confused why they're initialized the way they are...

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    44
    theyre supposed to be initialized to the count but that's not what was there. I initialized them both to zero and it still wont print anything. Im pretty sure my functions off.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    One way to tackle this problem is to realize that most character sets* have letters placed in a continuous sequence (ascii chart). Therefore, it is possible to match each letter to an element of an array.

    Code:
    int letters[26] = {0};
    
    letter[0]   // will correspond to 'A'
    letter[1]   // will correspond to 'B'
    letter[2]   // will correspond to 'C'
    // ...
    letter[25]  // will correspond to 'Z'
    If you use this approach, you would have to find a relationship between each letter value and the corresponding element of your array. Using this relationship, you can track the number of occurrences of all letters.

    Code:
    loop:
    
      input = /* read character from file */
    
      index = /* simple formula to convert the value of "input"
                 into the corresponding element number */
    
      letters[index]++;  /* increment the value at the index which
                           corresponds to the current letter */
    Then it's just a matter of finding the highest and lowest values in your array, and using the same relationship above to convert the index of those elements back to the corresponding letter.

    Note that some validation and normalization of the input should be performed before trying to access the array ("isalpha()" and "toupper()" would be well suited for this purpose).




    * Note: There are character sets that don't have all the letters placed in a continuous sequence, but I feel it's a safe assumption to make when writing simple programs for academic assignments

  8. #8
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Matticus View Post
    * Note: There are character sets that don't have all the letters placed in a continuous sequence, but I feel it's a safe assumption to make when writing simple programs for academic assignments
    True. Although doing it portably is also quite simple.

    E.g.
    Code:
    const char *alphabet = "abcdefghijklmopqrstuvwxyz";
    //...
    //... normalize input etc
    //...
    pos = strchr(alphabet, searchchar);
    if (!pos) {
        // ... something terrible happened
    } else 
       index = pos - alphabet;
    // ...

  9. #9
    Registered User
    Join Date
    Oct 2013
    Posts
    44
    Well, this didn't work. prob cause input[I] is my array which is an interger,and so is min and max

    Code:
    for(i = 0;i < 26;i++){
                    if(input[i] = min){
                            min = least;
    }
            if(input[i] = max){
                    max = most;}
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's a difference between = and ==.

  11. #11
    Registered User
    Join Date
    Oct 2013
    Posts
    44
    comparison might work

  12. #12
    Registered User
    Join Date
    Oct 2013
    Posts
    44
    ill look up constr and strchr. Havent learned those yet

  13. #13
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by tonedogz71 View Post
    ill look up constr and strchr. Havent learned those yet
    Look them up, yes. But make sure you're allowed to use them in your assignment; a lot of courses don't let you use language or library features not yet covered in class.

    The method Matticus suggested is perfectly fine assuming the character set is ASCII as he already mentioned. This may be the way to do it in the educational setting.

  14. #14
    Registered User
    Join Date
    Oct 2013
    Posts
    44
    still trying to research exactly how to write the code like that. Your way seemed easier but your right prob not allowed since we haven't gone over those yet

  15. #15
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by tonedogz71 View Post
    still trying to research exactly how to write the code like that. Your way seemed easier but your right prob not allowed since we haven't gone over those yet
    In that case I do think that the solution Matticus alluded to would be allowable. Maybe add a comment saying that it assumes ASCII characters for brownie points hehe

    Umm, so... what do you suspect the following short code do? What would be the value of idx?

    Code:
    int idx = 'f' - 'a';

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 07-31-2013, 12:15 AM
  2. Replies: 4
    Last Post: 05-30-2013, 05:45 PM
  3. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  4. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM