-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-second-largest-recursion.c
More file actions
98 lines (97 loc) · 1.66 KB
/
array-second-largest-recursion.c
File metadata and controls
98 lines (97 loc) · 1.66 KB
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
//Wap to find second largrest element in an array using recursion.
// To write this code in "C" use printf in place of cout and scanf in place of cin.
#include<iostream>
using namespace std;
int array_max(int arr[], int start, int N);
int array_min(int arr[], int start, int N);
int second_max(int arr[], int start, int N, int high, int low);
int main()
{
int arr[100];
int N, i;
cout<<"Enter size of the array: ";
cin>>N;
cout<<"\n";
for(i=0; i<N; i++)
{
cout<<"Enter element for arr["<<i<<"] : ";
cin>>arr[i];
}
cout<<"\n";
cout<<"Maximum element : ";
int k = array_max(arr, 0, N);
cout<<k;
cout<<"\n";
cout<<"\n";
cout<<"Minimum element : ";
int p = array_min(arr, 0, N);
cout<<p;
cout<<"\n";
cout<<"\n";
cout<<"Second largest element : ";
int q=second_max(arr,0,N,k,p);
cout<<q;
}
int array_max(int arr[], int start, int N)
{
static int max=arr[0];
if(start<N)
{
if(arr[start]>max)
{
max=arr[start];
array_max(arr,start+1,N);
}
else
{
array_max(arr,start+1,N);
}
}
else
{
return max;
}
}
int array_min(int arr[], int start, int N)
{
static int min=arr[0];
if(start<N)
{
if(arr[start]<min)
{
min = arr[start];
array_min(arr,start+1,N);
}
else
{
array_min(arr,start+1,N);
}
}
else
{
return min;
}
}
int second_max(int arr[], int start, int N, int high, int low)
{
static int second=arr[0];
if(start<N)
{
if(arr[start]<high && arr[start]>low)
{
if(second<arr[start])
{
second=arr[start];
}
second_max(arr,start+1,N,high,low);
}
else
{
second_max(arr,start+1,N,high,low);
}
}
else
{
return second;
}
}