0

I am trying to trace through this problem and can not figure out how the star is goes through the while loop and is stored in the array. Is * stored as 8 because of tolower? If anyone could please walk through the first for - to second for loop please I would be eternally grateful.

#include <stdio.h>
#include <ctype.h>

int main()
{
    int index, freq[26], c, stars, maxfreq;

    for(index=0; index<26; index++)
        freq[index] = 0;

    while ( (c = getchar()) != '7')
    {
        if (isalpha(c))
            freq[tolower(c)-'a']++;

        printf("%d", &freq[7]);

    }

    maxfreq = freq [25];
    for (index = 24; index >= 0; index--)
    {
        if (freq[index] > maxfreq)
            maxfreq = freq[index];
    }   

    printf ("a b c d e f\n");

    for (index = 0; index < 5; index++)
    {
        for (stars = 0; stars < (maxfreq - freq[index]); stars ++)
            printf(" ");

        for (stars = 0; stars < (freq[index]); stars++)
            printf("*");

        printf("%c  \n", ('A' + index) );
        printf(" \n");
    }
    return 0;
}
Narkha
  • 1,197
  • 2
  • 12
  • 30

1 Answers1

0

It seems that this code is a histogram of sorts that prints how many times a given character has been entered into the console before it reaches the character '7'.

The following code:

for(index=0; index<26; index++)
    freq[index] = 0;

Is simply setting all of the values of the array to 0. This is because of the fact that in C, variables that are declared in block scope (that is, inside a function) and that are not static do not have a specific default value and as such simply contain the garbage that was in that memory before the variable was declared. This would obviously affect the results that are displayed each time it is run, or when it is run elsewhere, which is not what you want I'm sure.

while ( (c = getchar()) != '7')
{
    if (isalpha(c))
        freq[tolower(c)-'a']++;

    printf("%d", &freq[7]);

}

This next section uses a while loop to continue accepting input using getchar() (which gets the next character of input from STDIN in this case) until the character "7" is reached. This is due to the fact that assigning a value (such as "c = getchar()") allows the value to be used in such a way that it can be compared using "! = '7'". This allows us to continue looping until the character that is accepted from STDIN is equal to '7', after which the while loop will end.

Inside the loop itself, it's checking the value that has been entered using "isalpha()", which returns true if the character is an alphabetic letter. By using "tolower()" and returning that value to be subtracted by the character value of 'a', we are basically finding which character in the alphabet this is numerically. An example would be if we took the letter 'F'. Capital 'F' is stored as the value 70 in the background. tolower() checks to see if it is an uppercase character, and if it is, it returns the lowercase version of it (in this case, 'f' == 102). This value is then subtracted by 'a' (stored as 97) which returns the value 6 (which, when counting from 0, is the position of 'F' in the alphabet). This is then used to target that element of the array and to increment it, which tells us that another "F" or "f" has been entered.

maxfreq = freq [25];
for (index = 24; index >= 0; index--)
{
    if (freq[index] > maxfreq)
        maxfreq = freq[index];
}

This next section sets the variable "maxfreq" to the last value (how many times 'Z' was found), and iterates downwards, changing the value of maxfreq to the highest value that is found (that is, the largest number of any given character that is found in the array). This is later used to format the output to make sure that the letters line up correctly and the number of stars and spaces are correct.

Matthew Champion
  • 400
  • 7
  • 18