Thread: confused

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    33

    confused

    can anyone explain what is happening in this program I really dont understand it. thank you.

    Code:
    #include <stdio.h>
    
    int main() {
    
      int index, freq[26], c, stars, maxfreq;
    
      for (index=0; index<26; index++)
        freq[index] = 0;
    
      while ( (c = getchar()) !=EOF) {
    
        if (isalpha(c))
          freq[2*(tolower(c)-'a')]++;
      }
    
    
      maxfreq = freq[0];
      for (index = 1; index < 26; index++) {
    
        if (freq[index] > maxfreq)
          maxfreq = freq[index];
      }
    
      for (index=maxfreq; index>=1; index--) {
    
        for (stars=0; stars<26; stars++) {
    
          if (freq[stars] >= index)
            printf("*");
          else
            printf(" ");
        }
        printf("\n");
      }
      printf("abcdefghijklmnopqrstuvwxyz\n");
    system("pause");
      return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please be reminded of our homework policy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    33
    please be reminded its not homework its a practice question for a test im preparing for.

  4. #4
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    As you say it is not a HW,
    It is a pgm to print a Histogram based upon the frequency of alphabets in ur
    input.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    please be reminded its not homework its a practice question for a test im preparing for.
    That's homework.

    Attempt the question, give what you think is the answer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    as stevesmithx allready said it writes a '*' for every letter inputed, but it is flawed for it only accepts letters up to 'm' cuz of your
    Code:
    freq[2*(tolower(c)-'a')]++;
    and I don't think this was such a hard problem that you couldn't do on your own, but still, that's what it does.

    by

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    but it is flawed for it only accepts letters up to 'm' cuz of your
    Actually, I'd call that a little more than a flaw... Entering "zzz" causes out of bounds array access in this program.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 07:32 PM
  2. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  3. So Now Im getting confused?!?!?
    By zergdeath1 in forum C++ Programming
    Replies: 11
    Last Post: 03-06-2004, 05:41 PM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM