alt

Find Minimum and Maximum Element in Array

Welcome you all to the Digital Chalkie Educational Blog. Here we are going to find the minimum element and the maximum element in an array. If you haven’t like our Facebook page yet, you can simply like our FB Page through this link. (FB Page – BMaxx EDU ). You can read our previous articles based on arrays by clicking here.

We can sort out the minimum and maximum elements in a single program or separately two programs. In this article, we build a single program to sort out both values.

Problem Solving

In this case, we use the following steps to find out the above requirements.
1. Create an array of user-defined size /or fixed size. We named array as “num”
2. Run the for loop to insert the element at each location.
3. Find the maximum value.
4. Find the minimum value.
5. Print both of them.

How to find the maximum element in an array

1. First, we define the first element of the array as the max.
2. Then start a for loop to compare the selected maximum with other elements.
3. When the selected maximum is smaller than an element being compared, update the maximum value with current element.
4. End of the for loop we can get the true maximum value.

How to find the minimum element in an array

1. First, we define the first element of the array as the min.
2. Then start a for loop to compare the selected minimum with other elements.
3. When the selected maximum is smaller than an element being compared, update the minimum value with the current element.
4. End of the for loop we can get the true maximum value.

Source Code

int main()
{
     
    int size, i, j, min, max;
    int num[size];

    printf("Max and Min \n");
    printf("Enter size of array: ");
    scanf("%d", &size);
    printf("\n");

    for(i=0;i<size;i++)
    {   //Enter the values to array
        printf("Enter numbers to array  : ");
        scanf("%d",&num[i]);
    }
    min=num[0]; //Define first element as min
    max=num[0]; //Define first element as max
    for(i=1;i<size;i++)
    {    // Searching for the real min
        if (min<num[i])
            min=min;
        else
            min=num[i];
    }
     for(i=1;i<size;i++) 
     {   // Searching for the real max
        if (max>num[i])
            max=max;
        else
            max=num[i];
    }
    printf(" Minimum of number set : %d",min);
    printf(" \n Maximum of number set : %d",max);
    return 0;
}

 

Bonomi Tharinda
Bonomi Tharinda is the Developer of the BMaxx Blog. He is a skilled web developer and graphic designer. In addition, he is an undergraduate student of a representative University in Sri Lanka. He is working as a tutor of an institute and as a part-time blogger, he will help you all to make your problems solved.Here are his social accounts and you can follow him through them.