Learning stump with FOR / IF loops

I'm experimenting while learning about these loops, and when i nest them i can get results, but not intended results where i want them.

The intended output would look like:
N.### - (hits)
...


But is returning n.69 - # (where # is an infinite loop)

There's two problems present:
1) n.### should be a 3 digit number
2) - (hits) should return a number <= 100 (i seriously doubt the same letter would be in this message more than 30 times

Here's the code for the loop:
1
2
3
4
5
6
7
8
9
for (b=0; b<1098;) { 
 if (b%3 == 0 || b == 0) {
   for (f=0; f<1098;) {
     if (tempSum == tempArr) { hits++; } // HERE is where the results are skewed
     else { f++; }
   cout << "N." << tempSum << " - " << hits << endl;
 }
 else { b++; }
}/*END LOOP*/



And here is tempSum, please note that this is a storage value for the return of the function sum() and equally for tempArr there is a storage vallue for sum2()
1
2
3
4
5
6
7
short sum () {
 q=b;
 x = message[q];
 y = message[q+1];
 z = message[q+2];
 return ((x+y+z) - offset);
}


Should i find a new way to sort the array? (Array holds 1098 ints, every 3 ints is equal to a set of numbers i will need later.) ;; While learning i've heard about more advanced ways to sort arrays (vectors / maps) but i'm actually curious of WHY this doesnt work, moreso than what would work, even though i do appreciate hints to what would work.
It looks like both of your loops will get stuck and loop forever because you are conditionally incrementing the loop counter.

Think about this:

1
2
3
   for (f=0; f<1098;) {
     if (tempSum == tempArr) { hits++; } // HERE is where the results are skewed
     else { f++; }


If tempSum == tempArr, then f will never increase, so the loop will never end. You will just keep incrementing hits over and over and over indefinitely.

You have the same problem with your outer loop. As soon as b=1, b will no longer increment and the loop will keep spinning forever and ever.
Last edited on
Thank you for turning on the lightbulb in my head. *Laughs at own stupidity*.. I guess that's what you get when you're learning.

Would accessing the variables from the function sum() make sense for an array like:
int message[] = {1, 0, 3, 2, 1}

(Pseudo code if you don't want to look at original post; for f within range; temp variable is equal to integer f in array; repeat previous for f+1 and f+2; return sum of temp variables)

Or could that give me grief later when i'm trying to pass over said array to find common variables
Topic archived. No new replies allowed.