-
Notifications
You must be signed in to change notification settings - Fork 3
/
AllIndicesProblem.cpp
75 lines (61 loc) · 1.62 KB
/
AllIndicesProblem.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
69
70
71
72
73
74
75
/*
All Indices Problem
Input elements of an array of size N.
Enter a number M to be searched.
Write a recursive function to print all index no. at which M is found in a
space seperated manner.
*/
#include<iostream>
using namespace std;
int main()
{
int N, M, A[20], x;
bool available(int [], int, int); //to check if element exists in the array or not.
void indices(int [], int, int); //print all indice(s) at which element is found.
cout<<"Enter size of the array : ";
cin>>N;
if(N<1) //size of the array should be atleast greater than or equal to 1.
{
cout<<"\nSize should not be less than 1.\nAborting!!!\n";
return 0;
}
else
{
cout<<"\nEnter array elements :\n";
for(int i=0; i<N; ++i)
cin>>A[i];
cout<<"\nEnter element to be searched : ";
cin>>M;
x=available(A,N,M);
if(x==1) //if element exists in the array.
indices(A,N,M);
else
cout<<endl<<M<<" does not exist in the entered array!!!\n";
}
return 0;
}
int available(int A[], int N, int M)
{
for(int i=0; i<N; ++i)
if(A[i]==M)
return true; //if element exists then return true or 1.
return false; //if it doesnt exist then return false or -1.
}
int i=0;
void indices(int A[], int N, int M)
{
if(i<N)
{
if(A[i]==M)
{
cout<<i<<" ";
++i;
indices(A,N,M);
}
else
{
++i;
indices(A,N,M);
}
}
}