Answer to Question #331850 in C++ for hallelu

Question #331850

Write one dimensional array C++ program that ask to the user to enter the array size and reads n integers, finds the largest of them, and count its occurrences. Suppose that you entered 100, 200, 100, 50, 50, 50 as shown in sample input/output. The program finds that the largest is 200 and that the occurrence count for 200 is 1.

1
Expert's answer
2022-04-21T12:30:06-0400
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
	int n;
	cout << "Please, enter a size of an array: ";
	cin >> n;
	int* arr = new int[n];
	cout << "Please, enter values: ";
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];
	}
	int largest = arr[0];
	int count = 0;
	for (int i = 0; i < n; i++)
	{
		if(arr[i]>largest)
			largest=arr[i];
	}
	for (int i = 0; i < n; i++)
	{
		if (arr[i]==largest)
			count++;
	}
	cout << "The largest value is " << largest
		<< " it`s occurences is " << count;
}

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