I was told to only write just the function. Did I do that correctly? Question 10
ID: 3695559 • Letter: I
Question
I was told to only write just the function. Did I do that correctly?
Question 10. Write template for two functions Average and Maximum. The Average function should accept two arguments: an array and its size and return the average of all the elements in the array. The maximum function should also accept two arguments: an array and its size and return the largest value among all the elements in the array. Design a simple program that demonstrates the templates with various data types
(int, float, double).
double max_Array( const float array[ ], int array_Size );
double average_array( const float array[], int aaray_size );
double array1[ ] = { 10.0, 20.0, 100.0, 0.001 };
double array2[ 2 ][ 3 ] = { { 5.0, 10.0, 20.0 },
{ 8.0, 15.0, 42.0 } };
int sizes[ 2 ] = { 4, 3 };
double max1, max2, max3, max4, max5;
float som,avg;
Explanation / Answer
//Function definition of max_Array
double max_Array( const float array[ ], int array_Size )
{
double maximum;
int i;
if( array_Size <= 0 )
{
return 0.0;
}
maximum = array[ 0 ];
for( i = 1; i < array_Size ; i++ )
maximum = ( array[ i ] > maximum ) ?array[ i ] : maximum;
return maximum;
}
//Function definition of average_array
double average_array( const float array[], int aaray_size )
{
float sum = 0.0;
int i;
for (i = 0; i < aaray_size; i++)
sum += array[i];
return sum/aaray_size;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.