# Help/Suggestions with Iteration

This is a discussion on Help/Suggestions with Iteration within the C++ Programming forums, part of the General Programming Boards category; If you read my original post, I have edited it to show the solution I have come up with. I ...

1. ## Help/Suggestions with Iteration

If you read my original post, I have edited it to show the solution I have come up with. I have been able to devise an algorithm to multiply two polynomials through the iteration and I have traced the general idea and it works. But currently, it does not work in my program and I have a good feeling that it has to do with the for loops inside the while loop, but I can't nail the exact problem. Can someone provide a hint as to what is the problem is so that I can get back on track? Thanks.

Code:
``` int sizeA = tmpPoly.PolyTerms.size();
int size = b.PolyTerms.size();

while(sizeA > 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--;

if(size > 0) //size of the second polynomial
{
//reset It iterator to first term since there are still more terms to
//multiply in the second polynomial.
It = tmpPoly.PolyTerms.begin();
}

//If the size of tmpPoly2 is 0 then
//we have multiplied all of the terms in tmpPoly2 by the first term in
//tmpPoly but tmpPoly still has more terms. So since tmpPoly has an exp
//followed by coefficient, subtract 2 from sizeA, and delete the first pair of
//exponent, coefficient and the while loop should repeat the process. Also,
//tmpPoly2 size is reset.

sizeA = (sizeA - 2);
cout << "SizeA is: " << sizeA << endl;

if(size == 0)
{
//remove first two enties in tmpPoly
for(int i = 0; i < 2; i++)
{
It = tmpPoly.PolyTerms.begin();
tmpPoly.PolyTerms.erase(It);
}

It2 = tmpPoly2.PolyTerms.begin();
size = tmpPoly2.PolyTerms.size();
}

if(sizeA == 0)
{
cout << "sizeA is 0. We're finished!: " << sizeA << endl;
break;
}

}//end tmpPoly2 for loop
}//end tmpPoly for loop

}//end while
...```

Popular pages Recent additions