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 #
theprogram need to output: 0:3 1: 0 2: 3 3: 2 4: 1 – itmemilan Nov 18 '21 at 14:59