Straight Min Max Algorithm - Method 2 - CSU083 - Shoolini U

Straight Min Max Algorithm - Method 2

Introduction

This is an introduction of Straight min max algorithm.


//#2 way to find min and max of array and returning 2 values using structure.
#include <stdio.h>
struct MM{
    int max;
    int min;
};
struct MM arr_min_max(int arr[], int size) {
    struct MM s;
    s.max =  s.min = arr[0]; 
    for (int i = 1; i < size; i++) {
        if (arr[i] > s.max) 
            s.max = arr[i];
         else if (arr[i] < s.min) 
            s.min = arr[i];     
    }
    return s;
}
int main() { 
    int n;
    printf("Enter size of array : ");
    scanf("%d",&n);
    int arr[n];
    printf("Enter the array: \n");
    for(int i =0;i < n;i++)
    scanf("%d",&arr[i]); 
    struct MM m = arr_min_max(arr,n);
     printf("Minimum value: %d\n",m.min);
    printf("Maximum value: %d\n",m.max);
    return 0;
}

Output:

Enter size of array : 5
Enter the array:
3 7 2 9 1
Minimum value: 1
Maximum value: 9