Compile error with vector sum



  • Hi

    I am trying to implement the below logic to find the largest sum of vector {5, 9, 7, 11} is sum of elements 9 and 11, which is 20.
    Can anyone tell me what is wrong in my code below? Compilation gets terminated here.

    `` cpp
    #include <stdexcept>
    #include <iostream>
    #include <vector>

    class MaxSum
    {
    public:
    static int findMaxSum(const std::vector<int>& numbers)
    { int curr = numbers[0];
    int maxsum = curr;
    for(int i = 1; i<numbers.size(); i++)
    {
    if(curr > 0)
    {
    curr = curr + numbers[i];
    }
    else
    {
    curr = numbers[i];
    }
    }
    maxsum = std::max(maxsum, curr);
    return maxsum;
    }

     throw std::logic_error("Waiting to be implemented");
    };
    

    #ifndef RunTests
    int main()
    {
    std:: vector<int> v {5, 9, 7, 11};
    std::cout << MaxSum::findMaxSum(v);
    }
    #endif

    ``



  • @tampere2021 In findMaxSum function, I am trying to implement logic to return largest sum of any 2 elements in given vector of numbers.



  • Whatever you do, don't do any of the following:

    • indent your code properly
    • format your posting properly
    • post the error message the compiler is giving you

    Never, ever do one of those things.
    </sarcasm>

    But since you tried, a free tip: you have to use three `, two are not enough.



  • @hustbaer Compilation gets terminated and I can see the below error.
    34 | throw std::logic_error("Waiting to be implemented");
    | ^~~~~



  • @tampere2021
    Et näytä välittävän lähdekoodisi muotoilusta.



  • @tampere2021 sagte in Compile error with vector sum:

    std:: vector<int> v {5, 9, 7, 11};

    Easiest way is using a partial sort:

    const std::vector<int> v {5, 9, 7, 11};
    std::array<int, 2> largest_values{};
    std::partial_sort_copy(
        std::begin(v), std::end(v), 
        std::begin(largest_values), std::end(largest_values),
        std::greater<int>()
    );
    cout << std::accumulate(std::begin(largest_values), std::end(largest_values), 0) << "\n";
    

    Edit: ginge auch mit nth_element, aber nur inplace.



  • @wob I have changed the logic now.

    
    #include <iostream>
    using namespace std;
    
    int main() {
    
      int arr [4] = {5, 9, 7, 11};
      int max = 0;
      int len = sizeof(arr)/sizeof(arr[0]);
      for (int i=0; i<len-2; i++){
        for (int j=i+2; j<len; j++){
            if ((arr[i]+arr[j]) > max)
                max = arr[i]+arr[j];
        }
      }
      
      cout << max;
    
      return 0;
    }
    
    


  • @tampere2021 I think the below logic restricts me, so can I use just curr = curr + numbers[i]; But again I am not sure if this logic would work for me. Is there any other solution? As mentioned here, do I need to use your logic by replacing the line std:: vector<int> v {5, 9, 7, 11}; and use partial sort approach?

    instead of

    if(curr > 0)
              {
                curr = curr + numbers[i];
              }
              else
              {
                curr = numbers[i];
              }
    


  • @tampere2021 sagte in Compile error with vector sum:

      for (int i=0; i<len-2; i++){
        for (int j=i+2; j<len; j++){
    

    Why -2/+2 ?

    With 4 elements there should be 6 [=i=1(41)i][= \sum_{i=1}^{(4-1)} i] comparisons inside the loops.


Log in to reply