### Need help logic errors

Pages: 12 Based on past experience the library is especially designed as a last resort to handle the current circumstances.
Particular functionality is the mind-reading(), wave_hands() and bingo() functions

Time for a green tick and moving on I think. @denver2020, I agree entirely with @againtry's advice.

However, ...

 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include using namespace std; bool movingTotal( const vector &V, int length, int target ) { if ( length > V.size() ) return false; int sum = 0; for ( int i = 0; i < length; i++ ) sum += V[i]; if ( sum == target ) return true; for ( int i = 0, j = length; j < V.size(); i++, j++ ) { sum += V[j] - V[i]; if ( sum == target ) return true; } return false; } int main() { vector V; cout << "Add 1 2 3\n"; V.insert( V.end(), { 1, 2, 3 } ); cout << "Contains 6 " << boolalpha << movingTotal( V, 3, 6 ) << '\n'; cout << "Contains 9 " << boolalpha << movingTotal( V, 3, 9 ) << '\n'; cout << "Add 4\n"; V.push_back( 4 ); cout << "Contains 9 " << boolalpha << movingTotal( V, 3, 9 ) << '\n'; }``````

 ```Add 1 2 3 Contains 6 true Contains 9 false Add 4 Contains 9 true```
Last edited on @lastchance, All good. Now to `#include<fingers_crossed> ` Thanks againtry and lastchance. I can see that with the latest code as above, the output (1+2+3 = 6) total. Again if append element 4 and creates additional total from [2,3,4]. There would now be two totals (1+2+3 = 6 and 2+3+4 =9). At this point contains(6) and contains(9) should return True, while contains(7) should return False. In code suggested by seeplus, it output the same behavior.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152`` ``````#include #include #include #include class MovingTotal { private: std::vector list; public: void append(const std::initializer_list& list); bool contains(int total); }; void MovingTotal::append(const std::initializer_list& addition) { list.insert(list.end(), addition.begin(), addition.end()); } bool MovingTotal::contains(int total) { for (int i = 0; i < list.size() - 2; ++i) if (list[i] + list[i + 1] + list[i + 2] == total) return true; return false; } int main() { MovingTotal movingTotal; std::cout << "Add 1 2 3\n"; movingTotal.append({1, 2, 3}); std::cout << "Contains 6 " << std::boolalpha << movingTotal.contains(6) << '\n'; std::cout << "Contains 9 " << std::boolalpha << movingTotal.contains(9) << '\n'; std::cout << "Add 4\n"; movingTotal.append({4}); std::cout << "Contains 9 " << std::boolalpha << movingTotal.contains(9) << '\n'; } OUTPUT: Add 1 2 3 Contains 6 true Contains 9 false Add 4 Contains 9 true `````` If the first 3 numbers are 1,2,3 the moving total is 6
6 is false because it hasn't occurred before.

If you add 4 to the sequence it becomes 1,2,3,4 giving moving total 9
9 is false because it hasn't occurred before.

If you add 2 to the sequence it becomes 1,2,3,4,2 giving moving total 9
9 is true because it has occurred before.

If you add 0 to the sequence it becomes 1,2,3,4,2,0 giving moving total 6
6 is true because it has occurred before.

(7 doesn't occur in any of the moving totals and is therefore false so far denver2020 wrote:
There would now be two totals (1+2+3 = 6 and 2+3+4 =9). At this point contains(6) and contains(9) should return True, while contains(7) should return False.

denver2020 wrote:
In code suggested by seeplus, it output the same behavior.

@denver2020, perhaps you had better explain WHAT output you actually want from that sequence. As far as I can see, mine, @seeplus's and @mbozzi's codes return true when tested with 6 and 9 once the vector is [1,2,3,4], and if we had tested with 7 it would have returned false.

If you want something else then you had better explain more clearly.

I still fail to see any good reason for having a user-defined class in your code here. From both this thread and your associated one:
https://www.cplusplus.com/forum/general/280321/#msg1211916
as well as your own quote:
 java has arrays.asList. What is the equivalent in c++?
you look as if you have been misled by familiarity with java where everything has to be in a class.
Last edited on @lastchance
Group think isn't a proof, especially when there is nothing to demonstrate any interpretation of what 'contains' mean.

All the gibbering about classes and java is irrelevant. Hi lastchance

Sorry for the confusion here.
All I need is that when you provide the output here like (1+2+3 = 6) it must give total as 6. Again if append element 4 and creates additional total from [2,3,4]. So there would now be two totals here now (1+2+3 = 6 and 2+3+4 =9) respectively. At this point contains(6) and contains(9) should return "True", while contains(7) should return "False". Hope its clear now. @OP
No it’s not clearer.
All you’re doing is repeating the same incoherence you started with for about the third or fourth time. Remember, it’s your problem, not ours
You have been given a number of clearly written choices based on that same gibberish in good faith from which you can pick and choose what suits you.
So green tick the thread and move on. Thanks everyone for your suggestions. I will take the choices described by you here and work on this and move on further. This thread can be closed.