C++ Visual Studio 2019
Write a C++ console application that accepts up to 5 numbers from the user. Display all numbers, the highest, the lowest, and the average of the numbers. Ask the user if they want to continue entering another set of numbers.
1) Use proper naming conventions for your variables and
functions.
2) Tell the user what the program is all about. Do NOT start the
program with “Enter a number”!!
3) Create an array to store the numbers.
4) The user does not have to enter all 5 numbers. They can enter
fewer. However, of course, you need at least two numbers to be able
to look for the highest and the lowest. Make sure you explain this
to the user.
5) Create three functions to perform the following tasks:
a. Calculate the highest number.
b. Calculate the lowest number.
c. Calculate the average.
6) All three functions receive an array and the number of elements,
and return a single value.
7) Keep track of the number of the values the user enters. Remember, they do not have to enter all 5 numbers.
#include<iostream>
using namespace std;
int currentArraySize=0;
int ArrayMax(int *arr)
{
int maxelem=INT_MIN;
for(int i=0;i<currentArraySize;i++)
{
if(arr[i]>maxelem)
maxelem=arr[i];
return maxelem;
}
}
int ArrayMin(int *arr)
{
int minelem=INT_MAX;
for(int i=0;i<currentArraySize;i++)
{
if(arr[i]<minelem)
minelem=arr[i];
return minelem;
}
}
float ArrayAvg(int *arr)
{
float sum=0,avg;
for(int i=0;i<currentArraySize;i++)
{
sum=sum+arr[i];
}
return sum/currentArraySize;
}
int main()
{
int option;
int input[5],elem;
do
{
cout<<"There are currently "<<currentArraySize<<"elements"<<endl;
cout<<"Enter 0 to stop "<<endl;
cout<<"Enter 1 to find max of Elements Entered (min elements=2)"<<endl;
cout<<"Enter 2 to find min of Elements Entered (min elements=2)"<<endl;
cout<<"Enter 3 to find Average of Elements Entered"<<endl;
cout<<"Enter 4 to enter new Elements"<<endl;
cin>>option;
switch(option)
{
case 1:
if(currentArraySize<2)
{
cout<<"Elements less not suffient to 2 enter more elements"<<endl;
}
else
{
cout<<"Maximum element is "<<ArrayMax(input)<<endl;
}
break;
case 2:
if(currentArraySize<2)
{
cout<<"Elements less not suffient to 2 enter more elements"<<endl;
}
else
{
cout<<"Minimum element is "<<ArrayMin(input)<<endl;
}
break;
case 3:
cout<<"Average of Elements is "<<ArrayAvg(input)<<endl;
break;
case 4:
if(currentArraySize ==5)
{
cout<<"Already 5 elements present"<<endl;
}
else
{
cout<<"enter Element"<<endl;
cin>>elem;
input[currentArraySize++]=elem;
}
break;
}
}while(option!=0 );
}
above shows output handles all case copy the code and run on ur machine to check
Thanks happy coding !
Get Answers For Free
Most questions answered within 1 hours.