Answer to Question #173320 in C++ for Keega

Question #173320

You need to find which digit occurs most number of times across the four given input numbers.

input1, input2, input3 and input4 are the four given input numbers.

The program is expected to find and print the most frequent digit.

Example1 –

If input1=123, input2=234, input3=345, input4=673

We see that across these four numbers,

1, 5, 6 and 7 occur once,

2 and 4 occur twice, and

3 occurs four times.

Therefore, 3 is the most frequent digit and so the program must print 3

NOTE: If more than a digit occurs the same number of most times, then the smallest of those digits should be the result. Below example illustrates this.

Example2 –

If input1=123, input2=456, input3=345, input4=5043

We see that

0, 1, 2 and 6 occur once, and

3, 4 and 5 occur thrice.

As there are three digits (3, 4 and 5) that occur most number of times, the result will be the smallest (min) digit out of these three. Hence, the result should be 3


1
Expert's answer
2021-03-19T19:20:51-0400
#include<iostream>
using namespace std;
int mostFrequentDigit(int num1, int num2, int num3, int num4){

    int arr[] = {num1,num2,num3,num4};
        int temp[10];
        int num,y,z;
        y=sizeof(arr)/sizeof(arr[0]);
        for(int i=0;i<y;i++)
        {   num=arr[i];
            while(num!=0)
            {
                int n=num%10;
                temp[n]++;
                num/=10;
            }
        }
        int max=-1;
        int x=0;
        z=sizeof(temp)/sizeof(temp[0]);
        for(int i=0;i<z;i++)
        {
            if(temp[i]>=max)
            {
                max=temp[i];
                x=i;
            }
        }
    return 0;
    }

int main(){
int num1, num2, num3, num4;

cout<<"Enter the four numbers of any digit"<<endl;
cout<<"\n enter first number:\n"<<endl;
cin>>num1;
cout<<"\n enter second number:\n"<<endl;
cin>>num2;
cout<<"\n enter third number:\n"<<endl;
cin>>num3;
cout<<"\n enter fourth number:\n"<<endl;
cin>>num4;

mostFrequentDigit(num1, num2, num3, num3);

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS