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
#include <iostream>
using namespace std;
int main() {
int n = 0, i, count=0;
cout<<"Enter N: ";
cin >> n;
int *arr = new int[n];
cout << "Enter " << n << " items" << endl;
for (i = 0; i < n; i++) {
cin >> *(arr+i);
}
cout << endl;
int max = *arr;
for (i = 1; i < n; i++) {
if(*(arr+i) > max)
max = *(arr+i);
}
for (i = 0; i < n; i++) {
if(*(arr+i) == max)
count++;
}
cout << "Largest: "<<max<<endl;
cout << "its occurrences: "<<count<<endl;
delete [] arr;
return 0;
}
Comments
Leave a comment