#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    9
    Rep Power
    0

    Question Comparing number... HELP @_@


    I have a set of numbers & character, and each number/char appears more than once. Suppose: A, B, 6, 9, B, 8, 3, B……. (a total of 10 different kinds) How should I program, in order to find out the most occurring number/char? All the number/char has already been count, which means that I already know 6 appears six times, 9 appears 3 times, etc. I was trying to use a loop that save the first number as “temp” then start to compare the other, and if the temp is great, I would keep comparing until either the count of the other number/char is the same, then I would do something…but I start to get into infinite loop and very very long codes… any idea how to make it easier?
    **Although this is a program in assembly language, I just want to get the concept (even in C is fine...)…

    THANKS!
  2. #2
  3. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    this is probably a very ineficiant way, but it's a way.

    make an array whose index values cover all possible ascii character values that you may encounter and therefore need to count. fill all the elements with 0. then step through your text and use the character's ascii numeric value to increment the corresponding element in the array. you end up with an array with all the characters (including commas and spaces) counted.

    Code:
    #include <stdio.h>
    #define SIZE 128
    
    main()
    {
    	int i;
    	char chars[] = "A, B, 6, 9, B, 8, 3, B";
    	int counts[SIZE]	 = {0};
    	char *p = chars;
    	
    	while(*p)
    		counts[*p++]++;
    		
    	for(i = 0; i < SIZE; i++)
    		if(counts[i])
    			printf("%c %d\n", i, counts[i]);
    }
    outputs:
    Code:
      7
    , 7
    3 1
    6 1
    8 1
    9 1
    A 1
    B 3
  4. #3
  5. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    I think he said he already had the counting done.

    If this is the case, you simply have to store a temporary max count, as you stated. Set this max count to the count of the FIRST counted character. Then loop through the remaining counts, and whenever the count exceeds the temporary max count, update it.

    Of course, if you wish to know which character this count is for, then you'll also have to keep another temporary character variable to store this. Initialize this variable to the first character counted, and update it wheneven a new maximum count is found.

IMN logo majestic logo threadwatch logo seochat tools logo