Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment 2: Sub-functions and Function Handles The aim of this assignment is explore how sub-functions can be made available from a function file, via functional

Assignment 2: Sub-functions and Function Handles The aim of this assignment is explore how sub-functions can be made available from a function file, via functional handles. The aim is to implement a simple stack in a file (mystack.m), where the main function passes back the three stack manipulation functions. A stack is a last in first out" queue. The function file does not maintain any stack state, therefore the stack itself is passed in to functions, and then, when it's modified, the stack is returned. The file mystack.m contains the following functions: Inputs Outputs Description Function Name mystack Function Type Main function for the file. None push pop Main function that returns 3 handles to the stack functions peek mystack_push Sub-function stack stack value Pushes value onto the stack (location 1) mystack_pop Sub-function stack stack Pops value off the stack (i.e. value in array location 1). Should return an empty stack if there is only one element mystack peek Sub-function stack value Returns the top value from the stack (array location 1). If the stack is empty, it should not throw an error.

The code below shows a test case. The first call sets up the three stack functions so that they can be called. The variable stack is an array variable that holds the data. It is passed into functions, and also its modified value returned (from two of the functions). Implement a script that contains the following test code. (push, pop, peek] = mystack(); stack = 0 stack = push(stack, 100) >> Stack = 100 stack = push(stack, 200) >> Stack = 200 100 peek (stack) >> ans = 200 stack = pop(stack) >> Stack = 100 peek (stack) >> ans = 100

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Probability And Statistics

Authors: William Mendenhall, Robert Beaver, Barbara Beaver

14th Edition

1133103758, 978-1133103752

Students also viewed these Programming questions