Question

In: Computer Science

Write functions for each of the following problems. Each problem should be solved by writing a...

Write functions for each of the following problems. Each problem should be solved by
writing a recursive function. Your final program should not have any loops in it.


(a) Write a function that uses recursion to raise a number to a power. The function should
take two arguments, the number to be raised to the power (floating point) and the
power (a non-negative int). (10
Points)
(b) Write a boolean function named isMember that takes two arguments: an array of type
char and a value. It should return true if the value is found in the array, or false if the
value is not found in the array. (10 Points)

(c) A palindrome is any word, phrase, or sentence that reads the same forwards or
backwards. Here are some palindromes:
Level
Civic
Pot top
A man a plan a canal Panama

note:your final programme must not have any loops, do it with recursion, the language must be c++, and kindly explain it in simple words in the form of comments before each part

Solutions

Expert Solution

To be as simple as possible we are designing functions sensitive to the input.

for example:

  • power function does not handle the case if input is negative.
  • isPalindrome function must provide length to avoid null character '\0' and also case sensitive and doesn't handle the case of whitespaces.
#include <iostream>

using std::cout;

float power(float a, int n){
  float temp;                 //to hold intermidiate results
  if(n == 0) return 1;        // base case

  temp = power(a, n/2);

  if(n % 2 == 0)              // if power is even
    return temp * temp;
  else{                       // if power is odd
    if(n > 0)
      return a * temp * temp;
    else
      return (temp * temp) / a;
  }

}

bool isMember(char a[], char val, int i, int l){
  if (i == l)                 // array is traversed completely
    return false;
  if (a[i] == val)            // element to be search is found at index i
    return true;
  return isMember(a, val, i + 1, l); // recursively search in remaining array
}

bool isPalindrome(char a[], int i, int l){

    if (i == l)                 // base case: If only one character is there
    return true;

    if (a[i] != a[l])           // if first and last characters do not match
    return false;               // string cannot be a palindrome

    if (i < l + 1)              // recursively check if middle array is palindrome
    return isPalindrome(a, i + 1, l - 1);

    return true;
}

int main(){
  float a = 2.5;
  int n = 3;

  char arr[] = {'h','e','l','l','o', '\0'};
  char pal[] = {'p','o','t','t','o','p','\0'};
  int p = sizeof(pal) / sizeof(pal[0]);
  int l = sizeof(arr) / sizeof(arr[0]);

  cout << power(a, n) << std::endl;

  cout << isMember(arr, 'i', 0, l) << std::endl;

  cout << isPalindrome(pal, 0, p - 2);

  return 0;
}


Related Solutions

Recursion practice Write a Java program with functions for each of the following problems. Each problem...
Recursion practice Write a Java program with functions for each of the following problems. Each problem should be solved by writing a recursive function. Your final program should not have any loops in it. All of your solutions should be in a single .java file. The main function of the file should demonstrate each of your solutions, by running several tests and producing the corresponding outputs. Write a recursive method to 1. calculate power of a give number 2. multiply...
Explain how to solve the following problems step by step, andreason it should be solved...
Explain how to solve the following problems step by step, and reason it should be solved that way:1) Find domain for each problem and explain in brief in each case. How do you find the domain of these problems without using a graph (don't state the problem only)?  Explain this: why is there a difference between in the domain of an even and an odd indexed radical? Why the domain of a function with a square root in the denominator IS...
Please formulate and solve each of the following problems. For each problem, you should include the...
Please formulate and solve each of the following problems. For each problem, you should include the final SOLVER printout (either your final spreadsheet or an answer report), as well as (1) clear and precise definitions for all decision variable; (2) your objective function indicating whether it is to be maximized and minimized; (3) all constraints, including non-negativity and integrality (if necessary); and (4) what the optimal decision is (in words) and what outcome will be produced. A manufacturer of stereos...
In this homework assignment, you will be writing three `void` functions. These functions each take a...
In this homework assignment, you will be writing three `void` functions. These functions each take a single integer parameter. The functions accomplish the following tasks. * The first function prints out the sum of the numbers from 1 up to and including the number passed to it as an argument. * The second function prints out the sum of the even numbers from 2 up to and including the number passed to it as an argument. * The third function...
For each of the following problems, write up your solutions using the 4-step problem solving procedure...
For each of the following problems, write up your solutions using the 4-step problem solving procedure (get started, plan, execute, evaluate) that is detailed in the problem set rubric. Please include sketches of the problem and words describing the steps, not just the math. 3. Design a current loop that, when rotated in a uniform magnetic field of strength 0.1 T, will produce an emf of ε=ε0 sinωt, where ε0 =110V and ω=120π rad/s.
Implement each of the following functions and write a basic main() function that tests each. For...
Implement each of the following functions and write a basic main() function that tests each. For convenience, you should be able to find this starter class under Mimir's assignment 4 starter code. Do not change the name, parameters, or returns of any of these functions or of the name of the class itself. There is also no need in this assignment for any global variables. You are strongly encouraged to use your solution for some of these functions in others...
1. Writing and Balancing Equations. Write the balanced chemical reaction for each of the following. On...
1. Writing and Balancing Equations. Write the balanced chemical reaction for each of the following. On the line, identify the kind of reaction. Your choices are: Combination, Combustion, Decomposition, Neutralization, Oxidation/Reduction, Precipitation. PLEASE MAKE SURE YOU DO NOT FORGET TO IDNETIFY THE KIND OF REACTION ___________________ a) Mg(s) combines with nitrogen gas. ___________________ b) When solid potassium chlorate is heated, it decomposes to form solid potassium chloride and oxygen gas. ___________________ c) The complete combustion of acetone, C3H6O(l), a compound...
28. Fluid Momentum problems are solved with the aid of two diagrams of such problems. Explain,...
28. Fluid Momentum problems are solved with the aid of two diagrams of such problems. Explain, list or define items which would be included on each of the two diagrams required, and describe or show features common to both diagrams. Why are these two drawings typically kept separate? 29. Propellers and turbines are similar in that both act as screws. Attribute each of the following to Props, Turbines, or Both: a. __________Average velocity through the blades, (V1 + V2)/2 b....
What are the problems that occur with concrete that can be solved by nanotechnology?
What are the problems that occur with concrete that can be solved by nanotechnology?
Write the recurrence for each of the following two problems. Try to come up with the...
Write the recurrence for each of the following two problems. Try to come up with the solution using your understanding. 1) Maximal subarray problem 2) Weighted interval selection problem
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT