Home > Mobile >  Using accumulate method but can't the first element is always shown as 0
Using accumulate method but can't the first element is always shown as 0

Time:11-19

I am trying to write a programm which returns a list whose n-th element is the sum of the first n Values of the transferred list.

list<int> summe(list<int> liste) {
        list<int> neueListe;
        list<int>::iterator itr;
        itr = liste.begin();
        int sum = 0; 
        int n = 0;
        
        cout << "Liste mit Summen: " << endl;
        cout << "{ " << endl;
        for (itr = liste.begin(); itr != liste.end(); itr  ) {
            sum = accumulate(liste.begin(), itr, 0);
            neueListe.push_back(sum);
            cout << sum << endl;
            n  ;
        }
        cout << " }";
        return neueListe;
        
    }
int main() {

    //Aufgabe 2.2 Teil 2
    list<int> l = { 1,2,3,4,5 };
    Algo a;
    a.summe(l);
    

}

The output is: 0,1,3,6,10 I think the problem is, that the first loop is accumulate(liste.begin(), liste.begin(), 0); which should be always 0. However I have no idea how to get the first element (although its just one element so theres no need to accumulate). I want the following output: 1,3,6,10,15.

CodePudding user response:

You are using a wrong algorithm. For this task there already exists the appropriate algorithm std::partial_sum declared in the header <numeric>.

Here is a demonstration program.

#include <iostream>
#include <list>
#include <iterator>
#include <numeric>

std::list<int> summe( const std::list<int> &liste ) 
{
    std::list<int> neueListe;
    
    std::partial_sum( std::begin( liste ), std::end( liste ),
                      std::back_inserter( neueListe ) );
                      
    return neueListe;                     
}   

int main() 
{
    std::list<int> l = { 1, 2, 3, 4, 5 };
    
    auto l2 = summe( l );
    
    for ( const auto &item : l2 )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';
    
    return 0;
}

The program output is

1 3 6 10 15 

Or you can transfer the implementation of the algorithm inside your function. For this purpose it is enough to have one loop.

CodePudding user response:

The output is: 0,1,3,6,10 I think the problem is, that the first loop is accumulate(liste.begin(), liste.begin(), 0); which should be always 0. However I have no idea how to get the first element (although its just one element so theres no need to accumulate). I want the following output: 1,3,6,10,15.

You've already been shown that this function already exists in <numeric> so you don't have to write it. But I'll answer this specific question anyway so you can learn from it, more generally.

original fragment

        for (itr = liste.begin(); itr != liste.end(); itr  ) {
            sum = accumulate(liste.begin(), itr, 0);
            neueListe.push_back(sum);
            cout << sum << endl;
            n  ;
        }

rewrite

int sum {0};
for (const auto x : liste) {   // use ranged for loop !!!!!
    sum  = x;
    neueListe.push_back(sum);
}

The various cout statements are for helping you understand what's happening, and don't belong in the final code. Rather, you print the list (or do whatever you need with it) separately.

The value n seems to have no purpose, as it's incremented but never referred to anywhere.

CodePudding user response:

 My implementation of summe() function uses an integer value in the loop to iterate over liste which I add to its length the value of 1 unit and inside accumulate method, I use as parameters begin(liste) iterator, and the next iteration next(begin(liste), i).

list<int> summe(list<int> liste) {
    list<int> neueListe;
    list<int>::iterator itr;
    itr = liste.begin();
    int sum = 0; 
    int n = 0;
        
    cout << "Liste mit Summen: " << endl;
    cout << "{ " ;
    for (int i=1; i < liste.size() 1; i  ) {
        sum = accumulate(begin(liste), next(begin(liste), i), 0);
        neueListe.push_back(sum);
        cout << sum << " ";
        n  ;
    }
    cout << " }";
    return neueListe;
}

output:

1 3 6 10 15

add binary operators

 If you want to multiply values you need to create a function to use as an additional parameter to call accumulate():

int mult_op(int x, int y) {return x*y;}
// use 1 otherwise return a list filled with 0's
accumulate(begin(liste), next(begin(liste), i), 1, mult_op); 
1 2 6 24 120
  • Related