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.
#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;
}
Comments
Leave a comment