vendredi 10 mai 2019

How to properly sum elements of an array

I'm running a recursion program that takes an array, prints out all values in the array, sums the values, and takes the average of the values but the values aren't being summed.

int sum(int Arr[], int num)
{
    int x = 0;
    int ans;

    if (x == num - 1)
        ans = Arr[x];
    else
        ans = Arr[x] + sum(Arr, x + 1);
    return (ans);
}

If I put in the values 1, 2, 3 I should get a sum of 6 and an average of 3.00 but instead, I get a sum of 2 and an average of 0.00

Aucun commentaire:

Enregistrer un commentaire