-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCMINMAX.CPP
68 lines (64 loc) · 1.04 KB
/
DCMINMAX.CPP
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
#include<iostream.h>
#include<conio.h>
int max,min;
void rmaxmin(int *a,int n,int i)
{
if(i<n)
{
if(a[i]<a[i+1])
{
if(a[i]<a[min]) min=i;
if(a[i+1]>a[max]) max=i+1;
}
if(a[i+1]<a[i])
{
if(a[i+1]<a[min]) min=i+1;
if(a[i]>a[max]) max=i;
}
i+=2;
rmaxmin(a,n,i);
}
return;
}
void main()
{
int *a,i,n;
clrscr();
cout<<" enter the no of elements"<<endl;
cin>>n;
a=new int [n];
cout<<endl<<"enter the elements";
for( i=0;i<n;i++)
cin>>a[i] ;
if(n==0)
cout<<"no elements entered \n";
else if(n==1)
cout<<"max element is"<<a[max]<<"min element is"<<a[0];
else
{
int s;
if(n%2)
{
min=0;
max=0;
s=1;
}
else
{
if(a[0]>a[1])
{
min=1;
max=0;
}
else
{
min=0;
max=1;
}
s=2;
}
rmaxmin(a,n,s);
cout<<"max element is"<<a[max]<<endl<<"min element is"<<a[min];
}
getch();
}