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

An error comes up when i run this code. The error is beacuse of #include \'Stak.

ID: 3806870 • Letter: A

Question

An error comes up when i run this code. The error is beacuse of #include 'Stak.h'.

#include <iostream>
#include "Stack.h"
#include <math.h>
#include <string.h>

using namespace std;

void primeFactors(Stack &stack, int n)
{
// Print the number of 2s that divide n
while (n%2 == 0)
{
stack.push(2);
n = n/2;
}

// n must be odd at this point. incresing by 2 at one loop
for (int i = 3; i <= sqrt(n); i = i+2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
stack.push(i);
n = n/i;
}
}

// This condition is to handle the case when n
// is a prime number greater than 2
if (n > 2)
stack.push(n);
}

int main()
{
Stack stack;
primeFactors(stack, 3960);
char s[10] = "";
while(!stack.empty()) {
cout << s << stack.top();
strcpy(s," * ");
stack.pop();
}
cout << endl;
return 0;
}

Explanation / Answer

Create a header file Stack.h as given below

#include <iostream>
#include <iomanip>
#define MAX 500
using namespace std;
//Class Stack definition
class Stack
{

public:
//Member function
Stack();
   void push(int);
   int pop();

   bool empty() const;
   bool full() const;

   int capacity() const;
   int size() const;
int top();
   friend ostream &operator <<(ostream &, const Stack &);

   int stack[MAX]; // pointer to local stack of int's
   int Top; // top of stack (next avail. location)
   int maxsize; // max size of the stack
};//End of class

//Default constructor
Stack::Stack()
{
int p;
Top = -1; //Initializes top to -1
maxsize = MAX;
//stack = &p;
}//End of constructor

// push an int into a Stack
void Stack::push(int i)
{
//Checks if stack is not full
   if (!full())
   {
       cout << "push( " << i << " ) at location "
           << ++Top << ' ';
       stack[Top] = i;
   }//End of if
}//End of function

// pop an int from a Stack
int Stack::pop()
{
//Checks if stack is not empty
   if (empty())
   {
       return -1;
   }
   else
   {
       cout << "pop( ) " << stack[Top]
           << " at location " << --Top << ' ';
       // return item at top of the stack
       return stack[Top];
   }//End of else
}//End of function

// Checks whether the Stack is empty?
bool Stack::empty() const
{
//If top is -1 then stack is empty
if(Top == -1)
return true;
else
return false;
}//End of function

// Checks whether the Stack full?
bool Stack::full() const
{
//If top is equal to maximum size -1 then full
if(Top == maxsize -1)
return true;
else
return false;
}//End of function

// Returns the maximum capacity of the stack
int Stack::capacity() const
{
return maxsize;
}//End of function

// Returns the current size of the stack
int Stack::size() const
{
return Top;
}//End of function

//Returns stack top position value
int Stack::top()
{
return stack[Top];
}//End of function

//To display the stack contents
ostream &operator <<(ostream &out, const Stack &s)
{
   for (int i = s.size() - 1; i >= 0; --i)
   {
       out << setw(3) << i << setw(5) << s.stack[i] << ' ';
   }
   return out;
}