Home > Enterprise >  How can I locate the same highest digit?
How can I locate the same highest digit?

Time:04-28

This code can locate the same digit in a multiple numbering system. The problem I have is that how can I locate the same highest digit if there is another same digit but lower?

Example Output

Can locate the same digit

Enter the random number: 32432
Greatest digit that occurred more than once = 3

Cannot locate the same highest digit. the program can only locate the lowest

Enter the random number: 34745676
Greatest digit that occurred more than once = 4
#include <stdio.h>
int main() {
    char c;
    int counts[10] = {0,0,0,0,0,0,0,0,0,0};
    int max = 0;
    printf("Enter the random number: ");
    while(c=getc(stdin)){
        if (c=='\n') break;
        counts[c-'0']  ;
    }
    for(int i=3; i<8; i  ) {
        if(counts[i] > counts[max]) max=i;
    }
    printf("Greatest digit that occurred more than once = %i",max);

    return 0;
}

CodePudding user response:

  • Rather then search only for digits 3 to 7, search digits 0 to 9.

  • Look for counts > 1

  • Recognize that there may be no solution with a digit that occurs 2 or more times.

  • There may exist more than 1 digit with the same max count greater than 1. Below simply reports the first one. You may want something different.

  • Use an int c to distinguish the 257 different results from getc().

  • Be prepared to handle input that is not a digit nor '\n'.

  • Append a '\n' to output.

...

// char c;
int c;
int counts[10] = {0,0,0,0,0,0,0,0,0,0};
int max = 0;
printf("Enter the random number: ");
while(c=getc(stdin)){
    // if (c=='\n') break;
    if (!isdigit(c)) break;
    counts[c-'0']  ;
}
// for(int i=3; i<8; i  ) {
for(int i=0; i<10; i  ) { // Note 1
    // if(counts[i] > counts[max]) max=i;
    if(counts[i] > 1 && counts[i] > counts[max]) max=i; // Note 2
}
if (counts[max] > 1) {
  printf("Greatest digit that occurred more than once = %i\n",max);
} else {
  printf("Greatest digit that occurred more than once did not occur\n", max);
} 

Note 1: Could start at 1 as 0 is the default max.

Note 2: Could omit the counts[i] > 1 && as that consideration is tested after the loop. Left it in here as conceptually we only want to consider cases where the count is > 1.

CodePudding user response:

Assumptions

You for loop runs from 3 to 8 which means it does not scan every cell of the array. Even if you scan it from 0 to 9 you will still not be able to get the output you want.


Explanation

If i understood corectly, for the input 34745676 the correct result is 7. If that's the case you have to change your for in some way, because right now as soon as it finds the max value the condition won't be true if another digit later (which means higher digit) has the same value in order to change the value of max.


One Solution

Change your for loop to:

int max = 0;
for (int i = 1; i < 10; i  )
    if (counts[i] >= counts[max]) 
        max = i;

Demonstration

With input = 34745676 your array will look something like this after the while loop:

counts[10] = { 0, 0, 0, 1, 2, 1, 2, 2, 0, 0 }

Since the condition inside for is true if there is a greater value or the same value from the current max value, 2 >= 2 is true, hence the assignment max = i will occur on equal values as well and since the array is scanned 0 to 9, position of higher digit will be assigned last each time even if there is the same value on a lower digit.

  •  Tags:  
  • c
  • Related