Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8233047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:00:18+00:00 2026-06-07T18:00:18+00:00

I have a method that is passing an array in as reference and I

  • 0

I have a method that is passing an array in as reference and I use recursion to call this method over several times. I use the array as a “stack”. The code is for a calculator which translates postfix to infix and is just a simple tool.

I have a question regarding the code and its output. The code works but when I uncomment the one section [stack removeLastObject]; it stops working and claims the array is empty.

I don’t get this since I am removing an object from the main array — not the copies which I am using in the recursion. If I input a copy for recursion and then remove an object from the original is the copy affected?

My main example is using an array of 3, 5, + which should output (3 + 5). If I uncomment the one sections of code I get (3+3). Here is the code:

+(NSString*) descriptionTop:(NSMutableArray *) stack{

NSMutableString *programFragment = [NSMutableString stringWithString:@""];

id topOfStack = [stack lastObject];
if (topOfStack) [stack removeLastObject];
NSLog(@"operation is %@", topOfStack);
NSLog(@"Stack is%@", stack);


if([ topOfStack isKindOfClass:[NSNumber class]]){
    [programFragment appendFormat:@"%g", [topOfStack doubleValue]];
}
else if( [topOfStack isKindOfClass:[NSString class]])
{
    NSString *operation = topOfStack;
    if ([operation isEqualToString:@"+"] || 
        [operation isEqualToString:@"-"] ||  
        [operation isEqualToString:@"/"] ||  
        [operation isEqualToString:@"*"]) {

       NSMutableArray *operand1 = [stack mutableCopy];
        [operand1 removeLastObject];

        NSMutableArray *operand2 = [stack mutableCopy];
        // [stack removeLastObject];

        [programFragment appendFormat:@"(%@ %@ %@)", [self descriptionTop:operand1], operation, [self descriptionTop:operand2]];

    }
 }

NSLog(@" program fragment returns %@", programFragment);
return programFragment;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:00:20+00:00Added an answer on June 7, 2026 at 6:00 pm

    I think I understand what you’re trying to do, but your logic is rather muddled. Let’s walk through your code for the input [3 5 +] (where + is the top of the stack), assuming your extra [stack removeLastObject] is uncommented. First, you (correctly) pop the operator from the stack:

    id topOfStack = [stack lastObject];
    // topOfStack = +
    // stack = [3 5 +]
    
    if (topOfStack) [stack removeLastObject];
    // topOfStack = +
    // stack = [3 5]
    

    Then you find that topOfStack is a string, so you assign it to operation. Then you do this:

            NSMutableArray *operand1 = [stack mutableCopy];
            // operation = +
            // operand1 = [3 5]
            // stack = [3 5]
    
            [operand1 removeLastObject];
            // operation = +
            // operand1 = [3]
            // stack = [3 5]
    
            [stack removeLastObject];
            // operation = +
            // operand1 = [3]
            // stack = [3]
    

    Notice that at this point, the 5 is gone entirely! You don’t have it in any of your variables. So when you set up operand2, the 3 is on top (and in fact is the only element):

            NSMutableArray *operand2 = [stack mutableCopy];
            // operation = +
            // operand1 = [3]
            // stack = [3]
            // operand2 = [3]
    

    Now, let’s rewind to where we set up operand1, but remove the extra [stack removeLastObject]:

            NSMutableArray *operand1 = [stack mutableCopy];
            // operation = +
            // operand1 = [3 5]
            // stack = [3 5]
    
            [operand1 removeLastObject];
            // operation = +
            // operand1 = [3]
            // stack = [3 5]
    
            NSMutableArray *operand2 = [stack mutableCopy];
            // operation = +
            // operand1 = [3]
            // stack = [3 5]
            // operand2 = [3 5]
    

    Now operand2 has 5 at the top, so the recursive call to descriptionTop: on operand2 finds the 5, and you get a correct answer (3 + 5) for [3 5 +].

    But this function is still broken.

    Consider the input [3 4 5 * +]. What’s the correct output? I think it should be (3 + (4 * 5)). But what does your function do? Let’s walk through it. First, it pops the + operator:

    id topOfStack = [stack lastObject];
    // topOfStack = +
    // stack = [3 4 5 * +]
    
    if (topOfStack) [stack removeLastObject];
    // topOfStack = +
    // stack = [3 4 5 *]
    

    Next it copies the stack to operand1 and removes the last element from operand1:

        NSMutableArray *operand1 = [stack mutableCopy];
        // operation = +
        // operand1 = [3 4 5 *]
        // stack = [3 4 5 *]
    
        [operand1 removeLastObject];
        // operation = +
        // operand1 = [3 4 5]
        // stack = [3 4 5 *]
    

    Then, assuming we don’t do [stack removeLastObject], it copies the stack to operand2:

        NSMutableArray *operand2 = [stack mutableCopy];
        // operation = +
        // operand1 = [3 4 5]
        // stack = [3 4 5 *]
        // operand2 = [3 4 5 *]
    

    Now you can see that when we recursively call descriptionTop: on operand1 here, it’s going to return 5. And when we call recursively call descriptionTop: on operand2, it’s going to return 4 * 5. So we return (5 + (4 * 5)). What happened to the 3? We never reached it!

    The problem here is that, to reach the 3, we have to consume the 4 5 * from the stack as one operand, and then look for the other operand in that same stack (where 4 5 * has been consumed) so that we can find the 3.

    What you need to do is not copy the stack at all! You need to pass the same stack object to your recursive calls, so that when you pop all of the elements that form one operand, you can find the elements for the other operand at the top of the stack. Like this:

        // operation = +
        // stack = [3 4 5 *]
    
        NSString *operand2Description = [self descriptionTop:stack];
        // operation = +
        // operand2Description = "4 * 5"
        // stack = [3]
    
        NSString *operand1Description = [self descriptionTop:stack];
        // operation = +
        // operand1Description = "3"
        // operand2Description = "4 * 5"
        // stack = [] (empty stack)
    
        [programFragment appendFormat:@"(%@ %@ %@)", operand1Description, operation, operand2Description];
        // programFragment = "(3 + (4 * 5))"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some class that I'm passing as a result of a service method,
So, say I have an array that looks like this: t = [ [
I'm using a third party component that I call a method passing a model
I have a method that I call to make web service requests using GET.
I have a method that returns an array of custom class objects that are
I'm having trouble passing an array between methods and have excerpted my code below.
I have method that returns Drawable , and if its Bitmap object is recycled
I have method that returned NSManagedObject and I don't know what kind of NSManagedObject
I have method that returns module path of given class name def findModulePath(path, className):
I have a method that receives something and it needs to determine the type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.