C++ program on visual studio. Part 2a: Calculating the Maximum and Minimum of an
ID: 3804144 • Letter: C
Question
C++ program on visual studio.
Part 2a: Calculating the Maximum and Minimum of an Array See example 3, 4 & 5 for examples that may help you solve this problem Before you start, study and understand example 4 above. Then uncomment the commented-out line in the function display it and recompile. This will demonstrate to you the purpose of the keyword Const Complete this program: add two functions (& their prototypes) that return the maximum and minimum value of an array of x elements. The program should ask the user for these x elements Hint: the functions will return a single integer value, and should have 2 parameters: the array (passed as a constant array), and the numbers of values in the array Question: do the min element and max element functions modify the arrays in any way? If not, then the array parameter (in both the function prototype and the function definition) should be declared "const". While your functions will work just fine without the keyword const, it is better to use it here. That signals to readers of your code that the array will not be modified by the function, and would trigger a compiler error if you tried to modify the array inside the function. This is one of C++'s features that helps reduce human error. Name your program minmax array.cppExplanation / Answer
part2a:
#include<iostream>
#include<cmath>
using namespace std;
int min_element(int arr[],int n)
{
int min =10000;
for(int i=0;i<n;i++)
{
if(arr[i]<min)
min =arr[i];
}
return min;
}
int max_element(int arr[],int n)
{
int max =-10000;
for(int i=0;i<n;i++)
{
if(arr[i]>max)
max =arr[i];
}
return max;
}
int main()
{
int arr[] = {1,2,3,4,5};
cout<<min_element(arr,5);
cout<<max_element(arr,5);
return 0;
}
part2b)
#include<iostream>
#include<cmath>
using namespace std;
void query_for_array(int *arr)
{
cout<<"enter size of an array:"<<endl;
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>arr[i];
}
int main()
{
int arr[1000];
query_for_array(arr);
cout<<arr[0];
cout<<arr[1];
cout<<arr[2];
return 0;
}
part2c)
#include<iostream>
#include<cmath>
using namespace std;
int min_element(int arr[],int n)
{
int min =10000;
for(int i=0;i<n;i++)
{
if(arr[i]<min)
min =arr[i];
}
return min;
}
int max_element(int arr[],int n)
{
int max =-10000;
for(int i=0;i<n;i++)
{
if(arr[i]>max)
max =arr[i];
}
return max;
}
double arr_average(int arr[],int len)
{
int sum=0;
for(int i=0;i<len;i++)
{
sum+=arr[i];
}
return double(sum)/double(len);
}
void array_stats(int arr[],int n,int *min,int *max,double* avg)
{
*min = min_element(arr,n);
*max = max_element(arr,n);
*avg = arr_average(arr,n);
}
int main()
{
int arr[] = {1,2,3,4,5};
int min;
int max;
double avg;
cout<<arr_average(arr,5);
array_stats(arr,5,&min,&max,&avg);
cout<<min<<endl;
cout<<max<<endl;
cout<<avg<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.