Question

In: Computer Science

I have the following question: Write a recursive function to find the Nth element from the...

I have the following question:

Write a recursive function to find the Nth element from the top of a stack. For example, if N is 3 the function should return the third element in the stack. Use the following header:

template <class Type> Type getNth( stack<Type> & s, int n)
Although your function may push and pop values from the stack, it must eventually leave the stack in its original state. Your function may NOT use a help stack or any other data structure. You can assume n is a positive integer and that the stack contains at least n elements.

1) I'd like to know what is meant by a "help stack", please and how should I avoid using it?

2) Also, if I used while and for loops would that fine or is that considered a data structure?

If you could assist me in writing this code too, it will be appreciated

Solutions

Expert Solution

1. By help stack , it means that we cannot use any other stack object for finding the nth element except the stack object received by the function

2. No while and for loops are not considered as data structure. These are looping statements. But in recursion we do not use loops. The work of loops is achieved by using recursion

// C++ recursive function to find the Nth element from the top of a stack.

// Assuming stack has a method Pop() that removes and returns the top element of the stack
// Assuming stack has a method Push() that inserts the element at top of the stack
template <class Type>
Type getNth(stack<Type> & s, int n)
{
   // if we have not reached the nth element
   if(n > 1)
   {
       T top = s.Pop(); // remove and get the current top element of the stack
       T a = getNth(s,n-1); // call the getNth with parameters s and n-1
       s.Push(top); // push the current top back to stack
       return a; // return the element returned by getNth
   }
   else // if we have reached the nth element
   {
       T top = s.Pop(); // remove and get the top element of the stack
       s.Push(top);// push the top element back to the stack
       return top; // return this top element
   }
}


Related Solutions

PYTHON: Write a recursive function named linear_search that searches a list to find a given element....
PYTHON: Write a recursive function named linear_search that searches a list to find a given element. If the element is in the list, the function returns the index of the first instance of the element, otherwise it returns -1000. Sample Output >> linear_search(72, [10, 32, 83, 2, 72, 100, 32]) 4 >> linear_search(32, [10, 32, 83, 2, 72, 100, 32]) 1 >> linear_search(0, [10, 32, 83, 2, 72, 100, 32]) -1000 >> linear_search('a', ['c', 'a', 'l', 'i', 'f', 'o', 'r',...
Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
From the following mySql query create a function that returns a nth value of a table:...
From the following mySql query create a function that returns a nth value of a table: SELECT name, (SUM(price * quantity)) AS TotalPrice FROM Items NATURAL JOIN Inventory GROUP BY name ORDER BY (TotalPrice) DESC LIMIT 1, OFFSET 1; Shema: Inventory(name: String,item:String,quantity:integer) Items(item: String, brand: String, price:double)
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
Problem 3: Minimum In this problem, we will write a function to find the smallest element...
Problem 3: Minimum In this problem, we will write a function to find the smallest element of a list. We are, in a sense, reinventing the wheel since the min() function already performs this exact task. However, the purpose of this exercise is to have you think through the logic of how such a function would be implemented from scratch. Define a function named minimum(). The function should accept a single parameter named x, which is expected to be a...
Write a recursive algorithm replace (start) to replace the value of each element of A with...
Write a recursive algorithm replace (start) to replace the value of each element of A with that of the next element in A. A is a singly linked list.
Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will...
Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will take 2 inputs: 1) the first input will be a row vector, c, containing the coefficients of the polynomial, starting with the coefficient of the highest - degree term; 2) the second input will be a scalar, x, which is a real number at which the polynomial will be evaluated. The function's only output, y, will be the scalar value of the polynomial computed...
MATLAB QUESTION: I have to write a function that pretty much cleans number. these are the...
MATLAB QUESTION: I have to write a function that pretty much cleans number. these are the instructions Inputs: number1 - a 1d of array of number, size doesn't matter outputs: number2 - all number in number1 that are 0 need to be set to nan and return via number2. data - storing the number of 0's in number1 that pretty much are being set to nans here's the code: function [number2, data] = cleanNumber(number1) %add your code below.. end code...
a. Find the​ nth-order Taylor polynomials of the given function centered at the given point​ a,...
a. Find the​ nth-order Taylor polynomials of the given function centered at the given point​ a, for n = ​0, ​1, and 2. b. Graph the Taylor polynomials and the function. f(x) = cos x, a = 2pi / 3
Determine if each of the following recursive definition is a valid recursive definition of a function...
Determine if each of the following recursive definition is a valid recursive definition of a function f from a set of non-negative integers. If f is well defined, find a formula for f(n) where n is non-negative and prove that your formula is valid. f(0) = 1, f(n) = -f(n-1) + 1 for n ≥ 1 f(0) = 0, f(1) = 1, f(n) = 2f(n-1) +1 for n ≥ 1 f(0) =0, f(n) = 2f(n-1) + 2 for n ≥...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT