Thread: May anyone find some bugs from this code?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    77

    May anyone find some bugs from this code?

    I have spent lots of time to debug but no result get. Problem from http://acm.hnu.cn:8080/online/?actio...=show&id=10190
    My idea:
    1) Calcuating the letter how often they appear.
    2) Find the topest apearing letter.
    3) If the current line number is less then current alphabet letter appearing number. Then print a asterisk, otherwise print a space. The space after the last asterisk should not be printed.
    Here is my code
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #define MAX_S 80
    
    int alphabet[26];
    int height;
    
    void vertHistogram()
    {
        int i, j, k;
        for (i = height; i >= 1; i--) {
            k = 0;
            for (j = 0; j < 26; j++)
                if (alphabet[j] >= i) k = j;
            for (j = 0; j <= k; j++) {
                if (alphabet[j] >= i)
                    printf("*");
                else printf(" ");
                if (j < k) printf(" ");
            }
            printf("\n");
        }
        for (i = 0; i < 26; i++) {
            printf("%c", 'A'+i);
            if (i < 25) printf(" ");
        }
        printf("\n");
    }
    
    int main()
    {
        char s[MAX_S];
        int i, j;
        for (i = 0; i < 26; i++)
            alphabet[i] = 0;
        for (i = 1; i <= 4; i++) {
            fgets(s, sizeof s, stdin);
            for (j = 0; j < strlen(s)-1; j++) {
                if (isalpha(s[j]) && isupper(s[j]))
                    alphabet[(int)(s[j]-'A')]++;
            }
        }
        height = 0;
        for (i = 0; i < 26; i++)
            if (height < alphabet[i])
                height = alphabet[i];
        vertHistogram();
        return 0;
    }
    Last edited by Mathsniper; 12-30-2006 at 09:21 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So what's the question?
    I'm guessing it's the "no spaces after last *" - am I right?

    What have you managed to figure out so far?

    You can't just dump your question and current code, and expect someone else to figure out what it ought to do / should do.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    sorry for my lazy habit and the wrong link. The problem is calcuating how often does the letter appear from A to Z. I don't know why I get the wrong answer. For example, I input the following data
    Code:
    AAABBEECDF23455K
    HAHKERH355;22AVcadf34gg
    ER{GHKLVN:>ZEasdfhF
    uepwrhdfnwe490
    The output from my prog
    Code:
    *       *
    *       *     *
    *       *     *     *
    * *     * *   *     *             *       *
    * * * * * * * *     * *   *       *       *       *
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    What's the problem here?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Mathsniper
    What's the problem here?
    Do you mean you wish to count lowercase letters in the totals?
    Code:
                if (isalpha(s[j]))
                    alphabet[toupper(s[j])-'A']++;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what are the bugs in this code..please help me.
    By me001 in forum C Programming
    Replies: 12
    Last Post: 09-23-2008, 10:52 AM
  2. Replies: 3
    Last Post: 05-08-2004, 10:58 AM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM
  4. Code to find the day type of day in the year?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-01-2002, 08:58 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM