Thread: recursion lab exercise

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    4

    Question recursion lab exercise

    i need help guys ,i cant find out how to do this programm.
    ------------------------
    Make a program that reads two physical numbers from user (N *
    )
    ,N and S. The program should display all unique numbers
    combinations of N single digits whose sum equals S. Eg.
    for N = 3 and S = 10 the program should display:
    8+1+1=10
    7+2+1=10
    6+3+1=10
    5+4+1=10
    6+2+2=10
    5+3+2=10
    4+4+2=10
    4+3+3=10


    other whice If a number is out of the scope it will display
    "Wrong input".
    Code:
    int digits(N)
    {
        int i,j;
        for (i=1;i<=9;i++)
        {
            for (j=1;i<=N;j++)
            {
                if (i+9<S)
                {
                    
                }
            }
        }
    }
    int sum ()
    {
        if (n==0)
            return 0;
        else
        {
    
    
        }
    }
    
    
    int main()
    {
        int N,S,i;
        scanf("%d,%d",&N,&S);
        
        if (N*9 <= S)
        {
            
        }
        else 
        printf("Wrong Input");
        
        for (i=1;i<=N;i++)
        {
            printf("%d",digits(N));
            printf("+");
        }
        printf("=%d",S);
    
    
        return 0;
    }
    Last edited by Salem; 11-16-2019 at 03:36 AM. Reason: Added code tags

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    Post the code where we can see that you've tried to solve the problem! No one here will solve your HW for you.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    4
    i upload the code

  4. #4
    Registered User
    Join Date
    Nov 2019
    Posts
    4
    insert
    Code:
    int digits (int N,int S)
    {
        if (N==0)
            return 0;
        if (S==0)
            return 1;
    
    
        int ans=0;
        int i;
    
    
        for (i=1;i<=9;i++)
            if (S-i>=0)
            ans += digits(N-1,S-1);
        return ans;
    }
    int main()
    {
        int N,S,i;
        printf("give digits:");
        scanf("%d",&N);
        printf("\ngive sum:");
        scanf("%d",&S);
        printf("%d + ",digits(N,S));
        return 0;
        /*
    Is it better,what more do i have to do?
    Last edited by Dayker; 11-16-2019 at 05:01 AM.

  5. #5
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    What are you even trying to do? Take a notebook and try to solve the problem at hand by analysing what your algorithm should be doing for a simple input.

    Understand the problem at hand. Don't jump around looking for something from someone that works. I don't think you've realised but the first thing you should have done in your code was added a validity check, i.e., should the problem be solved for +ve inputs or should it take into account -ve numbers too. Your description of the problem is vague.

    Take N = 3, S = 10.

    What you expect is this:

    8+1+1=10
    7+2+1=10
    6+3+1=10
    5+4+1=10
    6+2+2=10
    5+3+2=10
    4+4+2=10
    4+3+3=10

    What the actual answer is an infinite such series of numbers. Think about it based on the question you've stated. You can also have stuff like:
    9+(-1)+(1) = 9
    3 + 10 + (-4) = 9
    .
    .
    .
    These are valid solutions too based on your question because all we want here is N numbers that 'ADD' up to S!

    Is it better,what more do i have to do?

    No, let's first decide what is correct and what is not.

    Let's assume that you meant only positive numbers. Then, for N = 3, S = 5, the expected output should be (according to me, considering unique sum representation taking into respect the permutations of numbers in addends too i.e. (1 + 2) is considered different from (2 + 1) arrangement):
    5 + 0 + 0 = 5 (rejected because we are talking only of +ve numbers, 0 is neither +ve nor -ve)
    4 + 1 + 0 = 5 (rejected)
    4 + 0 + 1 = 5 (rejected)
    3 + 2 + 0 = 5 (rejected)
    3 + 1 + 1 = 5 (okay)
    3 + 0 + 2 = 5 (rejected)
    2 + 3 + 0 = 5 (rejected)
    2 + 2 + 1 = 5 (okay)
    2 + 1 + 2 = 5 (okay)
    2 + 0 + 3 = 5 (rejected)
    1 + 4 + 0 = 5 (rejected)
    1 + 3 + 1 = 5 (okay)
    1 + 2 + 2 = 5 (okay)
    1 + 1 + 3 = 5 (okay)
    1 + 0 + 4 = 5 (rejected)
    0 + 5 + 0 = 5
    (rejected)
    0 + 4 + 1 = 5
    (rejected)
    0 + 3 + 2 = 5
    (rejected)
    0 + 2 + 3 = 5
    (rejected)
    0 + 1 + 4 = 5
    (rejected)
    0 + 0 + 5 = 5
    (rejected)

    Effectively:
    3 + 1 + 1 = 5
    2 + 2 + 1 = 5
    2 + 1 + 2 = 5
    1 + 3 + 1 = 5
    1 + 2 + 2 = 5
    1 + 1 + 3 = 5

    If we agree on this, we can proceed forward in a correct way. This is how the INPUT and OUTPUT looked from a similar problem I solved.

  6. #6
    Registered User
    Join Date
    Nov 2019
    Posts
    4
    aha i get it,ill try it again and ill come back asap.Thank you for your time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list recursion exercise Chapter 16 problem 3
    By Icyblizz in forum C++ Programming
    Replies: 2
    Last Post: 06-24-2017, 02:08 AM
  2. Exercise 1-13 in K&M
    By deckoff8 in forum C Programming
    Replies: 2
    Last Post: 12-02-2011, 07:08 PM
  3. An exercise from K&R
    By GL.Sam in forum C Programming
    Replies: 4
    Last Post: 05-07-2010, 09:31 AM
  4. K&R exercise
    By Tool in forum C Programming
    Replies: 1
    Last Post: 12-05-2009, 09:39 AM

Tags for this Thread