looking for some solutions? You are welcome.

SOLVED: How can I convert this code into a recursive function? Base case analysis

David F.:
#include <stdio.h>

int main() {
    int n;
    printf("Enter width: ");
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
       for (int j = 0; j < n; j++) {
           if (i == j || (i + j + 1) == n) {
               printf("X");
           } else {
               printf(" ");
           }
       }
       printf("\n");
    }
    return 0;
}

How can I convert this code into a recursive function? I am having trouble with the "base case" I am trying to work with this line

void recursiveProblem(int num, int max) {
    if (num <= (max / 2 + 1))
        ...
}

so if I reach halfway I can print the middle line and return it, but it's not working. This function will print a large X made from x's?



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent