Question

In: Computer Science

In c++ Write a recursive driver function that will replace each of the odd values in...

In c++ Write a recursive driver function that will replace each of the odd values in a stack with the cube of the value.

Solutions

Expert Solution

#include <iostream>
#include <stack>
using namespace std;

//Recursive function

int oddv(int n) {
   if (n % 2 == 0)
   return n; //If even returns the same number
else
   return n*n*n; //if odd returns the cube of the number
}

//Displays the values in the stack
void showstack(stack <int> s)
{
while (!s.empty())
{
cout << '\t' << s.top();
s.pop();
}
cout << '\n';
}

//Driver class
int main() {
//Initializing the stack
stack <int> s;
stack <int> temp;

//Adding the values to the stack
s.push(10);
s.push(30);
s.push(20);
s.push(5);
s.push(3);
//Original Stack is
cout << "The stack is : ";
showstack(s);  
  
  //Storing the recursive function values in temporary  stack
while (!s.empty())
{
temp.push(oddv(s.top()));
s.pop();
}
//pushing back to the original stack
while (!temp.empty())
{
s.push(temp.top());
temp.pop();
}
//Printing the Stack
cout << '\n';
cout << "The stack is : ";
showstack(s);
return 0;
}


Related Solutions

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.
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line. Function #2 Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work...
Write a recursive function that converts Old Roman Numeral to base 10 values.
C++ program Write a recursive function that converts Old Roman Numeral to base 10 values. Print out the Old Roman Number and it base 10 equivalents. You will read all the digits of a Roman number into an array of characters. You will process each Roman digit in the array using ONLY POINTERS. Putting a ‘\0’ at the end of the input characters allows you to print the string easier. Use the following input to test your function:    DVIII, MMMDCCCCLXXXXIIII,...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that...
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that computes the sum of the digits in an integer. Use the following function header: int sumDigits(int n) For example, sumDigits(234) returns 2 + 3 + 4 = 9. Write a test program that prompts the user to enter an integer and displays its sum.
Write a recursive function in C++ that creates a copy of an array of linked lists....
Write a recursive function in C++ that creates a copy of an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this can equal 10) }
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not. #5: Write a recursive function 'void reverse(string &word)' that reverses the given input string. string name = "damian"; reverse(name); cout << name << endl; //should display "naimad". #7: Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n. cout << numTwos(2170) << endl; //...
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest...
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest value in the array. If the array is empty, return the “end” pointer ·and takes as parameters: (1)   a pointer to double. The pointer is the address of the start of an array, (2)   the “end” pointer to the address after the array (3)   and the address of the smallest value seen so far ·Write main() to test this function – try a case where the array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT