Search code examples
cloopsmaxdigitsunsigned-integer

Find the position of a max on a number


I have C program that needs to find the position of a number. It goes like this:

From standard input we enter unknown number of number that are positive. The numbers have maximum of 5 digits, we read new numbers till the user enters a value that is not a number. I need to find the positions of the max digit of a number from right to left. Use the right-most position if there are more than one instance of the max digit.

The program needs to output the position and the number of times the max digit of a number was found at that position.

For example:
input:

97654 48654 12345 12343 1263 12443 12643  12777 #

output:

0: 2
1: 3
2: 1
3: 1
4: 1

because

Position: 4      3        0    1    1     1    2        0
          v      v        v    v    v     v    v        v
          97654 48654 12345 12343 1263 12443 12643  12777 #

THE PROGRAM WORKS FOR THIS SPECIFIC TEST CASE

More test cases under the code.

Here is my code:

#include <stdio.h>

int main(){
    int n;
    int max;
    int num,digit,pos,br0=0,br1=0,br2=0,br3=0,br4=0;
    while (scanf("%d",&n)) {
        max =0;
        num = n;
        pos=0;
        while (num>0) {

            digit = num%10;
            if(digit > max){
                max=digit;
                pos++;
            }
            num/=10;

        }
                    printf("%d\n",pos);
        switch (pos) {
            case 1: br0++; break;
            case 2: br1++; break;
            case 3: br2++; break;
            case 4: br3++; break;
            case 5: br4++; break;
        }
    }
    printf("0: %d\n1: %d\n2: %d\n3: %d\n4: %d\n",br0,br1,br2,br3,br4);
    return 0;
}

This program work for some test cases, such as

97654 48654 12345 12343 1263 12443 12643 12777 #
123 456 789 987 654 321 #

But not for:

542 8965 7452 1111 12 8 6532 98745 15926 #
75386 86142 94285 15926 35724 #

Solution

  • The problem with your program is that within this loop

        while (num>0) {
    
            digit = num%10;
            if(digit > max){
                max=digit;
                pos++;
            }
            num/=10;
    
        }
    

    the variable pos is incremented only when a digit that is greater than previous digits is found. For example If you have a number like this

    51234 
    

    then the first largest digit is 4 and the variable pos is set to 1. After that when the next largest digit is found that is the digit 5 the variable pos is incremented and becomes equal to 2 while actually the largest digit 5 is at the position 5.

    You need to introduce one more variable as for example

        max =0;
        num = n;
        pos=1;
    
        int i = 1;
    
        do
        {
            digit = num%10;
            if(digit > max){
                max=digit;
                pos = i;
            }
         } while ( ( num /=10 ) && ( i++ != 5 ) );
    

    I would write the program the following way

    #include <stdio.h>
    
    int main(void) 
    {
        enum { N = 5 };
        const unsigned int Base = 10;
        
        size_t total[N] = { 0 };
        
        unsigned int n;
        
        while ( scanf( "%u", &n ) == 1 )
        {
            unsigned int pos = 0;
            unsigned int max_digit = 0;
            unsigned int i = 0;
            
            do
            {
                unsigned int current_digit = n % Base;
                
                if ( max_digit < current_digit )
                {
                    pos = i;
                    max_digit = current_digit;
                }
            } while ( ( n /= Base ) && ( ++i != N ) );
            
            ++total[pos];
        }
        
        for ( unsigned int i = 0; i < N; i++ )
        {
            printf( "%u: %zu\n", i, total[i] );
        }
        
        return 0;
    }
    

    For the input

    542 8965 7452 1111 12 8 6532 98745 15926 #
    

    the program output is

    0: 3
    1: 0
    2: 3
    3: 2
    4: 1