Home > other >  Number pattern from recursion - C Programming
Number pattern from recursion - C Programming

Time:08-09

I'm having trouble with outputting a number pattern from recursion. I have correctly outputted some of the right numbers, but I can't get it to output the negative number and some of the missing numbers. This is the example input and output for my assignment: Ex. If the input is:

12
3

the output is:

12 9 6 3 0 -3 0 3 6 9 12 

This is my code:

int PrintNumPattern(int num1, int num2) {
    printf("%d ", num1);
    if (num1 <= 0) {
        return 0;
    }
    
    else {
        PrintNumPattern(num1 - num2, num2);
    }
    
    printf("%d ", num1);
}

int main(void) {
   int num1;
   int num2;
   
   scanf("%d", &num1);
   scanf("%d", &num2);
   PrintNumPattern(num1, num2);
}

This is the output I keep getting from my code:

12 9 6 3 0 3 6 9 12 

CodePudding user response:

Your code just needs a few tweaks to provide the spirit of what you are after. Following is your code with some minor revisions.

#include <stdio.h>
#include <stdlib.h>

void PrintNumPattern(int num1, int num2) {

    if (num1 < 0) {
        num2 = num2 * -1;           /* Reverse the sign to start incrementing back up */
    }
    else {
        printf("%d ", num1);        /* Put this here to skip a redundant repeated print */
        PrintNumPattern(num1 - num2, num2);
    }
    printf("%d ", num1);
}

int main(void) {
   int num1;
   int num2;

   scanf("%d", &num1);
   scanf("%d", &num2);
   PrintNumPattern(num1, num2);
   printf("\n");
}

Here are the highlights.

  • When testing that the value has gone negative, there just needs to be a check for a negative number and not zero value. When that condition does occur, the second parameter needs to be changed to a negative number in order to effectively start adding instead of subtracting.
  • The initial "printf" statement is moved to be inside the "else" block so that it only prints out the negative number once.

In testing out these changes, the following is the output I saw on my terminal.

@Una:~/C_Programs/Console/Patterns/bin/Release$ ./Patterns 
12
3
12 9 6 3 0 -3 0 3 6 9 12 
@Una:~/C_Programs/Console/Patterns/bin/Release$ ./Patterns 
22
4
22 18 14 10 6 2 -2 2 6 10 14 18 22 

Experiment with that and see if that gives you what you want.

CodePudding user response:

The posted code can't print -3 because it can't reach that value in the recursive calls

printf("%d ", num1);
if (num1 <= 0) {         // As soon as num1 reaches 0, it stops the recursion
    return 0;
}
else {
    PrintNumPattern(num1 - num2, num2);
}
    
printf("%d ", num1);     // This is skipped too.

To obtain the expected output, the recursion should be stop only when num1 becomes negative:

void PrintNumPattern(int num1, int num2)
{
  printf("%d ", num1);
  
  if (num1 >= 0)
  {
    PrintNumPattern(num1 - num2, num2);
    printf("%d ", num1);
  }
}

Testable here.

Note that I changed the return type to void. In the posted code the returned value is never used and not all the paths in the function have a return statement.

CodePudding user response:

By rearranging @Bob's function, this is (imho) somewhat easier to think about. Full credit to @Bob...

When num1 >= 0, print it, and then go deeper... Eventually the bottom is reached, and the recursion unwinds through the 2nd printf()...

void PrintNumPattern(int num1, int num2) {
    if (num1 >= 0) {
        printf("%d ", num1);
        PrintNumPattern(num1 - num2, num2);
    }
    printf("%d ", num1);
}

int main(void) {
   PrintNumPattern( 12, 3 );
   printf("\n");
   return 0;
}
  • Related