low chee mun
low chee mun

Reputation: 39

calculate the sum of all element in a double array

i am a bit confused in using array in doing recursion, can anyone correct my mistake?

new update, based on question required some of the line cannot be edit

double sum_of_array(double x[],int size)
{
    static double sum; <---can be edit
    int index = 0; <--can be edit

    if(index<size){
        return sum + sum_of_array(x,size-1); <--can be edit
    } else {
       something ; <--can be edit
       return sum; <--can be edit
    }
}

int main(void){
    double x[] = {4.5,5.0,6.8};
    double y[] = {4.7,3.4,2.5,5.2};

    cout<<"Sum X = "<<sum_of_array(x,3)<<endl;
    cout<<"Sum Y = "<<sum_of_array(y,4)<<endl;

    return 0;
}

output:

Sum of the element in X[]=15.3

Sum of the element in Y[]= 15.8

Upvotes: 2

Views: 4636

Answers (12)

user4063679
user4063679

Reputation:

I hope I can still chime in with my answer. The most recent answer excluding mine is from 2011.

This is yet another solution to calculate the sum of all elements in an array.

double array_sum(double *p_array, int idx_low, int idx_high){
   if(idx_low == idx_high)
      return p_array[idx_low];
   int idx_mid=idx_low+(idx_high-idx_low)/2;
   return array_sum(p_array,idx_low,idx_mid)+array_sum(idx_mid+1, idx_high);
}

Analysis of this algorithm would yeild a run-time of O(n*log(n)). However, you'd do wise to take this statement with a pinch of salt.

Upvotes: 0

Mirzor
Mirzor

Reputation: 1

This is how I've done it:

double sum_of_array(double x[], int size)
{
    if(size == 0){
        return 0;
    }

    else{
        return x[--size] + sum_of_array(x, size);
   }

}

Upvotes: 0

Matthew
Matthew

Reputation: 13332

Ok last attempt:

double sum_of_array(double x[], int index, int size)
{
    if(index < size){
        return x[index] + sum_of_array(x, index + 1, size);
    }
    else {
        return 0;
    }

}

then

cout<<"Sum X = "<<sum_of_array(x,0,3)<<endl;

Upvotes: 0

Matthew
Matthew

Reputation: 13332

Ok then it should be like this:

double sum_of_array(double x[],int index)
{
    int size = sizeof( x) / sizeof( x[0] );
    if(index<size){
        return x[index] + sum_of_array(x, index + 1);
    }
    return 0;

}

then call

sum_of_array(x,0);

IE you always call the first time with 0 as the index

Upvotes: 0

Matthew
Matthew

Reputation: 13332

double sum_of_array(double x[],int size)
{
    size = size - 1;
    if(size < 0){
        return 0; 
    }
    return x[size] + sum_of_array(x, size);
}

Upvotes: 0

Jeff Mercado
Jeff Mercado

Reputation: 134881

The logic of your recursive function is just wrong. You never actually read the contents of the array. I'm surprised you got any meaningful output out of that.

You need to rethink of the recursive definition to perform this addition.

Base case(s):
Sum of an empty array is 0..
i.e., sum_of_array(x, 0) == 0.

Sum of a 1-element array is the value of the element.
i.e., sum_of_array(x, 1) == x[0]

Recursive case:
Sum of an n-element array is the sum of the nth element and the sum of the first n-1 elements.
i.e., sum_of_array(x, n) == x[n-1] + sum_of_array(x, n-1)

Figure out how to encode this logic in your function.

Upvotes: 2

Mankarse
Mankarse

Reputation: 40613

The problem is that you are using a static variable sum instead of x[size - 1]. Showing how to fix this is redundant at this moment (7 answers already do this). However, this can be done in one line with built in c++ capabilities:

#include <algorithm>
double sum_of_array(double x[], int size)
{
    return std::accumulate(x, x + size, 0.);
}

Upvotes: 1

flight
flight

Reputation: 7272

You never actually add the values in x[] and y[] to sum and in addition, index is always equal to 0. You should probably pass it as another parameter to the function:

double sum_of_array(double x[], int size, int index)
{
    if(index<size){
        return x[index] + sum_of_array(x, size, index+1); 
    }
    else {
        return 0;
    }
}

You don't actually need the sum variable.

Upvotes: 1

sharptooth
sharptooth

Reputation: 170489

You're trying to craft something extremely overengineered. You need two things - an edge case (recursion cut-off) and a general case (descend in recursion). In your case the edge case is "array size is zero" and the general case is "grab the first element and pass the rest of array into recursion".

It could be something like this:

double sum_of_array( double x[], int size )
{
    if( size == 0 ) { //this is the edge case
        return 0;
    }

    // here you grab the first element and pass the rest of array into a recursive call
    return x[0] + sum_of_array( x + 1, size - 1 );
}

Upvotes: 6

Patrick
Patrick

Reputation: 23619

There are quite some errors in this code:

  • First, the constant sum seems to be useless. What is ever used for?
  • Second, you never take the contents of x in your function.

Upvotes: 2

Nathan Fellman
Nathan Fellman

Reputation: 127458

I'd start with this:

    return sum + sum_of_array(x,size-1); 

Shouldn't you be returning:

    return x[size] + sum_of_array(x,size-1);      

Besides this, you should find a way to set sum to zero between initial calls to the function, because otherwise it will accumulate the sum of all the arrays you attempt to sum up.

The point is, you never initialize sum, so it has some sort of garbage there.

Whose idea was it to use a recursion of all things when a simple for loop would do the trick? Is this homework?

Upvotes: 0

Yuan
Yuan

Reputation: 2800

Error is you did not initialized static variable sum.

Upvotes: 0

Related Questions