Answer to Question #235612 in C++ for Tebogo mosehla

Question #235612
Create a C++ program that has 3 Stacks.
Insert, into the first stack, all the data above (which is the data of student’s names,
surnames and the marks they obtain in a particular assessment)
Display the content of the stack on the screen (console)
Then, remove all the students whose surname starts with the alphabets ‘R’, ‘J’ and
‘M’, from the first stack and insert them into the second Stack.
1
Expert's answer
2021-09-10T18:53:28-0400
#include <iostream>
#include <stack>
#include <string>
#include <string.h>
using namespace std;
int main()
{
    //The first Stack
	stack<string> stack1;
    //Inserting into the first stack
    stack1.push("1. Sam Williams 60"); 
    stack1.push("2. John Phoenix 85"); 
    stack1.push("3. Simon Johnson 75"); 
    stack1.push("4. Sarah Khosa 81"); 
    
    stack1.push("5. Mat Jackson 38"); 
    stack1.push("6. Nick Roberts 26"); 
    stack1.push("7. Isaac Wayne 74"); 
    stack1.push("8. Anna Mishima 34"); 
    stack1.push("9. Daniel Rose 64"); 
    stack1.push("10. Aaron Black 83"); 
    stack1.push("11. Jack Mohamed 27"); 
    stack1.push("12. Kathrine Bruckner 42"); 
//Displaying the first stack 
cout<<"The first stack before removing students whose surname starts with the alphabets R, J and M\n";


    for (stack<string> displayStack = stack1; !displayStack.empty(); displayStack.pop())
        cout << displayStack.top() << '\n';


    cout << "(" << stack1.size() << " elements)\n";
//Second stack
	stack<string>stack2;
	stack<string>temp_stack1; //Creating another stack to store students whose surname don't starts with the alphabets R, J and M\n
//	cout<<"The first stack after removing students whose surname starts with the alphabets R, J and M\n";
	 for(stack<string> displayStack = stack1; !displayStack.empty(); displayStack.pop()){
	  string item =  displayStack.top() ;
	  if(item== "3. Simon Johnson 75" || item=="9. Daniel Rose 64" || item== "6. Nick Roberts 26" || 
	   item== "5. Mat Jackson 38" || item== "11. Jack Mohamed 27" || item== "8. Anna Mishima 34"
	   )
	   {
	   	stack2.push(item);
	  // 	stack1.pop();
	   }
	   else{
	   	temp_stack1.push(item);
	   }
	   stack1.pop();
	 }
       stack1 = temp_stack1;
    cout<<"The first stack after the removal of students whose surname starts with the alphabets R, J and M\n\n";
    for (stack<string> displayStack = stack1; !displayStack.empty(); displayStack.pop())
        cout << displayStack.top() << '\n';


    cout << "(" << stack1.size() << " elements)\n";
    
    cout<<"\n\nThe second stack containing students whose surname starts with the alphabets R, J and M\n";
    for (stack<string> displayStack = stack2; !displayStack.empty(); displayStack.pop())
        cout << displayStack.top() << '\n';


    cout << "(" << stack2.size() << " elements)\n";
      
	stack<string>stack3;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS