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. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    34

    Exclamation 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
    ...
    Last edited by Bnchs400; 10-04-2007 at 02:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iteration revisited
    By robwhit in forum C Programming
    Replies: 3
    Last Post: 09-05-2007, 09:38 AM
  2. What is the difference between Recursion and Iteration?
    By neo_phyte in forum C Programming
    Replies: 12
    Last Post: 09-13-2006, 02:13 AM
  3. MPI - linear pipeline solution for jacobi iteration
    By eclipt in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2006, 05:25 AM
  4. recursivity & iteration
    By Marlon in forum C++ Programming
    Replies: 2
    Last Post: 06-14-2005, 01:58 PM
  5. The "continue" statement in JAVA
    By Hexxx in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-26-2004, 05:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21