Question

In a main function, use a menu system to exercise the usage of all of these...

In a main function, use a menu system to exercise the usage of all of these functions. The
menu should be in a continuous loop to allow the user to test all functions.


1. Write a recursive function for the following function that does not use recursion.
So, we want to implement the same function using recursion.
void what(int x)
{
while (x > 0)
{
cout<<”say what\n”;
x--;
} //x > 0
}//what

2. Write a recursive function that accepts two arguments, a1 and a2. The function
should return the value of a1 times a2. You should use the knowledge that
multiplication cane be performed by a set of additions. For example,
5 * 3 = 3 + 3 + 3 + 3 + 3

3. Write a recursive function that finds the number of occurrences of a specified
letter in a string. The function accepts a string and a character to look for. For
example, if we call the function countMe(“hello”, ‘l’); the function will return 2.
Ask the user for a string and a character to look for and display the number of
occurrences of the character. If we call the function countMe(“hello”, ‘z’); the
function will return 0.

4. Write a recursive function that returns the value of the following sum:
Sum = 1 + 1/2 + 1/3 + … + 1/n

The program should ask the user for an integer (n) and display the sum So, if n is
entered as 3, the returned value will be 1.8333 = (1+1/2+1/3) . If n is entered as 5,
the returned value be 1+1/2+1/3+1/4+1/5 = 2.2833

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program 1

#include <iostream>
using namespace std;
void what(int);
int main()
{
int x;
cout<<"Enter a number:";
cin>>x;
what(x);

return 0;
}
void what(int x)
{
if(x>0){
cout<<"say what\n";
what(x-1);
}
}

Screenshot of the program 1:

Program 2:

#include <iostream>
using namespace std;
int accepts_two(int,int);
int main()
{
int x,y,result;
cout<<"Enter two numbers:"<<endl;
cin>>x;
cin>>y;
result=accepts_two(x,y);
cout<<"Result="<<result;
return 0;
}
int accepts_two(int x,int y)
{
int static r=0;
if(x>0){
r=r+y;
accepts_two(x-1,y);
  
}
return r;
}

Screenshot of the program 2:

Program 3 :

#include <iostream>
using namespace std;
int countMe(char *,char);
int main()
{
int result;
result=countMe("hello", 'l');
cout<<"Result="<<result;
return 0;
}
int countMe(char *s,char x)
{
int static count=0,r=0;
if(s[r]!='\0'){
if(s[r]==x)
count++;
r++;
countMe(s+1,x);
}
return count;
}

Screenshot of the program 3:

Program 4:

#include <iostream>
using namespace std;
double sum(double);
int main()
{
double result;
int n;
cout<<"Enter n value:";
cin>>n;
result=sum(n);
cout<<"Result="<<result;
return 0;
}
double sum(double n)
{
double static s=0;
if(n>0){
s=s+(1/n);
sum(n-1);
}
return s;
}

Screenshot of the program 4:


Add a comment
Know the answer?
Add Answer to:
In a main function, use a menu system to exercise the usage of all of these...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • c++ Write a function that uses recursion to raise a number to a power. The function...

    c++ Write a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the function in a program. Write a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • This Python module will be menu driven and use conditions to check user input in order...

    This Python module will be menu driven and use conditions to check user input in order to determine which operation to perform based on the user’s menu selection. The output should look like the attached example output. The menu will have the following selections (NOTE: all menu selections by the user should not be case sensitive) : 1. ‘CC’ – Character Count a. This operation will prompt the user for a string b. The number if characters will be counted...

  • C++ Using Recursion We are going to create a (looping) menu that accesses functions that use...

    C++ Using Recursion We are going to create a (looping) menu that accesses functions that use recursion. The function headers and a description will be provided. You are responsible for defining the functions. Ensure that they each contain a base case they reach that doesn’t have a recursive function call, otherwise it’s an infinite loop! Functions must be implemented with recursion. int Factorial(int arg) Returns arg! (4! Is 4 * 3 * 2 = 24) Base case is Factorial(1) returning...

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

  • Write a program with a function that accepts a string as an argument and returns a...

    Write a program with a function that accepts a string as an argument and returns a copy of the string with the first character of each sentence capitalized. For instance, if the argument is “hello. my name is Joe. what is your name?” the function should return the string “Hello. My name is Joe. What is your name?” The program should let the user enter a string and then pass it to the function. The modified string should be displayed.

  • 1. Recursion is ususally where a function calls itself; (another example of recursion is where function...

    1. Recursion is ususally where a function calls itself; (another example of recursion is where function A calls function B, and B calls C and C calls A, creating a loop in the calls). Some problems are most naturally solved recursively, such as writing the factorial function, or finding all the perumutations of a sequence, or checking if a string is a palindrome. Since those examples were done in class, here we will give you a toy example, which normally...

  • Objectives: Integer arithmetic, Functions, menus. Write a C++ program that displays the following menu of choices....

    Objectives: Integer arithmetic, Functions, menus. Write a C++ program that displays the following menu of choices. 1. Find the number of digits in an integer. 2. Find the nth digit in an integer. 3. Find the sum of all digits of an integer. 4. Is the integer a palindrome? 5. Quit Enter a choice: Page 1 of 4 For each of the choices (1, 3, 4), Read a positive integer number and call a function that processes the menu choice...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT