A continued fraction representation of a number is a representation c_0 + (1/c_1
ID: 3801725 • Letter: A
Question
A continued fraction representation of a number is a representation c_0 + (1/c_1 + (1/c_2 + (1/c_3 + ... (1/c_n-1) ...))). For example, 1 + 1/2 + 1/3 = 1.428571 Write a program that does the following: Asks a user to input a size n, and then input the numbers c_0, ..., c_n-1. The program should store the c_i in an integer array. Use a recursive function (see below) to compute the continued fraction for those numbers. Print the value of the continued fraction, with 12 digits to the right of the decimal point. All these steps should be within a continuation loop so the user can input and compute a number of continued fractions. To compute the continued fraction, write a recursive function continuedFraction. You will need to decide what the parameters and return type for this function will be. Here is an example of program input/output: Input number of values (max. of 100): 1 Input values: 4 Value of continued fraction: 4.000000000000 Input another continued fraction (y/n)? y Input number of values (max. of 100): 3 Input values: 1 2 3 Value of continued fraction: 1.428571428571 Input another continued fraction (y/n)? y Input number of values (max. of 100): 8 Input values: 3 7 15 1 292 1 1 1 Values of continued fraction: 3.141592653619 Input another continued fraction (y/n)? nExplanation / Answer
C++ Code:
#include<bits/stdc++.h>
using namespace std;
double continuedFraction(int a[],int n) //recursive function
{
if(n==1)
return double(a[0]);
else
return double(a[0]+(1/continuedFraction(a+1,n-1)));
}
int main()
{
char c;
int n,i;
double sum;
do{
cout<<"Input number of values(max.of 100): ";
cin>>n;
int a[n];
cout<<"Input Values: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
sum=continuedFraction(a,n); //calling recursive function
printf("Value of continued fraction : %0.12lf ",sum); //printing value upto 12 digits
cout<<"Input another continued fraction (y/n): ";
cin>>c;
}while(tolower(c)!='n'); //Check for proceed or not
return 0;
}
Output:
Input number of values(max.of 100): 1
Input Values: 4
Value of continued fraction : 4.000000000000
Input another continued fraction (y/n): y
Input number of values(max.of 100): 3
Input Values: 1 2 3
Value of continued fraction : 1.428571428571
Input another continued fraction (y/n): y
Input number of values(max.of 100): 8
Input Values: 3 7 15 1 292 1 1 1
Value of continued fraction : 3.141592653619
Input another continued fraction (y/n): n
Hope u understand!!!!!If not comment here..Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.