Thanks MikeStgt, Thanks lastchange.
Unfortunately I'm really a beginner ... Sorry.
I tried to make some changes following your suggestions but I could not solve.
Certainly I was not clear in explaining the problem and also the code presented some errors ... now I put it in the last version (also the indentation improved).
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
|
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int N=7, k=3, i =0, j=0;
int sum, MaxSum;
int S1[N] = {10, -3, -1, 6, 4, 1, -10};
for (i=0; i<N; i++) {
cout << "i = " << i << endl;
MaxSum = 0;
for (j=0+i; j<i+k; j++) {
cout << " " << "k = " << k+i << " ";
cout << " " << "j = " << j << " ";
sum += S1[j];
cout << " " << "sum = " << sum << " " << endl;
if (sum > MaxSum) {
MaxSum = sum;
if (j==N-1) {
cout << "MaxSum = " << MaxSum << endl;
}
}
}
sum = 0;
}
return 0;
}
|
Therefore, I must find the
maximum sum between "k" adjacent elements of a vector of "N" elements "i"
In the example, the vector has N = 7, k = 3 and the elements vector are
{10, -3, -1, 6, 4, 1, -10}.
The
result should be 6 + 4 + 1 =
11 with i = 3 and k = 6
If I initialize the vector with the elements (as in the code posted), I get this incorrect result
i = 0
k = 3 j = 0 sum = 10
k = 3 j = 1 sum = 7
k = 3 j = 2 sum = 6
i = 1
k = 4 j = 1 sum = -3
k = 4 j = 2 sum = -4
k = 4 j = 3 sum = 2
i = 2
k = 5 j = 2 sum = -1
k = 5 j = 3 sum = 5
k = 5 j = 4 sum = 9
i = 3
k = 6 j = 3 sum = 6
k = 6 j = 4 sum = 10
k = 6 j = 5 sum = 11
i = 4
k = 7 j = 4 sum = 4
k = 7 j = 5 sum = 5
k = 7 j = 6 sum = -5
i = 5
k = 8 j = 5 sum = 1
k = 8 j = 6 sum = -9
k = 8 j = 7 sum = 22038
i = 6
k = 9 j = 6 sum = -10
k = 9 j = 7 sum = 22037
k = 9 j = 8 sum = 44074
------------------
(program exited with code: 0)
Press return to continue
|
I have tried various ways to block the count of k when I am at the end of the carrier but nothing to do.
I also thought of calculating the possible number of possible triplettes but I could not implement them.
I guess it's really a simple thing, but I've been trying for two days and I've lost hope of succeeding.
Thanks a lot for your help.