vendredi 13 novembre 2020

the sum of positive elements of an array using recursion

**hello guys i'm learning recursion and i'm trying to calculate the sum of the positive elements of an array and i dont know how to do it can anyone help me please **

 #include <iostream>
using namespace std;

int sum (int arr[],int n)
{
    if(n==-1)
    return  0;
    /*else if(arr[n]<0)
    return arr[n]+sum(arr,n+1) ;*/
    else
    return arr[n-1]+sum(arr , n-1);
    
}
int main()
{
    int  n , arr[n] ;
    cout<<"give the value of  n : " ; cin>>n;
    cout<<"give the values of the array ";
    for(int i=0;i<n;i++)
    cin>>arr[i];
    cout<<"the value of positive elements is  : " << sum(arr,n);
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire