Click here to Skip to main content
15,887,328 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
void add(stack<int>& inputStack, int N, int data, vector<int>& ans, int total, stack<int>& tempStack) {
    if (data <= 0) {
        return;
    }
    
    inputStack.push(ans[data - 1]);
    add(inputStack, N, data - 1, ans, total, tempStack);
}

void remove(stack<int>& inputStack, int N, int data, vector<int>& ans, stack<int>& tempStack) {
    int mid = (N) / 2;
    if (data <= 0) {
        inputStack.pop();
        return;
    }
    
    ans.push_back(inputStack.top());
    inputStack.pop();
    remove(inputStack, N, data - 1, ans, tempStack);
}

void deleteMiddle(stack<int>& inputStack, int N) {
    int data = (N) / 2;
    vector<int> ans(data);
    stack<int> tempStack;
    remove(inputStack, N, data, ans, tempStack);
   
    int total = 0;
    data = (N) / 2;
    add(inputStack, N, data, ans, total, tempStack);
}


What I have tried:

I have tried using recursion.
It is showing error for testcase like 1 2 3 4 5
Mine output:
0 0 4 5?
could anybody explain why is it showing error?
Posted
Updated 13-Aug-23 6:23am

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C++
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Comments
I RAJ 13-Aug-23 2:28am    
I am basically trying to find the error here from last two days. Although, I am not able to get, where am I going wrong? Could you be more specific where exactly I am doing the error?
OriginalGriff 13-Aug-23 3:13am    
Yes, I could - but then you wouldn't learn anything, and learning to debug your code is a pretty big part of development!

You wrote the code, so you know exactly what it is supposed to do - so use that. Start by putting a breakpoint at the top of the Remove function, and step through your code, looking at what you expect to happen for each line, and comparing what did happen after it has executed.
When you find a difference, look at why; run it again up to that point and look more closely at the input.

It's not rocket science!

And by the way, using recursive functions to remove stuff is probably a bad idea: it uses a lot of stack space so there is a fairly small limit to the number of items you can process before your app crashes. Iterative solutions are generally better than recursion unless the algorithm is very specifically recursive as in a disk folder structure for example. A stack isn't truly a recursive data structure!
I RAJ 13-Aug-23 4:14am    
I am doing it step by step. For the above test case, data=5/2=2. In the remove function, I am recursively removing values from top here, so vector_ans will store [5 4] and then I am popping out the value 3 in the base case, then the function will return. Then, in add function I am trying to push the values back to stack. Again, data=2 so ans[data-1]=ans[1]....which is 4 and then next time ans[data-1]=ans[0] which is 5... then I am returning back. I don't find an error plz help me out
OriginalGriff 13-Aug-23 4:40am    
And did you check what is happening to ans and input_stack at each pass through?
I RAJ 13-Aug-23 4:52am    
In which function remove or add?
Just as an aside, I took your task, and implemented it in C# (so you can't hand it in as your own work) because your C++ implementation is ... ummm ... wierd.
Here's the class that does all the work:
using System;
using System.Collections.Generic;
using System.Linq;

namespace OneOffJobs
    {
    public class DebugStack : Stack<int>
        {
        #region Constructors
        public DebugStack(IEnumerable<int> data)
            {
            foreach (int x in data) Push(x);
            }

        #endregion

        #region Public Methods
        public List<int> Remove(int n)
            {
            List<int> ans = new List<int>();
            while (n-- > 0) { ans.Add(Pop()); }
            return ans;
            }
        public int DeleteMiddle()
            {
            if (Count == 0) return int.MinValue;
            int data = Count / 2;
            Stack<int> ans = new Stack<int>();
            while (data-- >= 0) ans.Push(Pop());
            int returnValue = ans.Pop();
            foreach (int x in ans) Push(x);
            return returnValue;
            }
        #endregion

        #region Overrides
        public override string ToString()
            {
            return string.Join(", ", this.Reverse());
            }
        #endregion
        }
    }

And the calling code is clean as well:
DebugStack ds = new DebugStack(new int[] {1, 2, 3, 4, 5});
Console.WriteLine(ds.ToString());
ds.DeleteMiddle();
Console.WriteLine(ds.ToString());
ds.Remove(2);
Console.WriteLine(ds.ToString());
That's the advantage of understanding how a language works: instead of trying to force "built in classes" to do what you want, you use what they can do properly. In this case, the class derives from Stack<int> so it inherits useful methods like Push and Pop automatically. Your code shows that you don't understand C++ classes very well, and to get you to this kind of exercise, you really should by now!

I'd suggest that you throw away the code you have, and start again using the stack and vector constructs intelligently. You will get better code, and that should mean a better grade.
 
Share this answer
 
Comments
I RAJ 13-Aug-23 20:22pm    
Thank you man for responding every time I asked and providing me the answer. But, I wanted to know what exactly was wrong in my solutions. After a couple of days, I finally knew what was wrong, it was setting up the size of (ans) to be data. After removing it, it runs smoothly.
OriginalGriff 14-Aug-23 1:14am    
"I wanted to know what exactly was wrong in my solutions"
Rather than going into full "code review mode" on your solution, I'd just say park this code somewhere (Zip it up and leave it alone) and come back and look at it in three months time. I'm pretty sure you will be able to understand why it's such poor code and what is wrong with the whole design!

That's not saying anything rude about you - you are a student, and they rarely produce decent code because they don't know any better - you will improve with practice but it'll be worth you looking back at your earlier stuff to realize how far you have come!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900