Question

In: Computer Science

Please solve this question in C++ language using recursion. Q (1) Write functions for each of...

Please solve this question in C++ language using recursion.

Q (1) 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.

  1. 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)
  1. 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)
  2. 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

Write a boolean function that determines if a string argument is a palindrome. The function should return true if the argument reads the same forwards and backwards. Your function should ignore spaces and be case-insensitive. Assume the input is just letters and spaces.                                                                                         (20 Points)

Solutions

Expert Solution

Part a.

/* This function calculated recursively the power of b raised by exponent e i.e. b^e.
 * Full explanation is provided inside the function. Here I give an example how its working
 * Suppose we want to calculate 3^6 then first we calculate 3^3 and then we calculate 3^1 then we calculate 3^0
 */
float power(float b, int e) {
        // if expoenent is 0 then power of any number is 1 so we return 1
        if(e == 0)
                return 1;
        // now we calculate recursively half of the power
        float x = power(b, e/2);
        // if e is even then power is multiplication of x and x because we can write 3^4 = 3^2 * 3^2.
        // but if e is odd then it is b*x*x because for 2^5 we can write 3^5 = 3^2 * 3^2 * 3
        if(e % 2 == 0)
                return x * x;
        else
                return b * x * x;
}

Part b.

/* This function takes pointer to character array as input and the character which needs to be found
 * First we check if the array is empty using strlen() function. If it is empty then we return false as characer could not be found
 * If arr[0] is equal to c then we return true
 * Else we recurse and starts finding by passing position to one plus the previous array position.
 * Example if arr = {a, e, i} and we need to find 'e' then first it checks arr[0] == 'e' or not. as it is not true we recurse for arr+1 i.e. arr is now [e, i]
 * as arr[0] = 'e' and we need to find 'e' and we have found so we return true
 */
bool isMember(char *arr, char c) {
        // if length is 0 then we return false
        if(strlen(arr) == 0)
                return false;
        // if 0th position has the character then we return true
        if(arr[0] == c)
                return true;
        // else we recurse for arr+1 i.e. next position in the array
        return isMember((arr+1), c);
}

Part c.

/* This function takes 1 argument string s - string which needs to be checked if it is palindrome or not
 * We first check the length of the string s. If it is 0 or 1 then it is palindrome. This is our base condition
 * Then we check if 0th or last position has white space or not if it has white space then we recurse for substring
 * we check s[0] and s[len-1] if any of them is an upper case alphabet we add 32 to it to make it lower case because ASCII value of 'A' is 65 and that of 'a' is 97.
 * if s[0] == s[len-1] after conversion of uppercase to lowercase then we recurse for substring(0, len-1)
 * if they are not equal then we return false
 */
bool isPalindrome(string s) {
        int len = s.length();
        // if len is 1 or 0 then return true
        if(len <= 1)
                return true;
        // if 0th position has white space then recurse for substring from 1st position
        if(s[0] == ' ')
                return isPalindrome(s.substr(1, s.length()));
        // if last position has white space then recurse from 0 to len-1
        if(s[len-1] == ' ')
                return isPalindrome(s.substr(0, len-1));
        // if 0th or (len-1)th position has uppercase alphabet convert it into lower case by adding 32 to it
        if(s[0] >= 'A' && s[0] <= 'Z')
                s[0] += 32;
        if(s[len-1] >= 'A' && s[len-1] <= 'Z')
                s[len-1] += 32;
        // if first and last element are not equal we return false
        if(s[0] != s[len-1])
                return false;
        // else we recurse for substring between first and last element
        return isPalindrome(s.substr(1, len-2));
}

Related Solutions

(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an...
(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an integer n and returns 1 if n is a perfect number, otherwise it will return 0. If the sum of a number’s proper divisors are equal to the number, than the number is called a perfect number. For example, 6 is a perfect number: 6=1+2+3.
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the...
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the Weightlifting Walrus is training for a contest where it will have to lift 1000 kg. Wallace has some weight plates lying around, possibly of different weights, and its goal is to add some of the plates to a bar so that it can train with a weight as close as possible to 1000 kg. In case there exist two such numbers which are equally...
Please write code for C language Problem: Write a couple of functions to process arrays. Note...
Please write code for C language Problem: Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an...
Write a c program to calculate the factorial of a number using recursion    [8] Question five...
Write a c program to calculate the factorial of a number using recursion    [8] Question five Write a stack algorithm to POP an item                                                         [6] What does FRONT and REAR signify in a queue?                                                                 [6] Write an algorithm for a dequeue operation                                                                       [8]
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
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...
Solve the following question by using python language In range of 1-10000 if the numbers are...
Solve the following question by using python language In range of 1-10000 if the numbers are divisible by n1 increase count by 1 , if the number is divisible by n2 increase count by 2, and if the number are divisible by n3 increase the count by 3 if none of the above conditions match for the number, increase count by the number. n1=10 +17 n2=50 +21 n3=100 +9
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...
can you please solve this for me. please write a paragraph for each question and explain...
can you please solve this for me. please write a paragraph for each question and explain your reasoning. 1) assume that you are a full-time worker earning $10/hour, $80/day, $400/week, $20000/year. would you be willing to quit your jobs or keep working if the tax rate was 10%? 20%? 30%? 40%? what do you think is the best tax rate? what do you think is the best tax rate? 2)the state of California has decided to increase funding for public...
write a program that will print a truth table for p ^ ~q. Using C++ please.
write a program that will print a truth table for p ^ ~q. Using C++ please.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT