Write a coding c++ program that asks a user to input for a positive number let’s say N. The program then asks the user for N number of positive integers. The program is to determine the
largest value among the numbers and the number of times it occurred. For example, if the
user enters 5, your program should ask 5 numbers, 4 2 5 1 5 and display Highest: 5 and
its Occurrence: 2.
Input: 5 Input: 10
4 2 5 1 5 5 2 15 3 7 15 8 9 5 2
Output: Output:
Highest: 5 Highest: 15
Occurrence: 2 Occurrence: 2
Answers
Answer:
#include <iostream>
#include <climits>
#include <iomanip>
using namespace std;
int main()
{
int posnum, ctr, sum, max = 0;
int min = INT_MAX;
int terval = -1;
cout << "\n\n Input a positive integers to calculate some processes or -1 to terminate:\n";
cout << "----------------------------------------------------------------------------\n";
cout << " Input positive integer or " << terval << " to terminate: ";
while (cin >> posnum && posnum != terval)
{
if (posnum > 0)
{
++ctr;
sum += posnum;
if (max < posnum)
max = posnum;
if (min > posnum)
min = posnum;
}
else
{
cout << "error: input must be positive! if negative, the value will only be -1! try again..." << endl;
}
cout << " Input positive integer or " << terval << " to terminate: ";
}
cout << "\n Your input is for termination. Here is the result below: " << endl;
cout << " Number of positive integers is: " << ctr << endl;
if (ctr > 0)
{
cout << " The maximum value is: " << max << endl;
cout << " The minimum value is: " << min << endl;
cout << fixed << setprecision(2);
cout << " The average is " << (double)sum / ctr << endl;
}
}