Q:

Write C++ program to find maximum and minimum element in array

0

Write C++ program to find maximum and minimum element in array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
#define MAX_SIZE 100 //Maximum size of the array
using namespace std;
 
int main()
{
 
   int arr[100];
    int i, max, min, size;
 
   // Reading array sizr & elements in the array
 
    cout<<"Enter size of the array: ";
    cin>>size;
    cout<<"Enter elements in the array: ";
    for(i=0; i<size; i++)
    {
        cin>>arr[i];
    }
 
    /* Supposes the first element as maximum and minimum */
    max = arr[0];
    min = arr[0];
 
    /*
     * Finds maximum and minimum in all array elements.
     */
    for(i=1; i<size; i++)
    {
        // Finding max number
        //if cuurent element of array is greater than max
        if(arr[i]>max)
        {
            max = arr[i];
        }
 
        // Finding min number
        // If current element of array is smaller than min
        if(arr[i]<min)
        {
            min = arr[i];
        }
    }
    //Finding the maximum and minimum element
 
    cout<<"Maximum element is: "<<max<<endl;
    cout<<"Minimum element is: "<<min<<endl;
    return 0;
}

Result:

Enter size of the array: 5

Enter elements in the array: 10

20

30

40

50

Maximum element is: 50

Minimum element is: 10

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write C++ program to insert an element in array... >>
<< Write C++ program to count even and odd elements i...