Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I push my student object onto my stack? My student class has different d

ID: 3858613 • Letter: H

Question

How can I push my student object onto my stack? My student class has different data types (string,int,string,int) for a student record. There are 15 student records in an array and Id like to take those records and push them onto the stack. My thoughts are to use a for loop and simply say studentStack.push(studentRec[i]) and place the one record into one slot on the stack and the stackSize being the same size as the array.

Here is the staticStack class with member functions including my push function:

here is my push function:

And here is my student class and member functions:

class staticStack private: int *testscores; //Pointer to avg test scores iutmstackSizeu. int top; public: //Constructor taticStack(int); ICopy constructor staticStack (const staticStack &); //Destcuctoc staticStack(); //stack operations void push void pop (int &); bool isFull() const; bool isEmpty() const; 1

Explanation / Answer

Step1: staticStack

First of all take a look at the staticStack class. You have used int *testScores to store the data.

So, you can't push the complete student record into this stack. We can only push the score value of the student record into this stack.

Also you have made a slight mistake in the void push method.

It should be void push (student &)

class staticStack
{
private:
   int *testScores;
   int stackSize;
   int top;
public:
   staticStack(int);

   staticStack(const staticStack &);

   ~staticStack();

   void push(student &);
   void pop (int &);
   bool isFull() const;
   bool isEmpty() const;
};

--------------------

Step 2: push() method

The second mistake is in this method. In staticStack class, it has been defined that stack contains only the average test scores. But in the push method you are pushing the whole student record. This is not possible. Either you have to change the staticStack class to allow the complete student record or change the push method to push only the average test score.

Below is the correct method to push the average student scores:

void staticStack::push(student &obj)
{
   if(isFull())
   {
       cout << "The stack is full. ";
   }
   else
   {
       top++;
       testScores[top] = obj.getTestsc();
   }
}

-----------------

Step3: main method

NOTE: You have not given the complete definition of the staticStack class so I can't tell what is the int value in its constructor. So, I'm just giving you the pseudocode for the main method. You have to fix the parameter that is being sent to the staticStack class as per the question.

In the below code, I have set the student test score to 15 random values and pushed them into the stack.

int main()
{
   student s[15];
   int value;
   staticStack mystack(value); // you have not mentioned about this parameter in question

   srand (time(NULL));

   for (int i = 0; i < 15; i++)
   {
       /* code */
       s[i].setTestSc(rand()%101); // sets the average test score to a random integer from 0 to 99
       mystack.push(s[i]);
   }
   return 0;
}

You have to add the following directives for the main method to work:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

Since, you have not mentioned the complete question, it is not possible to give you more details at the moment. You can use the comment section to ask any query regarding this question. Thank you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote