What is the index of the top element of the stack

Assignment Help Computer Engineering
Reference no: EM132189960

Will be working with two files lab7.cpp and stack.h ( implemnetation )

/* Lab 7 Application file Calls to functions from the enhanced stack class. */ #include <cstdlib> #include <iostream> #include "stack.h" // Note: stack implementation file is included using namespace std; int main(int argc, char *argv[]) { // declare a stack called stacky // Push characters F, L, O, W, E, and R on the stack cout << "The initial stack top and bottom are:\n"; // Call print_ends // Change the bottom of the stack to 'P' using change_bottom // Pop the stack cout << "\n\nThe stack top and bottom after changes are:\n"; // Call print_ends // Print the number of elements in the stack using elements function cout << "\n\nThere are now " << << " elements in the stack"; cout << "\n\n"; return 0; }
stack.h file

#include <iostream> using namespace std; // Implementation file for the stack const int stack_size = 1000; class stack { private: // array of elements in the stack char data [stack_size]; // index to the top of the stack int top; public: // stack is a constructor, creates an empty stack stack (); // removes an element from the stack and returns it char pop (); // adds an element to the top of the stack void push (char item); // returns true if the stack is empty, else false bool empty (); // returns true if the stack is full, else false bool full (); }; // stack is a constructor, creates an empty stack stack::stack () { top = -1; } // removes an element from the stack and returns it char stack::pop () { // if the stack is empty, print an error if (empty ()) { cout << "\n\nStack error: pop"; cout << "\nPopping an empty stack"; cout << "\nReturning a space"; return ' '; } else // OK to pop the stack { top--; return data [top + 1]; } } // adds an element to the top of the stack void stack::push (char item) { // if the stack is full, print an error message if (full ()) { cout << "\n\nStack error: push"; cout << "\nPushing onto a full stack"; } else // OK to push { top++; data [top] = item; } } // returns true if the stack is empty, else false bool stack::empty () { return top == -1; } // returns true if the stack is full, else false bool stack::full () { return top == stack_size - 1; } Questions:
1. Write an arithmetic expression that will compute the number of elements in a stack given the value of top.

2. Does this expression give the correct answer for an empty stack? Explain.

3. What is the index of the top element of the stack.

4. What is the index of the bottom element of the stack?

5. Write a C++ expression that gives the value stored at the top of the stack.

6. Write a C++ expression that gives the value stored at the bottom of the stack.

Make the following changes to the implementation file:

- Write a stack member function called elements that returns the number of elements in a stack (the stack should not be changed):

int elements ();

- Write a stack member function called print_ends that prints the top element in the stack and then the bottom element in the stack with a space between them. It should not print a label, just the values from the stack. void print_ends ();

- Write a stack member function called change_bottom that sets the element at the bottom of the stack to its parameter: void change_bottom (char new_item);

Make the following changes to the application file (there are comments to help instruct you where to place each item):

Declare a stack called stacky.

- Push the characters F, L, O, W, E, and R on the stack.

- Call your print_ends function to print the stack.

- Change the bottom of the stack to ‘P' using change_bottom

- Pop the stack.

- Call your print_ends function to print the stack

- Print the number of elements in the stack (using the elements function to find the number of elements). Plug the call into the already present cout.

Reference no: EM132189960

Questions Cloud

Determine the bond price trade at today : Investors now require a 15% return to invest in the bonds. Determine the bond price trade at today right after the latest coupon payment
Should center on organizational communication and strategies : Should center on organizational communication and strategies that determine what, when, and how information is communicated within the organization
Calculate the amount of profit in swiss francs : Assume you have an initial SF12,000,000. Can you make a profit via triangular arbitrage? If so, show steps and calculate the amount of profit in Swiss francs
What rbac functionality would be difficult to implement : Your shop uses a UNIX system. You do not have a system with an implementation of RBAC available to you.
What is the index of the top element of the stack : What is the index of the top element of the stack. What is the index of the bottom element of the stack?
Overall profitability of the industry to improve : All of Southwest airlines competitors adopt the same jet engine maintenance programs as part of their strategy to improve.
Find the projects IRR : Question - A project costs 1000 dollars today and returns $500 in Year 1, $300 in Year 2, and $400 in Year 4. Find the projects IRR
Central planning in planned economies : How do prices and wages perform functions similar to 'central planning' in planned economies?
What is an alternative to using permissions : How effective are Unix permissions for allowing shared file access? What is an alternative to using permissions?

Reviews

Write a Review

Computer Engineering Questions & Answers

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd