C Program to find the maximum and minimum element in the array

The following is a C program to find the maximum and minimum element in the array:

Expected Output:

How it works

We use the for loop to iterate over the elements in the array. If the current element in the array is smaller than the min , we assign that value to min. Similarly, if the current element is larger than max, we assign that value to the max. When the loop terminates we print the value of min and max variable:

The following table demonstrates what happens at each iteration of the for loop:

Iteration Condition 1  Condition 2 min max
1 arr[0]<min=>50<50=>0  arr[0]>max=>50>50=>0 min=50 max=50
2 arr[1]<min=>-100<50=>1  arr[1]>max=>-100>50=>0 min=-100 max=50
3 arr[2]<min=>20<-100=>0  arr[2]>max=>20>50=>0 min=-100 max=50
4 arr[3]<min=>245<-100=>0  arr[2]>max=>245>50=>1 min=-100 max=245
5 arr[4]<min=>0<-100=>0  arr[4]>max=>0>50=>0 min=-100 max=245

 

Leave a Comment

%d bloggers like this: