0

I am working on a magic square problem, the size of the square will be an odd number between 3 to 99. Here is the result of a 5x5 square:

   15    8    1   24   17

   16   14    7    5   23

   22   20   13    6    4

    3   21   19   12   10

    9    2   25   18   11

The program works fine with a for loop, but I've been asked to rewrite this using recursion.

Can anyone give me some direction? Much appreciated!

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            square[i][j] = 0;
        }
    }

    square[i][j] = k;    // Start from the middle of the first row (1)

    for (k = 2; k <= size * size; k++) {
        // Move up and left    

        if (square[I][j]) {
            // Move down if space is occupied
        } 

        square[I][j] = k;
    }

I tried to rewrite it in recursive, but got "Segmentation fault: 11".

void magic(int square[][size], int size, int k, int i, int j) {

    if (k >= size * size) {
        return;
    } else {
        // Move up and left   
        
        if (square[I][j]) {
            // Move down if space is occupied
        } 

        square[I][j] = k;

        magic(square, size, k + 1, i, j);
    }
}
Bryce Chan
  • 1,639
  • 11
  • 26
  • 1
    *"I am too embarrassed to post lol"* - we can't tell you where you went off the rails until we see the wreckage. Post it anyway. – WhozCraig Oct 15 '20 at 10:52
  • 2
    I really don't like it when a teacher asks their students to implement a naturally iterative problem with recursion. They're basically forcing you to do something worse than what you've already done. If they want to teach recursion, that's fine, but they should make an effort to come up with a *real* example that actually *benefits* from recursion. In this case, perhaps they really do have some solution in mind that is simplified by using recursion, but I don't know what it is. – Tom Karzes Oct 15 '20 at 11:16
  • Do you want to recursively find **ALL** solutions for magic squares, or just the one with numbers increasing in diagonals starting from the middle of the top row? – pmg Oct 15 '20 at 12:04
  • @pmg Just the one with numbers increasing until all slot is filled. It can start from anywhere I just think start from middle and move top left is easier to implement. – Bryce Chan Oct 15 '20 at 12:21
  • 1
    Ok ... let's say our recursive function is `magic(arr, size, n);` ... we start off with `magic(arr, 25, 1)` ... which puts the `1` in place and recurses with `magic(arr, 25, 2)`... which puts `2` in place and recurses with 3 ... puts `3` recurses with `n + 1` ... until `n` is 25. – pmg Oct 15 '20 at 12:26

2 Answers2

2

Since building a magic square is naturally iterative, and not at all naturally recursive, I can only suppose that the instructor's objective is to emphasize the fact that any iterative algorithm can be rewritten as a recursive one. (The reverse is also true.) So how does that work?

An iterative algorithm has this abstract form:

iterative(initial_index, final_index):    
    i := initial_index
    current_state := initial_state
    loop_top:
    if i > final_index then
        return current_state
    update_state(current_state, i)
    i := i + 1
    go to loop_top

That can be rewritten recursively like so (for example):

recursive(initial_index, final_index):
    if initial_index > final_index then
        return initial_state                   // the same initial_state as before
    current_state = recursive(initial_index, final_index - 1)  // recursion
    update_state(current_state, final_index)   // the same update_state() as before
    return current_state

Details of how to map your particular algorithm onto that or something equivalent are left as the exercise they are meant to be.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
1

Turns out the recursion I wrote is correct all along, the compile error come from the 2D array parameter of the recursive function, the size of that 2D array is dynamic assign from the user after the program compiled, in the old version I defined the function with the maximum value that's why the function only works when the size is maximum.

Bryce Chan
  • 1,639
  • 11
  • 26