Need help with compilation error

Pages: 12
Below code gives output as 32 while testing, but the expected output must be 20 instead of 32. What could be wrong here?. I am trying to find the largest sum of vector {5, 9, 7, 11} is sum of elements 9 and 11, which is 20.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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 
Last edited on
https://rubberduckdebugging.com/
¿what's the purpose of `curr'?
¿why do you check `curr > 0'?
do you mean to say to just use only curr = numbers[i]; and eliminate the remaining loop in the code? Instead of using if loop, should i restrict myself to just use only curr = curr + numbers[i]; thereby doing some kind of partial sort? I have no idea how this can be done.

1
2
3
4
5
6
7
8
if(curr > 0)
          {
            curr = curr + numbers[i];
          }
          else
          {
            curr = numbers[i];
          }
Last edited on
More general algorithms notwithstanding, you can just do k = 2 passes of selection sort, O(nk/2)

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <algorithm>

int main()
{
  int xs[] = {5, 9, 7, 11};
  
  std::iter_swap(std::begin(xs) + 0, std::max_element(std::begin(xs) + 0, std::end(xs)));
  std::iter_swap(std::begin(xs) + 1, std::max_element(std::begin(xs) + 1, std::end(xs)));
  
  std::cout << "maximum sum is " << xs[0] << " + " << xs[1] << " = " << xs[0] + xs[1] << '\n';
}


The class MaxSum is boilerplate which doesn't contribute to the solution. Including it forces a programmer to read and understand code that doesn't do anything. Just declare MaxSum::findMaxSum at namespace scope.

If you simply want "MaxSum" to serve as a grouping of names, use a namespace instead.

Last edited on
hi mbozzi

I have modified my code as per your suggestions and tested the output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdexcept>
#include <iostream>
#include <vector>
#include <algorithm>

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);
   int xs[] = {5, 9, 7, 11};
  
  std::iter_swap(std::begin(xs) + 0, std::max_element(std::begin(xs) + 0, std::end(xs)));
  std::iter_swap(std::begin(xs) + 1, std::max_element(std::begin(xs) + 1, std::end(xs)));
  
  std::cout << "maximum sum is " << xs[0] << " + " << xs[1] << " = " << xs[0] + xs[1] << '\n';
   
}
#endif 

OUTPUT:

maximum sum is 11 + 9 = 20
Registered users can post here. Sign in or register to post.
Pages: 12