-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path10_generating_subarrays.cpp
60 lines (51 loc) · 1.07 KB
/
10_generating_subarrays.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
/*
Generating Subarrays
>> Given a aray, print all the subarrays
*/
#include <iostream>
using namespace std;
void subarrays(int arr[], int range){
cout << "Subarrays :- \n";
// Generating all subarays
for(int start=0; start<range; start++){ // For subarray start values
for(int end=start; end<range; end++){ // For subarray end values
// Elements of subarrays(start,end)
for(int k=start; k<=end; k++){ // Loop array-values to generate subarray
cout << arr[k] << ",";
}
cout << endl;
}
}
}
int main(){
int arr[100], range;
cout << "Enter total elements: ";
cin >> range;
cout << "Enter values: ";
for(int itr=0; itr<range; itr++){
cin>>arr[itr];
}
subarrays(arr, range);
return 0;
}
/*
OUTPUT:
Enter total elements: 5
Enter values: 1 2 3 4 5
Subarrays :-
1,
1,2,
1,2,3,
1,2,3,4,
1,2,3,4,5,
2,
2,3,
2,3,4,
2,3,4,5,
3,
3,4,
3,4,5,
4,
4,5,
5,
*/