maximum and Minimum element in array in C++/C.

 Write a program find maximum and Minimum element in array in C++/C.


#include <iostream>
using namespace std;

void display(int *arr, int n)
{

    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int minimum(int arr[], int n)
{
    int mini = INT_MAX;
    for (int i = 0; i < n; i++)
    {
        if (arr[i] < mini)
        {
            mini = arr[i];
        }
    }
    return mini;
}

int maximum(int arr[], int n)
{
    int maxi = INT_MIN;
    for (int i = 0; i < n; i++)
    {
        if (arr[i] > maxi)
        {
            maxi = arr[i];
        }
    }
    return maxi;
}

int main()
{
    int arr[] = {11, 2, 4, 15, 30, 9};
    int n = sizeof(arr) / sizeof(int);
    cout << "The array is : ";
    display(arr, n);
    cout << "The Minimum array : " << minimum(arr, n) << endl;
    cout << "The Maximun array : " << maximum(arr, n) << endl;
}



Output is:

The array is : 11 2 4 15 30 9 The Minimum array : 2 The Maximun array : 30

Post a Comment

0 Comments