Uncategories
Given an array of length N, you need to find and return the sum of all elements of the array. Do this recursively.
Next
« Prev Post
« Prev Post
Previous
Next Post »
Next Post »
Subscribe to:
Post Comments (Atom)
PROBLEM:-
Line 1 : An Integer N i.e. size of array
Line 2 : N integers which are elements of the array, separated by spaces
Sum
1 <= N <= 10^3
3
9 8 9
26
3
4 2 1
7
SOLUTION:-
#include<iostream>
using namespace std;
int sum(int input[],int n)
{
if(n==1)
return input[n-1];
return input[n-1]+sum(input,n-1);
}
int main(){
int n;
cin >> n;
int *input = new int[n];
for(int i = 0; i < n; i++) {
cin >> input[i];
}
cout << sum(input, n) << endl;
}
This is dummy text. It is not meant to be read. Accordingly, it is difficult to figure out when to end it. But then, this is dummy text. It is not meant to be read. Period.
If you have any doubts then please let me know... ConversionConversion EmoticonEmoticon