Part 2a Calculating the Maximum and Minimum of an Array See

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 keywordconst, 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.cpp

Part 2b: Filling an array in a function instead of in main

Move the task of querying the user for the array values to a function query_for_array(...) .

Question:  what should be the return type of this function?
Question 2:  Do you need the keyword const here?

Part 2c: One function to do several tasks (needs pass-by-reference)

Add a function array_average(int array, int length);  that returns the average value of the elements in the array.

Then add another function array_stats(...) that returns the min, max and average of an array.

Useful hint:  you already have functions to get the min, max and average.  Your stats function can simply call them...no need to cut and paste to get the smaller jobs done!
Question:  what should be the return type of this function?  (hint:  with more than one piece of information to pass back, your function should use all reference parameters, and not pass anything back via the return statement.)

Reminder:  be sure to put ampersands (&) in front of your parameter names to make them reference  parameters!

Part 2d:  Using rand( ) to generate random numbers

Add code to your program from parts 2a-2c to fill the array rand_array with 25 values using rand( ), and .   Rerun your program, to demonstrate that you get the same sequence of random numbers each time.  Finally, add code to seed the RNG (random number generator) from the system clock, and run you program several times to verify that you get a different sequence each time.  The sample program 5 above should help with both using the RNG and seeding it.

Question:  (Same question as for part 2a)

Name your program minmax_array_random.cpp

Program is posted below:

===============================================================================

Solution

//Adding both cpp files, along with answers to asked questions in function themselves

//code for minimax_array.cpp

#include <iostream>
#include <iomanip>
using namespace std;

//......................add your prototypes here................
int min_element( const int*, int);
int max_element( const int*, int);
//..............................................................


int main(void)
{
    int n = 10;
    int myarray[10];
    // int rand_array[25] // for part 2d
    int val = 0;
    int mymin, mymax;


    for(int i = 0;i < n;i++)
    {
        cout << \"Enter the element No. \" << i << \"of the array \";
        cin >> val;
        myarray[i] = val;
    }
  
// for part 2b, replace the above loop with a this function call, and write the function:
  
//    query_for_array(myarray, n);
  
// for part 2c, add a function to calculate the average of an array, and a function to return all 3 values (min, max and average).

    mymin = min_element(myarray, n);
    mymax = max_element(myarray, n);
    cout << \"The minimum element of the array is \" << mymin << endl;

    cout << \"The maximum element of the array is \" << mymax << endl;

// for part 2d, add code here to fill rand_array[] with 25 random numbers, and then use your stats function
// to find the min, max and average
  
    return 0;
}

//.......................add your functions here....................
int min_element( const int* myarray, int n ){
    int minAt = 0;
    for(int i =1; i < n; i++){
        if( myarray[i] < myarray[minAt] ){
            minAt = i;
        }
    };
    return myarray[minAt];
}

int max_element( const int* myarray, int n ){
    int maxAt = 0;
    for(int i =1; i < n; i++){
        if( myarray[i] > myarray[maxAt] ){
            maxAt = i;
        }
    };
    return myarray[maxAt];
}

//code for minmax_array_random.cpp

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

//......................add your prototypes here................
int min_element( const int*, int);
int max_element( const int*, int);
void query_for_array( int*, int );
double array_average( int* , int );
void array_stats( int*, int, int&, int&, double&);
//..............................................................


int main(void)
{
    int n = 10;
    int myarray[10];
    // int rand_array[25] // for part 2d
    int mymin, mymax;

    query_for_array(myarray, n);
  
// for part 2c, add a function to calculate the average of an array, and a function to return all 3 values (min, max and average).

    mymin = min_element(myarray, n);
    mymax = max_element(myarray, n);
    cout << \"The minimum element of the array is \" << mymin << endl;
    cout << \"The maximum element of the array is \" << mymax << endl;

// for part 2d, add code here to fill rand_array[] with 25 random numbers, and then use your stats function
// to find the min, max and average
    int rand_array[25];
    for(int i =0; i < 25; i++){
        rand_array[i] = rand()%100;
    }
    double myaverage;
    array_stats( rand_array, 25, mymin, mymax, myaverage );
    for(int i = 0; i< 25; i++){
        cout << rand_array[i] << \" \";
    } cout << endl;
    cout << \"The minimum element of the array is \" << mymin << endl;
    cout << \"The maximum element of the array is \" << mymax << endl;
    cout << \"The average sum of the array is \" << myaverage << endl;

    return 0;
}

//.......................add your functions here....................
int min_element( const int* myarray, int n ){
    int minAt = 0;
    for(int i =1; i < n; i++){
        if( myarray[i] < myarray[minAt] ){
            minAt = i;
        }
    };
    return myarray[minAt];
}

int max_element( const int* myarray, int n ){
    int maxAt = 0;
    for(int i =1; i < n; i++){
        if( myarray[i] > myarray[maxAt] ){
            maxAt = i;
        }
    };
    return myarray[maxAt];
}

double array_average( int* array, int n ){
    double answer = 0.0;
    for(int i =0; i < n; i++){
        answer = answer + array[i];
    }
    return (answer/n);
}

void query_for_array( int* myarray, int n ){
    int val = 0;
    for(int i = 0;i < n;i++)
    {
        cout << \"Enter the element No. \" << i << \"of the array \";
        cin >> val;
        myarray[i] = val;
    }
    //Question : Return type is void
    //Question2: No, as we are modifying the array, when taking the input
}

void array_stats( int* array, int n, int& min, int& max, double& average ){
    min = min_element( array, n );
    max = max_element( array, n );
    average = array_average( array, n );
    return;
    //Question : Return type is void, instead passing information by reference parameters
}

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
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
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
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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site