-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum_using_recursion.cpp
More file actions
73 lines (66 loc) · 1.64 KB
/
sum_using_recursion.cpp
File metadata and controls
73 lines (66 loc) · 1.64 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
#include<iostream>
using namespace std;
//recursively check whether number is present
// bool checkNumber(int input[], int size, int x) {
// /* Don't write main().
// Don't read input, it is passed as function argument.
// Return output and don't print it.
// Taking input and printing output is handled automatically.
// */
// if(size==0)
// return false;
// if(input[0]==x)
// return true;
// checkNumber(input+1,size-1,x);
// }
// int lastIndex(int input[], int size, int x) {
// /* Don't write main().
// Don't read input, it is passed as function argument.
// Return output and don't print it.
// Taking input and printing output is handled automatically.
// */
// if (size==0)
// return -1;
// if(input[size-1]==x)
// return size-1;
// lastIndex(input,size-1,x);
// }
int firstIndex(int input[], int size, int x,int i) {
if (size==0)
return -1;
if(input[i]==x)
return i;
firstIndex(input,size-1,x,i+1);
}
/////
int sum(int input[],int n){
if(n==0){
return 0;
}
return input[0]+sum(input+1,n-1);
}
// int sum(int input[], int n) {
// /* Don't write main().
// Don't read input, it is passed as function argument.
// Return output and don't print it.
// Taking input and printing output is handled automatically.
// */
// if(n<=0)
// return 0;
// int value;
// value = input[n-1]+sum(input,n-1);
// return value;
// }
int main(){
int input[10];
int n;
int x;
cin>>n;
for(int i=0;i<n;i++)
cin>>input[i];
cin>>x;
//int c=sum(input,n);
int c =firstIndex(input,n,x,0);
cout<<c;
return 0;
}