G++ Programming Goals: Analyzing recursive functions and practicing writing func
ID: 3803222 • Letter: G
Question
G++ Programming
Goals: Analyzing recursive functions and practicing writing functions
Create a new project in Visual Studio. Analyze the code (below) to determine what series the recursive function calculates. Modify the introductory comments by putting the series in place of the first ______________ plus add some test data and the expected result for the second ______________
Modify main by adding an output statement that tells the user what series is being calculated.
Modify the comments for the recursive function by adding what series is calculated
Write comments for a second function that will perform the same calculation using repetition. Write the code for the second function that will perform the same calculation using repetition (do not use any built-in functions) after the recursive function. Add the function declaration (prototype) for your new function before main. Add a function call to your new function in main. Add and output statement in main to output the result of the call to your new function.
Program:
The program will calculate the series ____________________________________ using a recursive function and a function that uses repetition. input: a floating point value and and integer value output: the result of the series processing: Prompt the user to enter the floating point value and the integer value. Call the function that uses recursion and output the result in main. Call the function that uses repetition and output the result in main. test data: _________________________________ */
# include <iostream> using namespace std;
double RecursiveFunct(double c, int i); //put function declaration (prototype) for your function here
int main() {
double abc, resulta, resultb; int def;
//output what series will be calculated
cout << "Enter a floating point number "; cin >> abc;
cout << "Enter a whole number greater than or equal to zero. "; cin >> def;
while (def < 0) {
cout << "The whole number MUST be greater than or equal to zero
zero! ";
cout << "Enter a whole number greater than zero. "; cin >> def;
}
resulta = RecursiveFunct(abc, def); cout << "The result of the series calculated using recursion is "
<< resulta << ". ";
// add function call to repetitious function here store the returned value
in resultb
//add output statement to state the result of the function using
repetition
return 0; }
/* Recursive funtion to calculate the series _______________________________________ input: a floating point number and a whole number is passed to the function. The whole number has been confirmed to greater than or equal to zero output: result of the series processing: use recursion to call the function again as long as the whole number is greater than 1
*/ double RecursiveFunct(double x, int n) {
double ans; if (n == 0) { ans = 1; } else {
ans = 1 + x * RecursiveFunct(x, n - 1);
} return ans;
}
//put comments for your function that uses repetition here and your function definition
Explanation / Answer
# include<iostream>
using namespace std;
double RecursiveFunct(double c, int i);
double RepetitiveFunct(double c, int i);
int main() {
double abc, resulta, resultb; int def;
//output what series will be calculated
cout << "Enter a floating point number "; cin >> abc;
cout << "Enter a whole number greater than or equal to zero. "; cin >> def;
while (def < 0) {
cout << "The whole number MUST be greater than or equal to zero! ";
cout << "Enter a whole number greater than zero. "; cin >> def;
}
resulta = RecursiveFunct(abc, def);
cout << "The result of the series calculated using recursion is "<< resulta << ". ";
resultb = RepetitiveFunct(abc, def);
cout << "The result of the series calculated using repetition is "<< resultb << ". ";
return 0;
}
/* Recursive funtion to calculate the series (1+x*(1+x*(1+x*(1+x*(1+x))))) n times
input: a floating point number and a whole number is passed to the function. The whole number has been confirmed to greater than or equal to zero
output: result of the series
processing: use recursion to call the function again as long as the whole number is greater than 1
*/
double RecursiveFunct(double x, int n) {
double ans; if (n == 0) { ans = 1; } else {
ans = 1 + x * RecursiveFunct(x, n - 1);
}
return ans;
}
/* Repetitive funtion to calculate the series (1+x*(1+x*(1+x*(1+x*(1+x))))) n times
input: a floating point number and a whole number is passed to the function. The whole number has been confirmed to greater than or equal to zero
output: result of the series
processing: use repetition to call the function again as long as the whole number is greater than 1
*/
double RepetitiveFunct(double x, int n) {
double ans=0;
for(int i=0;i<=n;++i) {
ans=1+(x*ans);
}
return ans;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.