I was planning on using this cycle as part of an assignment, but it keeps looping forever. What did I do wrong?
To make it more clear, for assignment I need to calculate which logo out of 3 won. Data is written like this:
1 2 3
1 2 3
etc...
So I planned to "jump" 3 positions in the array and add them up, then set the used number as "0" and use "if" later to ignore "0's" when printing it out.
pointSum=18. As there are 18 total votes
1 2 3 4 5 6
for (int i=0; i<pointSum-1; i++){
for (int j=i+3; j<pointSum-1; j+3){
point[i]=point[i]+point[j];
point[j]=0;
}
}
I find it somewhat difficult to understand your question, but whatever else is wrong with your program, it's not likely to do what you want when you have j+3 in line 2 (which will do nothing). Presumably you mean: j+=3.
As Cheddar said, "j+3" wont save that sum anywhere, and it certainly wont increment the variable "j" by any amount. Because of that, j is always equal to 3, the original number that it equals when the loop is run through the first time, and that value never changes.
And yes, presumably, you'd probably want j+=3, which is the same as saying j=j+3;
Using i (or j) < pointSum -1 as the test condition in your loops will exclude your last array element from being utilized. Your loops go 0 - 16, not 0 -17.
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
int main()
{
int pointSum = 18;
for (int i = 0; i < pointSum - 1; i++)
{
std::cout << i << ' ';
}
std::cout << '\n';
}