-
While Loop
I am trying to iterate through two list objects representing polynomials to perform a multiplication operation. But I have been unable to fix the problem with the while loop because it never enters the nested for loops and size never gets decremented and I get an infinite loop. Can someone point me in the right direction? Thanks.
Code:
int size = b.PolyTerms.size();
while(!size < 0)
{
for(It = tmpPoly.PolyTerms.begin(); It != tmpPoly.PolyTerms.end(); ++It)
{
for(It2 = tmpPoly2.PolyTerms.begin(); It2 != tmpPoly2.PolyTerms.end(); ++It2)
{
exp = (*It) + (*It2);
size--;
++It;
++It2;
coeff = (*It) * (*It2);
tPoly.PolyTerms.push_back(exp);
tPoly.PolyTerms.push_back(coeff);
size--;
//reset It iterator to first term since there are still more terms to multiply
//in the second polynomial
It = tmpPoly.PolyTerms.begin();
}
}
}//end while
//Post-Condition: size < 0 so all of the terms in the second polynomial have
//been multiplied by the first term of the first polynomial. Repeat the
//process for the next term.
...
-
By this
I'm guessing you mean 'while size is not less than zero'. This is normally written as That is, "while size is greater than or equal to zero".
!size will either evaluate to 1 or 0, neither of which will ever be less than zero.
-
Oh yes. But I did that to test to see If I could determine what was being executed and what was not. I even tried while (size >0) and for some reason, the for loops are not being executed.
-
Output b.PolyTerms.size(), tmpPoly.PolyTerms.size(), and tmpPoly2.PolyTerms.size() before the while loop. One of them will probably be zero. If all are greater than zero (and you use while (size > 0)) then you should be at least entering the nested for loops.