Thread: Recursive concept

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    Recursive concept

    Hi guys; sorry in advance for posting this thread but it's harassing me whenever I code in a recursive and I believe here's the place to learn and post my knowledge professionally.
    Lets take an example the quickSort .. generally the recursion rule I succeed of writing it..but still not make sense for me ..
    Lets say I have case that QuickSort(int p, int r) which p,r is the boundaries of the array, p is the first index, r is the last index..
    Lets say I got case QuickSort(9,8) logically it's not make sense and something going wrong here over the boundaries because p=9,r=8 and that's not allowed.
    So here I'm stuck in; what should I return in the case?! here's exactly my problem; how to decide here which kind of "return" to return it?

    Why should I have consider this case? because lets say I have QuickSort(1,8) I may get in the recursive calls of QuickSort(1,8) a case that QuickSort(9,8).. so I should consider this case with something to be returned once occured..

    Thanks for your help!
    Last edited by RyanC; 12-28-2018 at 01:22 PM.

  2. #2
    Banned
    Join Date
    Apr 2015
    Posts
    596
    I assume I should return "trash" .. but how could I analog it to PC's concept
    Last edited by RyanC; 12-28-2018 at 01:17 PM.

  3. #3
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    I don't believe that You have a knowledge professionally . . . if You want to understand "recursive programming" then start simple, and not with "Quicksort".
    This program calculate the Fibonacci-Number - iterativ. Change it to a recursive program.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main(void)
    {
        unsigned long x,y;
        int i, n;
    
    
        x = y = i = 1;
        
        //For Linux "system(clear)"
        system("cls");
        printf("\nBerechnet iterativ die Fibonaccizahl.");
        printf("\nGeben Sie eine Zahl ein: ");
        scanf("%d", &n);
        printf("\n");
        if (n == 0 || n == 1)
        {
            printf("\nDie Fibonaccizahl von %d ist: 1", n);
        }
        //For 3 show 1,2,3 -- Um bei 3 eine Anzeige von: 1 2 3 zu erreichen
        printf("1 "); 
        if (n > 40)
        {
            //Input only to 40 - sonst Überlauf
            printf("\nNur Eingaben bis 40!\n");        
        }
        else while (i < n)
        {
            x = x + y; y = x - y; i++;
            printf("%d  ", x);
        }
        printf("\n\nDie Fibonaccizahl von %d ist: %lu\n\n", n, x);
    }
    Last edited by Kernelpanic; 12-28-2018 at 02:25 PM.

  4. #4
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    There was a (little) mistake when the input is over forty.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void main(void)
    {
    	unsigned long x,y;
    	int i, n;
    
    
    	x = y = i = 1;
    	
    	//For Linux "system(clear)"
    	system("cls");
    	printf("\nBerechnet iterativ die Fibonaccizahl.");
    	printf("\nGeben Sie eine Zahl ein: ");
    	scanf("%d", &n);
    	printf("\n");
    	if (n == 0 || n == 1)
    	{
    		printf("\nDie Fibonaccizahl von %d ist: 1", n);
    	}	
    	
    	if (n > 40)
    	{
    		//Input only to 40 - sonst Überlauf
    		printf("\nNur Eingaben bis 40!\n");
    		exit(0);
    	}
    	else while (i < n)
    	{
    		x = x + y; y = x - y; i++;
    		printf("%d  ", x);
    	}
    	printf("\n\nDie Fibonaccizahl von %d ist: %lu\n\n", n, x);
    }

  5. #5
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Kernelpanic View Post
    There was a (little) mistake when the input is over forty.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void main(void)
    {
        unsigned long x,y;
        int i, n;
    
    
        x = y = i = 1;
        
        //For Linux "system(clear)"
        system("cls");
        printf("\nBerechnet iterativ die Fibonaccizahl.");
        printf("\nGeben Sie eine Zahl ein: ");
        scanf("%d", &n);
        printf("\n");
        if (n == 0 || n == 1)
        {
            printf("\nDie Fibonaccizahl von %d ist: 1", n);
        }    
        
        if (n > 40)
        {
            //Input only to 40 - sonst Überlauf
            printf("\nNur Eingaben bis 40!\n");
            exit(0);
        }
        else while (i < n)
        {
            x = x + y; y = x - y; i++;
            printf("%d  ", x);
        }
        printf("\n\nDie Fibonaccizahl von %d ist: %lu\n\n", n, x);
    }
    First, I appreciate your help to understand the concept. but what's exactly confusing me like this :
    lets say I have or actually after "n" splitting of array I arrived to array {2,3} and the pivot is 3(assuming that) I call it q=2(index of 3), then I must call to quicksort(1,1) which it gives me sorted "list" because {2} is already sorted ! , but what about quicksort(3,2) - the right side ? there's nothing outter than 3 in the list {2,3} ?! here what I do and how to decide which return to return it once this case occurred?! "whenever" I arrive to case like dont care/trash/outter of the bounds/blocking ..what should I return in the function?!
    Last edited by RyanC; 12-28-2018 at 03:02 PM.

  6. #6
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    God damn, I'm getting another crisis! This little program is full of pitfalls ... Last version! The Lord is with me!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void main(void)
    {
        unsigned long x,y;
        int i, n;
    
    
        x = y = i = 1;
        
        //For Linux "system(clear)"
        system("cls");
        printf("\nBerechnet iterativ die Fibonaccizahl.");
        printf("\nGeben Sie eine Zahl ein: ");
        scanf("%d", &n);
        printf("\n");
        if (n == 0 || n == 1)
        {
            printf("\nDie Fibonaccizahl von %d ist: 1", n);
            exit(0);
        }
        else if    (n == 3)
        {
            printf("1 ");
        }
        
        if (n > 40)
        {
            //Input only to 40 - sonst Überlauf
            printf("\nNur Eingaben bis 40!\n");
            //Sonst wird unten trotzdem "printf ..." ausgeführt
            exit(0);
        }
        else while (i < n)
        {
            x = x + y; y = x - y; i++;
            printf("%d  ", x);
        }
        printf("\n\nDie Fibonaccizahl von %d ist: %lu\n\n", n, x);
    }
    Last edited by Kernelpanic; 12-28-2018 at 03:14 PM.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Kernelpanic View Post
    Code:
    void main(void)
    Just... no. Please don't tell me you're using Turbo C or some other old compiler that allows this rubbish!

  8. #8
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by christop View Post
    Just... no. Please don't tell me you're using Turbo C or some other old compiler that allows this rubbish!
    I'm trying to run it over visual studio and don't compile .. maybe that's the problem

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    RyanC: Does QuickSort have to return anything in any case? All implementations of quick sort I've seen have a return type of "void", meaning it doesn't return a value.

  10. #10
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by christop View Post
    Just... no. Please don't tell me you're using Turbo C or some other old compiler that allows this rubbish!
    Code:
    int main(void)
    . . .
    return(0);
    Recursive concept-gcc-version2018-12-28_230706-jpg
    Last edited by Kernelpanic; 12-28-2018 at 04:08 PM.

  11. #11
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by christop View Post
    RyanC: Does QuickSort have to return anything in any case? All implementations of quick sort I've seen have a return type of "void", meaning it doesn't return a value.
    Exactly ! It doesn't return ..that's why Im confused..the quickreturn doesn't return in any case..which means in other words the sub-array is sorted; but what about the casr that quicksort(6,5) it's not allowed at all because there's no sub-array start from 6 and end at 5; in this case what should i return? What decision should I make to deal with that situation?

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    You don't have to return anything. The caller doesn't expect the quicksort() to return a value. Just make it return when start >= end.

  13. #13
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by christop View Post
    You don't have to return anything. The caller doesn't expect the quicksort() to return a value. Just make it return when start >= end.
    so if it's not returning anything then its actually saying its already sorted .. because the whole algorithm of quicksort whenever there's just return "nothing" then it means its already sorted , like
    Code:
    {2}
    is already sorted then not returning anything .. here I'm getting confused ..we said that if we don't return anything then it means already sorted, but you are declaring if there's a cross between r,p like quicksort(4,3) then it will just return.. so it means the array is already sorted! but we don't have array at all because there's no array start at 4 and end to 3 !

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Aiyo, if you are concerned about invalid arguments then just add a check for that in an initially called function that does have some way of indicating an error, e.g., with a return value or pointer parameter. It's not hard.
    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

  15. #15
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by RyanC View Post
    I'm trying to run it over visual studio and don't compile .. maybe that's the problem
    Turn up your warning levels to the highest level, and don't ignore any of the warnings. Warning messages are presented for a reason!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting recursive function to tail recursive
    By ajacobs365 in forum C Programming
    Replies: 1
    Last Post: 10-30-2011, 08:15 AM
  2. Replies: 1
    Last Post: 12-03-2010, 01:54 AM
  3. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 10:13 AM
  4. merge sort: recursive is fasfter than non-recursive
    By rio_cat in forum C Programming
    Replies: 8
    Last Post: 12-04-2006, 12:52 AM
  5. Algorithm help (Changing from Recursive to Non Recursive)
    By Thantos in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2004, 07:27 PM

Tags for this Thread