include iostream using namespace std int sum int array int key if key

 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
#include <iostream>
using namespace std;
int sum(int *array, int key) {
if (key == 0) {
return 0;
} else {
return *array + sum(array + 1, key - 1);
}
}
int main() {
int dimension;
printf("Enter the dimension of the array: ");
cin >> dimension;
int array[dimension];
for (int i = 0; i < dimension; i++) {
printf("Enter the value of element %d of the array: ", i);
cin >> array[i];
}
printf("The sum of the array = %d", sum(array, dimension));
return 0;
}