Answer to Question #238075 in C++ for Owen

Question #238075

Name Surname Score

1. Sam Williams 60

2. John Phoenix 85

3. Simon Johnson 75

4. Sarah Khosa 81

5. Mat Jackson 38

6. Nick Roberts 26

7. Isaac Wayne 74

8. Anna Mishima 34

9. Daniel Rose 64

10. Aaron Black 83

11. Jack Mohamed 27

12. Kathrine Bruckner 42

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-16T19:09:06-0400
#include <iostream>
#include <stack>
using namespace std;
class Student{
    public:
    string name, surname;
    int marks;
    Student(){}
    Student(string name, string surname, int marks){
        this->name = name;
        this->surname = surname;
        this->marks = marks;
    }
    void display(){
        cout<<"Name: "<<this->name<<endl;
        cout<<"Surname: "<<this->surname<<endl;
        cout<<"Marks: "<<this->marks<<endl<<endl;
    }
};
int main(){
    stack<Student> stack1, stack2, stack3;
    stack1.push(Student("Sam", "Williams", 60));
    stack1.push(Student("John", "Phoenix", 85));
    stack1.push(Student("Simon", "Johnson", 75));
    stack1.push(Student("Sarah", "Khosa", 81));
    stack1.push(Student("Mat", "Jackson", 38));
    stack1.push(Student("Nick", "Roberts", 26));
    stack1.push(Student("Isaac", "Wayne", 74));
    stack1.push(Student("Anna", "Mishima", 34));
    stack1.push(Student("Daniel", "Rose", 64));
    stack1.push(Student("Aaron", "Black", 83));
    stack1.push(Student("Jack", "Mohamed", 27));
    stack1.push(Student("Kathrine", "Bruckner", 42));


    Student student;
    cout<<"Stack 1 contents\n";
    while(!stack1.empty()){
        student = stack1.top();
        student.display();
        stack3.push(student);
        stack1.pop();
    }
    while(!stack3.empty()){
        stack1.push(stack3.top());
        stack3.pop();
    }
    while(!stack1.empty()){
        student = stack1.top();
        if(student.surname[0] == 'R' || student.surname[0] == 'J' || student.surname[0] == 'M'){
            stack2.push(student);
        }
        stack3.push(student);
        stack1.pop();
    }
    cout<<"Stack 2 contents\n";
    while(!stack2.empty()){
        student = stack2.top();
        student.display();
        stack2.pop();
    }
    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