Thread: c programing recursion homework.

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    18

    c programing recursion homework.

    so i have a homework in recursion , i have to write a function that gets 2 scores of players, and print all the options to get the last score, every player can get 1 point or 2 points per single turn, i need it to with no loop use.
    Attached Images Attached Images c programing recursion homework.-1-jpg c programing recursion homework.-2-jpg c programing recursion homework.-3-jpg c programing recursion homework.-4-jpg 

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,750
    Please paste the code that you've currently written

  3. #3
    Registered User
    Join Date
    Dec 2019
    Posts
    18
    Code:
    void Stanga()
    {
        int firstteam, socendteam;
        firstteam = first_team_score();
        socendteam = socend_team_score();
        print_all_Stanga_options(0, 0, firstteam, socendteam);
    }
    
    
    int first_team_score()
    {
        int score;
        printf("Enter the result of the 1st team(0-15):\n");
        scanf("%d", &score);
        if (score < 0 || score>15)
            score = first_team_score();
        return score;
    }
    
    
    int socend_team_score()
    {
        int score;
        printf("Enter the result of the 2nd team(0-15):\n");
        scanf("%d", &score);
        if (score < 0 || score>15)
            score = socend_team_score();
        return score;
    }
    
    
    void print_all_Stanga_options(int printfirst, int printsocend, int firstteam, int socendteam)
    {
        printf("%d:%d ", printfirst, printsocend);
        if (firstteam != printfirst)
            print_all_Stanga_options(printfirst + 1, printsocend, firstteam, socendteam);
    
    
        if (printsocend != socendteam)
            print_all_Stanga_options(printfirst, printsocend + 1, firstteam, socendteam);
    
    
        if (firstteam + 1 != printfirst && firstteam != printfirst)
            print_all_Stanga_options(printfirst + 1, printsocend, firstteam, socendteam);
    
    
        if (printsocend + 1 != socendteam && printsocend != socendteam)
            print_all_Stanga_options(printfirst, printsocend + 1, firstteam, socendteam);
    
    
        if (firstteam == printfirst && socendteam == printsocend)
        printf("\n");
    
    
    }




    the print_all_Stanga_options function dont realy work correctly

  4. #4
    Registered User
    Join Date
    Dec 2019
    Posts
    18
    the pictures i added is what i want to get.

  5. #5
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    300
    Where's your main function?

    Code:
    int first_team_score()
    {
        int score;
        printf("Enter the result of the 1st team(0-15):\n");
        scanf("%d", &score);
        if (score < 0 || score>15)
            score = first_team_score();
        return score;
    }
     
     
    int socend_team_score()
    {
        int score;
        printf("Enter the result of the 2nd team(0-15):\n");
        scanf("%d", &score);
        if (score < 0 || score>15)
            score = socend_team_score();
        return score;
    }
    You need to do recursion for input? I can put -1 as long as I want as a user of your program and get it to crash....

    Also, if you're trying to learn recursion and recursive functions, this is not the right way. Try printing something like the Fibonacci sequence with recursion.
    Last edited by Zeus_; 12-18-2019 at 06:02 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  6. #6
    Registered User
    Join Date
    Dec 2019
    Posts
    18
    im okay with that code, those are for making sure i get numbers just betwen 1-15,,
    my problem is with "voidprint_all_Stanga_options(intprintfirst, intprintsocend, intfirstteam, intsocendteam)" is these int enogh for me ? and what i need to change to get the output in the pictures i added.

  7. #7
    Registered User
    Join Date
    Dec 2019
    Posts
    18
    Code:
    void print_all_Stanga_options(int printfirst, int printsocend, int firstteam, int socendteam)
    {
    	if (firstteam == printfirst && socendteam == printsocend)
    	{
    		printf("%d:%d ", printfirst, printsocend);
    		printf("\n");
    	}
    	if (printfirst < firstteam)
    	{
    		printf("%d:%d ", printfirst, printsocend);
    		print_all_Stanga_options(printfirst + 1, printsocend, firstteam, socendteam);
    		//if (printfirst > 0)
    		//	printfirst = printfirst - 1;
    	}
    	if (printfirst + 1 < firstteam)
    	{
    		printf("%d:%d ", printfirst, printsocend);
    		print_all_Stanga_options(printfirst + 2, printsocend, firstteam, socendteam);
    		//if (printfirst > 1)
    		//	printfirst = printfirst - 2;
    	}
    	if (printsocend < socendteam)
    	{
    		printf("%d:%d ", printfirst, printsocend);
    		print_all_Stanga_options(printfirst, printsocend + 1, firstteam, socendteam);
    		//if (printsocend > 0)
    		//	printsocend = printsocend - 1;
    	}
    	if (printsocend + 1 < socendteam)
    	{
    		printf("%d:%d ", printfirst, printsocend);
    		print_all_Stanga_options(printfirst, printsocend + 2, firstteam, socendteam);
    		//if (printsocend > 1)
    		//	printsocend = printsocend - 2;
    	}
    }
    thats what i have done, im not sure what have to done next.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Recursion. I'm so lost on this homework problem.
    By Kurtanius21 in forum C Programming
    Replies: 11
    Last Post: 12-06-2012, 09:10 PM
  2. Homework help: Sudoku and "explosive recursion"
    By green_ghoul in forum C Programming
    Replies: 13
    Last Post: 11-22-2011, 11:16 PM
  3. programing with pi, canīt get it
    By royaro in forum C Programming
    Replies: 1
    Last Post: 02-12-2011, 05:01 PM
  4. C programing for DNA...
    By S16 in forum C Programming
    Replies: 9
    Last Post: 04-29-2009, 09:25 AM
  5. I do not do a lot of programing but ...
    By dcsmithCB in forum C Programming
    Replies: 3
    Last Post: 10-07-2006, 07:11 AM

Tags for this Thread