Thank you John.
The value of the 1st index is constant 1 then subsequently, others follows the sequence.
however, it is mandatory I use dynamic memory for this.
Also this condition must hold (AL – AL – 1) < ep or L < 15
It will be good to incorporate the condition in the recursion function but I can't do it, hence I used it at the output function.
I tried using your code to achieve my objective but have issues with the output. Please help me look at it.
the value of X is 0.9, ep is 0.8
Below is the full code.
Code:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;
void input(ifstream&, double, double);
void recus(double*, double, double, int);
void output(ofstream&, double*, double, int);
int main(void)
{
ofstream of;
ifstream ip;
double X=0.0, ep=0.0;
double* A;
A=(double*)malloc(sizeof(double));
int size = sizeof A / sizeof A[0];
ip.open("inputf.txt");
of.open("outputf.txt");
input(ip, X, ep);
recus(A, X, X*X, size);
output(of, A, ep, size);
cout << size;
free(A);
ip.close();
of.close();
return 0;
}
void input(ifstream& ff, double X, double ep)
{
if (ff.is_open())
{
cout << ("Files opened successfully");
}
else
{
cout << ("error opening files");
exit(0);
}
while(!ff.eof())
{
ff >> X >> ep;
}
}
void recus(double* A, double no, double nom, int L)
{
if (L==0)
{
*(A+0) = 1;
}
else if (L > 0)
{
A=(double*)realloc(A,(L*sizeof(double)));
*A = 1.0 / nom;
recus(A + 1, no, nom * no, L - 1);
}
}
void output(ofstream& of, double* A, double ep, int L)
{
for (int i = 0; i < L; ++i)
{
if ( A[i]-A[i-1] < ep || L < 15)
{
cout << i << A[i];
of << A[i];
}
}
}