# Thread: Recursion with C

1. ## Recursion with C

Hello, good day all.

I have been reading recursion for sometimes now but I can't still find a way to solve my problem. I want to store the values of this sequence (1/X^2, 1/X^3, 1/X^4.........) in an array A[i] until the 15th index with recursion. X is an input.

Thank you 2. The storing the value in array A[i] makes the problem one that using recursion to solve petty much useless.

Tim S. 3. But, if you pass the value 15, the pointer to the start of the array, and the value of X to a recursive function it could be done.

Note: 15 is my guess on the value it might be 14 or 16 instead.

Tim S. 4. Maybe something like this.
Code:
```#include <stdio.h>

void f(double *a, double x, double xx, int n)
{
if (n > 0)
{
*a = 1.0 / xx;
f(a + 1, x, xx * x, n - 1);
}
}

int main()
{
double a;
double x = 1.23;
int size = sizeof a / sizeof a;

f(a, x, x * x, size);

for (int i = 0; i < size; ++i)
printf("%2d. %8.5f\n", i, a[i]);

return 0;
}``` 5. john.c code looks good to me. I was trying to avoid the "xx" variable; but, his use seems better than my way to avoid it.
I was returning the xx value from the R. function; but, that required more complex logic inside the R. function.

Tim S. 6. 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;

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];
}

}
}``` 7. Chibisco, do you intend to write your program in C or C++? 8. Originally Posted by laserlight Chibisco, do you intend to write your program in C or C++?
I intend to use both 9. Originally Posted by Chibisco
I intend to use both
That's possible (by compiling separate translation units, or perhaps by the use of extern "C"), but you're presently not doing so: you're writing a program that might be valid C++, but is certainly not valid C.

What's stopping you from picking either C or C++? It doesn't look like a program to "store the values of this sequence (1/X^2, 1/X^3, 1/X^4.........) in an array A[i] until the 15th index with recursion" requires it to be written in a mixture of C and C++. If you do want to use C++ with the memory management facilities from standard C, that's certainly an option, though not an advisable one unless you're really careful in the face of exceptions in C++. Originally Posted by Chibisco
however, it is mandatory I use dynamic memory for this.
Why? With a maximum of 15 values, dynamic memory allocation is likely to be unnecessary. If you really wanted, you could just "dynamically" allocate 15 values up front. 10. Can you help me correct the memory standard to c++. I'm used to c, just starting to learn c++ Popular pages Recent additions #### Tags for this Thread

1/x^2, 1/x^3, 1/x^4, recursion, values 