-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8960 - Find minimum and maximum element from an array.c
100 lines (79 loc) · 2.77 KB
/
8960 - Find minimum and maximum element from an array.c
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**********************************************************************************************************
NAME: CANDIDA RUTH NORONHA
CLASS: SE COMPS B
ROLL NO. : 8960
BATCH: C
TITLE: FIND MINIMUM AND MAXIMUM ELEMENT FROM AN ARRAY
SUBMISSION DATE : 22nd February, 2021
**********************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
int findMinMax(int a[],int low,int high,int *min,int *max)
{
int minL, minR, maxL, maxR, mid;
if(high == low)
*min = *max = a[low];
else if(low+1 == high)
{
if(a[low]<a[high])
{
*min = a[low];
*max = a[high];
}
else
{
*min = a[high];
*max = a[low];
}
}
else
{
mid = ( low + high )/2;
findMinMax(a, low, mid, &minL, &maxL);
findMinMax(a, mid+1, high, &minR, &maxR );
if(minL < minR)
*min = minL;
else
*min = minR;
if(maxL > maxR)
*max = maxL;
else
*max = maxR;
}
return;
}
int main()
{
int a[SIZE], i, n,min,max, low = 0, high;
printf("\n Enter the number of elements in the array : ");
scanf("%d",&n);
printf("\n Enter the elements of the array : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
high = n-1;
findMinMax(a, low, high, &min, &max);
printf("\n------------------------------------------------------------------------------------------------\n");
printf(" The minimum element is : %d\n\n The maximum element is : %d", min, max);
printf("\n------------------------------------------------------------------------------------------------\n");
return 0;
}
/**********************************************************************************************************
OUTPUT :
TEST CASE 1:
Enter the number of elements in the array : 7
Enter the elements of the array : 31 20 18 27 52 16 25
------------------------------------------------------------------------------------------------
The minimum element is : 16
The maximum element is : 52
------------------------------------------------------------------------------------------------
TEST CASE 2:
Enter the number of elements in the array : 5
Enter the elements of the array : 25 45 15 5 10
------------------------------------------------------------------------------------------------
The minimum element is : 5
The maximum element is : 45
------------------------------------------------------------------------------------------------
**********************************************************************************************************/