Thread: Recursion with C

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    15

    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. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The storing the value in array A[i] makes the problem one that using recursion to solve petty much useless.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    Last edited by stahta01; 04-12-2020 at 06:54 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    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[15];
        double x = 1.23;
        int size = sizeof a / sizeof a[0];
     
        f(a, x, x * x, size);
     
        for (int i = 0; i < size; ++i)
            printf("%2d. %8.5f\n", i, a[i]);
     
        return 0;
    }
    Last edited by john.c; 04-12-2020 at 08:20 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    15
    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]; 
      }
        
        }
    }
    Last edited by Chibisco; 04-13-2020 at 05:47 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Chibisco, do you intend to write your program in C or C++?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2019
    Posts
    15
    Quote Originally Posted by laserlight View Post
    Chibisco, do you intend to write your program in C or C++?
    I intend to use both

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote 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++.

    Quote 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.
    Last edited by laserlight; 04-13-2020 at 06:01 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2019
    Posts
    15
    Can you help me correct the memory standard to c++. I'm used to c, just starting to learn c++
    Last edited by Chibisco; 04-13-2020 at 06:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion
    By supaman in forum C Programming
    Replies: 1
    Last Post: 03-10-2006, 12:09 AM
  2. Recursion
    By dayknight in forum C++ Programming
    Replies: 4
    Last Post: 01-26-2006, 02:02 AM
  3. Recursion help
    By Cstudent2121 in forum C Programming
    Replies: 11
    Last Post: 12-12-2005, 12:40 PM
  4. Recursion
    By Axel in forum C Programming
    Replies: 14
    Last Post: 11-17-2005, 11:39 AM
  5. Recursion
    By scottmanc in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 03:53 PM

Tags for this Thread