108k views
0 votes
(Occurrence of max numbers) 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 greater than max, assign it to max and reset count to 1. If the number is equal to max, increment count by 1. Sample Run 1 Enter an integer (0: for end of input): 3 Enter an integer (0: for end of input): 5 Enter an integer (0: for end of input): 2 Enter an integer (0: for end of input): 5 Enter an integer (0: for end of input): 5 Enter an integer (0: for end of input): 5 Enter an integer (0: for end of input): 0 The largest number is 5 The occurrence count of the largest number is 4 Sample Run 2 Enter an integer (0: for end of input): 0 No numbers are entered except 0 Class Name: Exercise05_41

User Sweetfa
by
6.0k points

1 Answer

5 votes

Answer:

Following are the code to the given question:

#include <iostream>//header file

using namespace std;

int main() //main method

{

int n=0,a[1000],c=0,val=10,ma=0;//defining integer variable

cin>>val;//input value

while(val!=0)//defining while loop that checks val is not equal to 0

{

a[n++]=val;//add value in array

cout<<"Enter an integer (0: for end of input):";//print message

cin>>val;//input value

}

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

{

ma=max(a[i],ma);//holding max value in ma variable

}

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

{

if(a[i]==ma)//use if to check maximum value in array

c++;//incrementing count value

}

cout<<"The maximum value is "<<ma<<" The count of maximum value is "<<c<<endl;//print result

return 0;

}

output:

Please find the attachment file.

Step-by-step explanation:

In this code inside the main method, an integer variable is declared that input values and use while loop to check first input if the value is true it will accept value from the user and adds in the array.

In the next step, two for loop is declared that checks the max value and in the next loop it will count the number of maximum value which is occurring.

(Occurrence of max numbers) Write a program that reads integers, finds the largest-example-1
User Dlchang
by
6.6k points