### 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.

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include #include class MaxSum { public: static int findMaxSum(const std::vector& numbers) { int curr = numbers[0]; int maxsum = curr; for(int i = 1; i 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 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.

 ``12345678`` ``````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)

 ``123456789101112`` ``````#include #include 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include #include #include class MaxSum { public: static int findMaxSum(const std::vector& numbers) { int curr = numbers[0]; int maxsum = curr; for(int i = 1; i 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 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``````
Topic archived. No new replies allowed.
Pages: 12