Multiplying Array Based Stacks?

This is a discussion on Multiplying Array Based Stacks? within the C Programming forums, part of the General Programming Boards category; How would one go about doing this? It's like doing second grade multiplication, like: Code: 1234 11 ____ 1234 12340 ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    Multiplying Array Based Stacks?

    How would one go about doing this? It's like doing second grade multiplication, like:

    Code:
      1234
        11
      ____
      1234
     12340
    +
     13574
    like that. I'm trying alot of different things but ive managed to come up with some really buggy code. When responding, don't just give me code, but suggestions on what to do change/do. Heres my function. It makes use of push, pop, etc.

    Code:
    stack multiply(stack stackOne, stack stackTwo)
    {
         stack copyStack;
         copyStack.top = -1;
         stack carry;
         carry.top = -1;
         stack runOne;
         runOne.top = -1;
         
         int x, y, limitOne, limitTwo, lhs, rhs, test, quotient, carryInt, total = 0, digit;
         int limitThree, z;
         limitOne = stackOne.top;
         limitTwo = stackTwo.top;
         
         for(x = 0; x <= limitTwo; x++)
         {
               copyStack = stackOne;
               pop(&stackTwo, &rhs);
               
               for(y = 0; y <= limitOne; y++)
               {
                     if(test = isEmpty(&copyStack) != 1)
                             lhs = 1;
                     else 
                          pop(&copyStack, &lhs);
                     
                     if(test = isEmpty(&carry) != 1)
                             carryInt = 0;
                     else
                         pop(&carry, &carryInt);
                         
                     quotient = (lhs * rhs) + carryInt;
                     
                     if(quotient > 10)
                     {
                          digit = quotient % 10;
                          push(&runOne, digit);
                          carryInt = (quotient - digit) / 10;
                          push(&carry, carryInt);
                     }
                     else if(quotient == 10)
                     {
                          push(&runOne, 0);
                          push(&carry, 1);
                     }
                     else if(quotient < 10)
                     {
                          push(&runOne, quotient);
                          }
                     if(y == stackTwo.top-1)
                     {
                          limitThree = carry.top;
                          for(z = 0; z <= limitThree; z++)
                                pop(&carry, &carryInt);
                                push(&runOne, carryInt);
                     }
               }
         }     
         return runOne;             
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
                     if(test = isEmpty(&carry) != 1)
                             carryInt = 0;
    What's test for?

    Code:
                     if(quotient > 10)
                     {
                          digit = quotient % 10;
                          push(&runOne, digit);
                          carryInt = (quotient - digit) / 10;
                          push(&carry, carryInt);
                     }
                     else if(quotient == 10)
                     {
                          push(&runOne, 0);
                          push(&carry, 1);
                     }
                     else if(quotient < 10) // Could be else
                     {
                          push(&runOne, quotient);
                          }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    test is for the function isEmpty. if ifEmpty returns 0, than stack is not empty. otherwise itll return empty

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > if(test = isEmpty(&carry) != 1)
    I think he meant you should look at it more closely

    You've written
    if(test = (isEmpty(&carry) != 1) )

    What most people want, when they have an assignment is
    if( (test = isEmpty(&carry)) != 1)

    Mind you, since you don't use the variable, it's just a waste of space

    Since you've stated that it will return 0 when the stack is not empty, you can just write
    if ( isEmpty(&carry) )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. declaring an array of n elements based on input
    By strobe9 in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2001, 03:37 PM

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