179k views
4 votes
Write a program that reads integers, finds the largest of them, and counts its occurrences. Assume that the input ends with number 0. Suppose that you entered 3 5 2 5 5 5 0; the program finds that the largest is 5 and the occurrence count for 5 is 4. (Hint: Maintain two variables, max and count. max stores the current max number, and count stores its occurrences. Initially, assign the first number to max and 1 to count. Compare each subsequent number with max. If the number is > max, assign it to max and reset count to 1. If the number is equal to max, increment count by 1.)

User Somputer
by
5.2k points

1 Answer

6 votes

Answer:

#include <bits/stdc++.h>

using namespace std;

int main() {

int n=0,a[1000],c=0,val=10,ma=INT_MIN;//n is for size c for count of maximum,ma to store maximum.

cout<<"Enter the values"<<endl;

cin>>val;//taking input..

while(val!=0)

{

a[n++]=val;

cin>>val;

}

for(int i=0;i<n;i++)//loop for finding the maximum in the array.

{

ma=max(a[i],ma);

}

for(int i=0;i<n;i++)//loop for counting the occurence of maximum.

{

if(a[i]==ma)

c++;

}

cout<<"The maximum value is "<<ma<<" The count of maximum value is "<<c<<endl;//printing the result..

return 0;

}

Output:-

Enter the values

5 4 7 8 5 8 4 5 6 2 1 5 3 8 8 8 8 0

The maximum value is 8 The count of maximum value is 6

Step-by-step explanation:

I have iterated over the array twice.First time to find the maximum and second time to find the occurrences of the maximum in the array. The value of ma is minimum number possible because if we assign it maximum value possible it will become the maximum.

User Tuomas Pelkonen
by
5.4k points