-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLastIndex.cpp
More file actions
59 lines (45 loc) · 1.14 KB
/
LastIndex.cpp
File metadata and controls
59 lines (45 loc) · 1.14 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
/*
Last Index
Enter elements of an array of size N. Take as input a number M.
Write a recursive function which returns the last index at which M is found in the array
and -1 if M is not found anywhere. Print the value returned.
*/
#include<iostream>
using namespace std;
int main()
{
int N, A[20], M, x, lastIndex(int [], int, int);
cout<<"Enter array size : ";
cin>>N;
if(N>0)
{
cout<<"\nEnter array elements :\n";
for(int i=0; i<N; ++i)
cin>>A[i];
cout<<"\nEnter element to be searched : ";
cin>>M;
int j=N-1;
x=lastIndex(A,j,M);
if(x!=-1)
cout<<"\nLast index at which "<<M<<" is found is : "<<x<<endl;
else
cout<<"\nElement not found!!!\n";
}
else
cout<<"\nArray size should be atleast greater than or equal to 1 !!!\n";
return 0;
}
int lastIndex(int A[], int j, int M)
{
if(j>=0)
{
if(A[j]==M)
return j;
else
{
--j;
lastIndex(A,j,M);
}
}
return -1;
}