Thread: Recursive functions

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    30

    Recursive functions

    Hello guys,i'm new to c programming and programming in general.

    I have a question about recursive functions .

    My questions are(so far)(with reference to tutorialspoint.com about:recursion)

    1.Why this isn't an infinte loop?
    2.What is the original value of i when the programme starts?
    3.Is there any problem if i write the main() function 1st and not the factorial() function?

    This is the code tutorialspoint.com provides:

    Code:
    include<stdio.h>
    int factorial(unsigned int i) 
    {
    
       if(i <= 1) 
    {
          return 1;
       }
       return i * factorial(i - 1);
    }
    
    int  main() 
    {
       int i = 15;
       printf("Factorial of %d is %d\n", i, factorial(i));
       return 0; 
    }
    Output:Factorial of 15 is 2004310016

    Also i would like some help on my programme about recursive functions:

    This is the excercise:

    Write a RECURSIVE functions that checks if in an array that consists of ten [10] there is on quartet of integer numbers A,B,C,D so that A+D=B+C

    For example, the array 5 10 20 21 31 4 3 9 contains the integers 10 20 21 31 that meet the requirements

    Your recursive function must print a message to point whether the array input contains a suitable quartet of numbers, the pointer of the 1st number of the quartet, and the 4 numbers.Your recursive function must return the pointer of the 1st number of the quartet.In case of failure, a proper message must be printed and your function must return -1;

    YOU ARE NOT ALLOWED TO USE AND LOOPS OR GLOBAL/STATIC VARIABLES.

    ______

    This is my code so far(neither built or debugged as i know i have many mistakes that i can't spot )

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int quadruple(int i)
    {
        int j=0,a[10];
        int *ptr;
        if (j>7)
        {
            return -1;
            printf("There are no numbers so that A+D=B+C is true");
        }
        else if ((a[j+1]+a[j+4])=(a[j+2]+a[j+3]))
        {
            ptr=&a[j+1];
            printf("There is a suitable quartet of numbers");
            printf("The numbers are:%d %d %d %d", a[j+1],a[j+2],a[j+3],a[j+4])
            printf("The position of the 1st digit is :%d", ptr);
            return ptr;
            j++;
        }
        return quadruple(i, a[10]);
    
    
    }
    
    
    
    
    int main ()
    {
        int i=10,a[10];
        quadruple(i, a[10]);
        return 0;
    }
    Pretty confused here guys.


    Sorry for any grammar,vocab.,syntax mistakes.English is not my native lang.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Just looking at the first example:

    a) It's not an infinite loop because you have what is called (most commonly these days) a base case that stops the recursive calls.
    b) The answer is not correct (presumably because of integer overflow)

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    30
    Hmmm ,thanks Hodor!

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by George Ioannou View Post
    Hmmm ,thanks Hodor!
    Ok, great. So you understand now. The thread can be closed?

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    30
    i still need help on my project

    and can you point out the base case...i thought i got it at first but i don't now
    Last edited by George Ioannou; 11-25-2015 at 09:12 AM.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    You don't know why it doesn't infinitely call itself?

    What does

    Code:
    if(i <= 1) { return 1; }
    do?

    Where is the recursive call?

    Edit: there is not much point in moving onto your specific problem until you can understand what's actually going on Grab a piece of paper and trace through the initial example by hand. See what happens if factorial is initially called with 1, 2, 3 and 4 and you'll probably "get it"
    Last edited by Hodor; 11-25-2015 at 09:47 AM.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    For some good information on a problem just like yours, check out this link on Recursive Functions.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Functions
    By jack999 in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2006, 01:28 PM
  2. Help Recursive functions
    By Alawamia in forum C++ Programming
    Replies: 6
    Last Post: 01-23-2006, 09:58 AM
  3. Recursive Functions
    By zz3 in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2004, 08:40 AM
  4. recursive functions
    By doug in forum C Programming
    Replies: 1
    Last Post: 02-24-2002, 10:54 AM
  5. Recursive Functions
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-10-2001, 07:47 PM