1

I need to make a program in C that reads two positive integers from user, numbers N and S. The program should display all unique number combinations of N single digits whose sum equals S using recursion. 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

If a number is out of the scope it will display "Wrong input".

This is the code i wrote so far:

void check(int n,int s)
{
    int arr[n];
    for (int i=1;i<=n;i++)
    {
        arr[i]=1;
    }
    if (n==s)
    {
        int j=1;
        while (j<n)
        {
            printf("%d+",arr[j]);
            j++;
            if (j==n)
            {
                printf("%d=%d",arr[j],s);
                break;
            }
        }
    }
    else if (n<s)
    {
        printf("Wrong input");
        exit(0);
    }
    else
    {

    }
}

int main()
{
    int N,S;
    scanf("%d %d",&N,&S);
    if (N<=0 || S<=0)
    {
        printf("Wrong input");
        exit(0);
    }
    check(N,S);

}

I cant figure out how to solve this problem and i must use recursion for this project.Can anyone help me?

Wal Choice
  • 51
  • 2
  • 7
  • Review `int arr[n]; for (int i=1;i<=n;i++) { arr[i]=1; }`, C arrays are indexed from 0. Perhaps other issues. – chux - Reinstate Monica Nov 17 '19 at 18:49
  • i skipped `arr[0]` on purpose. I can change it if it will create problems further into my solving. – Wal Choice Nov 17 '19 at 18:51
  • The same task as a recent [closed question](https://stackoverflow.com/questions/58894353/c-recursion-function-to-find-all-possible-combinations-of-n-digits-that-sum-up-t). – Weather Vane Nov 17 '19 at 18:52
  • You not only skipped `arr[0]` but indexed the out-of-bounds `arr[n]` with that `for` loop range. – Weather Vane Nov 17 '19 at 18:55
  • @WeatherVane It seems to me that we have the same problem to solve. – Wal Choice Nov 17 '19 at 18:56
  • "i skipped arr[0] on purpose" --> then use `int arr[n+1];` – chux - Reinstate Monica Nov 17 '19 at 18:57
  • @WeatherVane Thank you i will change it, but my main problem is a way to solve the recursion part. – Wal Choice Nov 17 '19 at 18:58
  • I think you need to pass an array from `main`, not keep defining it in the (will-be) recursive function. The first task of the recursive function should be to check the digit position, and when you are *over* the limit you print the result (if valid), increment the solution count, and `return`, thus terminating the recursion. The rest of the recursive function should iterate with possible values in its given digit position, and recurse. – Weather Vane Nov 17 '19 at 19:02
  • ...this makes the recursion process fairly easy to understand. The first instance deals with the first digit position, the next invocation deals with the second digit position, and so on until the array is full. I don't know it you are allowed to use *any* loops, but nothing stops you developing the code in stages. When you get this working, go to the next stage and also replace the looping on each digit, with recursion ;) – Weather Vane Nov 17 '19 at 19:11
  • @WeatherVane Than you for your answer i will give it a try and reply back :) – Wal Choice Nov 17 '19 at 19:18
  • @WeatherVane I cant seem to figure out the solution. I will ask my teacher tommorow in class and reply back.Thank you for your time! I don't fully understand the way it is supposed to go and cant translate that into code. – Wal Choice Nov 17 '19 at 20:58
  • This seems related: [https://stackoverflow.com/questions/14162798/generating-all-distinct-partitions-of-a-number](https://stackoverflow.com/questions/14162798/generating-all-distinct-partitions-of-a-number) – גלעד ברקן Nov 18 '19 at 04:45

0 Answers0