Welcome! Log In Create A New Profile

Advanced

Assignment1 Semester2 Q4

Posted by tshax 
Announcements Last Post
Announcement myUnisa availability 21 to 24 March 2019 03/25/2019 07:26PM
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Assignment1 Semester2 Q4
August 18, 2010 11:53AM
Hi All, am trying to find da solution for this question but am kinda confuse when they say we must use two additional stacks to reverse the order elements:

below is my coding:

#include <iostream>
#include <stack>

using namespace std;

int main() {

stack<int> allint;
int num ;
num = 0;

cout<<"Enter number "<<endl;

while (num != -99)
{ cin>>num;
allint.push(num);
}


while (!allint.empty()) {
cout << allint.top() << endl;
allint.pop();
return 0;
}

Any suggestions is welcomed eye rolling smiley
avatar Re: Assignment1 Semester2 Q4
August 18, 2010 01:03PM
Your coding is fine, the only problem is that you lose the original order. Should you require the original order of the stack again, you could reverse the reversed stack. Yet its more efficient just to access a copy of the original stack, than to call the function again to reverse the order. Hope that helps.
avatar Re: Assignment1 Semester2 Q4
August 18, 2010 02:10PM
If you find the two stack method conceptually confusing, just step out the procedure by hand and it'll all make sense.

1
2
3

3
2
1

1
2
3

3
2
1
Re: Assignment1 Semester2 Q4
August 18, 2010 03:29PM
Thanks Guys, now I got it..smiling smiley
Sorry, only registered users may post in this forum.

Click here to login