Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

prototypes functions i need to make this program in c++ template < typename T> l

ID: 3806099 • Letter: P

Question

prototypes functions

i need to make this program in c++

template < typename T>
long meanArray (const structArray<T> &anne );
// sum function
template < typename T>
long sumArray( const structArray<T> &janet );

Let's keep adding functions to our new array library. You should have the following functions:

sumArray
meanArray
medianArray
maxArray
minArray

Each function should ONLY accept a structArray data type. Don't forget that our structArray is now templated, meaning all of our functions will also be templated.

Addtionally, add these lines to your structArray.h file so that you can sort the array:

#include <algorithm> // include the library with a sort function

// CAREFUL! this will sort the original array!!!
template <typename T>
void sortArray( structArray &arr ){
std::sort( arr.arrayData, arr.arrayData + arr.length );
}

BONUS:

for the median, min, and max functions, make sure you don't actually sort the original data. Ideally, you would create an extra function named copyArray that would create a copy for you, but you don't have to.

Explanation / Answer

Here is the code for you:

template < typename T>
long sumArray( const T &janet )
{
int sum = 0; // start our sum at 0
// iterate through each element in our array
for( int x = 0; x < janet.length; x++ ){
sum += janet.arrayData[ x ];
}
cout << "The sum is: " << sum << endl;
return sum;
}
template < typename T>
long meanArray (const T &anne )
{
int sum = sumArray(anne.length); // i dont have idea what wrong in this line the progam said that i have i primary expression before ','
int avg = sum/anne.length ;// calculate the average
cout << "The mean is:" << avg << endl;
}

template < typename T>
long maxArray( const T &janet )
{
int max = 0; // start our sum at 0
// iterate through each element in our array
for( int x = 0; x < janet.length; x++ ){
if(janet.arrayData[ x ] > janet.arrayData[ max ])
   max = x;   
}
cout << "The max is: " << janet.arrayData[max] << endl;
return janet.arrayData[max];
}

template < typename T>
long minArray( const T &janet )
{
int min = 0; // start our sum at 0
// iterate through each element in our array
for( int x = 0; x < janet.length; x++ ){
if(janet.arrayData[ x ] < janet.arrayData[ min ])
   min = x;   
}
cout << "The min is: " << janet.arrayData[min] << endl;
return janet.arrayData[min];
}

// CAREFUL! this will sort the original array!!!
template <typename T>
void sortArray( T &arr ){
std::sort( arr.arrayData, arr.arrayData + arr.length );
}