In C++ Please write a program that implements and tests the following functions
ID: 3766297 • Letter: I
Question
In C++ Please write a program that implements and tests the following functions for use with dynamically-allocated arrays of double-precision floating-point data (based on problems 7.3, 7.4, and 7.5) through use of the new operator:
double average(double* a, int size);
This function returns the average value of an array of floating- point data. In the function, use a pointer variable, not an integer index, to traverse the array elements. If the size of the array is 0, return 0.
double* maximum(double* a, int size);
This function returns a pointer to the maximum value of an array of floating-point data. If the size of the array is 0, return NULL.
double* minimum(double* a, int size);
This function returns a pointer to the minimum value of an array of floating-point data. If the size of the array is 0, return NULL.
Example run (with user input indicated with bold italics):
Enter size of array: 6
Enter 6 floating-point values: 1.2 2.3 4.7 5.1 3.3 4.4
Average is 3.5
Maximum value is element #3: 5.1
Minimum value is element #0: 1.2
Explanation / Answer
#include<iostream>
using namespace std;
double average(double* a, int size)
{
double avg = 0;
if(size==0)
return 0;
for(int i=0;i<size;i++)
avg = avg+*(a+i);
return avg/size;
}
double* maximum(double* a, int size)
{
if(size == 0)
return NULL;
else
{
int max = 0;
for(int i=1;i<size;i++)
if(*(a+i)>*(a+max))
max = i;
return a+max;
}
}
double* minimum(double* a, int size)
{
if(size == 0)
return NULL;
else
{
int min = 0;
for(int i=1;i<size;i++)
if(*(a+i)<*(a+min))
min = i;
return a+min;
}
}
int main()
{
int size;
cout << "Enter the size of an array : ";
cin >> size;
double arr[size];
cout << "Enter 6 floating point values : ";
for(int i=0;i<size;i++)
cin >> arr[i];
double *max = maximum(arr,size);
double *min = minimum(arr,size);
cout << "Average is : " << average(arr, size) << endl;
cout << "Maximum value is element : " << *max << endl;
cout << "Minimum value is element : " << *min << endl;
return 0;
}