Thread: Switch Case for Counting Frequency of numbers

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    55

    Switch Case for Counting Frequency of numbers

    The goal is to count the frequency of no.s(0,1,2,3,4) and increment all other characters.

    Only the default case increments. I have a break statement which I thought would kick back to while which precedes the switch case statement.

    I am still trying to figure out how to search for a number - do I look for 0 or '0'?

    Thanks in advance for any help on pointing me in the right direction.

    Code:
    #include <stdio.h>
    #include <cctype>
    
    
    int main()
    {
        int c, nzero, none, ntwo, nthree, nfour, nother;
    
    
        nzero = none = ntwo = nthree = nfour = nother = 0;
        
        while ((c = getchar()) && c != EOF)
        {
            switch (c) 
            {
            case 0:
                if(c == 0 && isdigit(c))
                    ++nzero;
                break;
            case 1:
                if(c == 1 && isdigit(c))
                    ++none;
                break;
            case 2:
                if(c == 2 && isdigit(c))
                    ++ntwo;
                break;
            case 3:
                if(c == 3 && isdigit(c))
                    ++nthree;
                break;
            case 4:
                if(c == 4)
                    ++nfour;
                break;
            default:
                if(isalpha(c) || isdigit(c))
                    ++nother;
                if(c = EOF)
                    break;
            }
        }
        
        printf("Zero occurs %d times\nOne occurs %d times\nTwo occurs %d times\nThree occurs %d times\nFour occurs %d times\nOther Characters appear %d times\n", nzero,none,ntwo,nthree,nfour,nother);
    
    
        return 0;

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    "getchar()" will return an integer value that represents the code for the character entered. It looks like you want to check if that character is a numeric symbol (i.e. 1, 2, 3, etc). Therefore, you must compare it against that character (i.e. '1', '2', '3', etc) and not the integer value itself.

    The integer values that correspond to each character can be found on a standard ASCII chart. Note that the character '1' has a decimal integer value of 49, etc.

    I must ask - it seems you had an idea that this was the way to go - have you tried it in the code before asking for help?

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    Yes I tried - definitely confused on how the character/number is read by the computer. Also know the default as is will increment everything that falls under getchar(c) - and I need to fix that.

    Code:
    case 0:
    	if(c == '0')
       	    ++nzero;
    for Each but again the only thing that works is Other:
    Code:
    default:
    			if(isalpha(c) || isdigit(c))
    				++nother;
    			if(c = EOF)
    				break;

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    default:
        if(isalpha(c) || isdigit(c))
        ++nother;
        if(c = EOF)  // <--- You have the wrong operator here, you want ==
        break;
    definitely confused on how the character/number is read by the computer
    This is simple enough that if you're having trouble understanding it, do a little practice program and study the output.

    Code:
    int userInput;
    
    userInput = getchar();
    
    // assume user enters 1
    
    printf("Character = %c\nInteger Value = %d\n", userInput,userInput);
    
    if(userInput == 1)
        printf("Integer 1\n");
    if(userInput == '1')
        printf("Character 1\n");

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by sjmp View Post
    ..that falls under getchar(c) ....
    Why not using scanf ?Then you are going to read numbers and things will get much easier(i my opinion )

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    We have not gotten to scanf - but I will research it. I was orignally hoping that this would have done the trick

    Code:
    int main()
    {
    	int c, nzero, none, ntwo, nthree, nfour, nother;
    
    
    	nzero = none = ntwo = nthree = nfour = nother = 0;
    	
    	while ((c = getchar()) && c != EOF)
    	{
    		switch (c) 
    		{
    		case 't':
    		case '\n':
    		case ' ':
    			break;
    
    
    		case '0':
    			++nzero;
    			break;
    		case '1':
    			++none;
    			break;
    		case '2':
    			++ntwo;
    			break;
    		case '3':
    			++nthree;
    			break;
    		case '4':
    			++nfour;
    			break;
    		default:
    			++nother;
    			break;
    		}
    	}
    	
    	printf("Zero occurs %d times\nOne occurs %d times\nTwo occurs %d times\nThree occurs %d times\nFour occurs %d times\nOther Characters appear %d times\n", nzero,none,ntwo,nthree,nfour,nother);
    
    
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    This code is different. Is it not working for you?

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    actually this does do the trick.

    Matticus - thank you - running the simple program to see the output - answers all questions. I need to work on troubleshooting. Been doing this for a few weeks now.

    Thanks for the direction - SP

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I think that this code you posted works,isn't it?

    TIp:Try to write int main(void) instead of int main()

    EDIT->I am slower than a replay! :P

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Another way to do it:

    Code:
    #include <stdio.h>
    #include <ctype.h>    
    
    int main(void)
    {
       int c, i,nother=0;
       int numbers[5]={0};
        
       while ((c = getchar()) && c != EOF) {
          if(isdigit(c)) {
             c-='0';
             numbers[c]++;
          }
          else if(isalpha(c))
             nother++;
       } 
       for(i=0;i<5;i++) {
          printf("%d occurs %d times\n",i,numbers[i]);
       }
       printf("Other characters occurred %d times\n",nother);
       return 0;
    }

  11. #11
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    thanks that looks a lot cleaner - but we have to use a switch for the assignment.

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    @Adak:
    If you enter numbers between 5 and 9 you are writing into memory which doesn't belong to the array and could lead to nasty bugs in bigger programs:
    Code:
    $ cat test.c
    #include <stdio.h>
    #include <ctype.h>   
     
    int main(void)
    {
       int c, i,nother=0;
       int numbers[5]={0};
       int x = 999;
         
       while ((c = getchar()) && c != EOF) {
          if(isdigit(c)) {
             c-='0';
             numbers[c]++;
          }
          else if(isalpha(c))
             nother++;
       }
       for(i=0;i<5;i++) {
          printf("%d occurs %d times\n",i,numbers[i]);
       }
       printf("Other characters occurred %d times\n",nother);
       printf("x = %d\n", x);
       return 0;
    }
    $ gcc -o test test.c
    $ ./test
    555666777888999
    0 occurs 0 times
    1 occurs 0 times
    2 occurs 0 times
    3 occurs 0 times
    4 occurs 0 times
    Other characters occurred 3 times    <-- WTF, I've just pressed CTRL-D (=EOF) after the numbers
    x = 1002      <-- And it looks like we've changed x too!!
    Bye, Andreas
    Last edited by AndiPersti; 07-30-2012 at 01:07 PM. Reason: noticed, that we even change nother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Letter Frequency counting.
    By Rezi in forum C Programming
    Replies: 18
    Last Post: 05-06-2011, 10:16 AM
  2. Replies: 11
    Last Post: 04-11-2011, 05:52 PM
  3. Character frequency counting
    By zcrself in forum C Programming
    Replies: 2
    Last Post: 03-01-2010, 11:04 AM
  4. how to use case/switch with letters and numbers
    By jericjones45 in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 04:38 PM
  5. optimizing loop (frequency counting)... HELP
    By skeptik in forum C Programming
    Replies: 22
    Last Post: 05-24-2004, 09:11 PM

Tags for this Thread