forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinMaxElementFinder
44 lines (43 loc) · 1.05 KB
/
MinMaxElementFinder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// C program to find maximum and minimum element in the array
#include <stdio.h>
//Calculate array size
#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
// Function to find the minimum and
// maximum element of the array
void findMinimumMaximum(int arr[], int N)
{
int i;
// variable to store the minimum
// and maximum element
int min = arr[0], max = arr[0];
// Traverse the given array
for (i = 0; i < N; i++)
{
// If current element is smaller
// than min then update it
if (arr[i] < min)
{
min = arr[i];
}
// If current element is greater
// than max then update it
if (arr[i] > max)
{
max = arr[i];
}
}
// Print the minimum and maximum element
printf("minimum element is %d", min);
printf("\n");
printf("maximum element is %d", max);
}
int main()
{
// Given array
int arr[] = {5, 8, 4, -1 };
// length of the array
int N = ARRAY_SIZE(arr);
// Function call
findMinimumMaximum(arr, N);
return 0;
}