Find Min and Max in array
- We will make a function return two values, the maximum and minimum values, and store them in another array.
- Thereafter, the array containing the maximum and minimum values will be returned from the function
Min Find Max Find
#include<stdio.h> #define max 100 int * maxmin(int ar[], int v); void main() { int arr[max]; int n, i, * p; printf("How many values? "); scanf("%d", & n); printf("Enter %d values\n", n); for (i = 0; i < n; i++) scanf("%d", & arr[i]); p = maxmin(arr, n); printf("Minimum value is %d\n", * p++); printf("Maximum value is %d\n", * p); } int * maxmin(int ar[], int v) { int i; static int mm[2]; mm[0] = ar[0]; mm[1] = ar[0]; for (i = 1; i < v; i++) { if (mm[0] > ar[i]) mm[0] = ar[i]; if (mm[1] < ar[i]) mm[1] = ar[i]; } return mm; }
How many values? 4 Enter 4 values 11 22 52 01 Minimum value is 1 Maximum value is 52
- We will use Two Arrays.
- The first array will contain the values from which the maximum and minimum values have to be found.
- The second array will be used to store the minimum and maximum values of the first array.