Thread: Problem with magicSquare

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    27

    Problem with magicSquare

    I cannot understand why it doesnt fill the array the way it supposed to.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main()
    {
        int number;
        printf("Type an odd number between 3 and 9:");
    
    
        while(!(number % 2)){//Odd control
            scanf("%d" , &number);
            if(!(number % 2))printf("\nYou didnt type an odd number, please type again:");
        }
    
    
        //array
        int magicsquare[number][number];
    
    
        memset(magicsquare, 0, sizeof(magicsquare));
    
    
        int i,j;
    
    
        //putting the one
        int one = (number+1) /2 ;
        printf("%d", one);
        magicsquare[ one - 1 ][ number - 1 ] = 1;
    
    
        //filling the array
        int counter = 0, limit = number - 1, fill = 2, tempj = 0;
        i = one - 1;
        j = limit ;
    
    
        while ( fill <= (number*number)){
            i = i + 1;
            if ( i > limit) {
                i = 0 ;
                counter++;
            }
            tempj = j;//it keeps the j in case the i,j goes out
            j = j + 1;
            if ( j > limit ){
                j = 0;
                counter++;
            }
            if ( counter == 2){//when i and j goes beyond the array
                magicsquare[i][tempj] = fill;
                fill++;
            }
            else{
                if ( magicsquare[i][j] == 0){
                    magicsquare[i][j] = fill;
                    fill++;
                }
                else {
                    i = i - 1;
                    j = j - 2;
                    magicsquare[i][j] = fill;
                    fill++;
                }
            }
            printf("\ncounter=%d j=%d i=%d fill=%d\n", counter, j, i, fill);
            counter = 0;
    
    
    
    
        }
    
    
    
    
        //printing the array
        printf("\n");
        for(i= 0; i < number ; i++){
            for(j =0; j < number; j++){
                printf("%d |", magicsquare[i][j]);
            }
            printf("\n");
        }
    
    
    
    
    
    
        return 0;
    }

    OUTPUT FOR A 5X5 MAGICSQUARE
    11 |10 |4 |23 |16 |
    18 |12 |6 |5 |24 |
    25 |19 |13 |7 |1 |
    2 |0 |20 |14 |8 |
    9 |3 |0 |21 |15 |

    WHEN 5X5 SQUARE IT SHOULD BE LIKE THIS:
    11 10 4 23 17
    18 12 6 5 24
    25 19 13 7 1
    2 21 20 14 8
    9 3 22 16 15

  2. #2
    Registered User
    Join Date
    Oct 2017
    Posts
    27
    ........ YOU NOOBS I MADE IT
    if anyones intrested for the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main()
    {
        int number;
        printf("Type an odd number between 3 and 9:");
    
    
        while(!(number % 2)){//Odd control
            scanf("%d" , &number);
            if(!(number % 2))printf("\nYou didnt type an odd number, please type again:");
        }
    
    
        //array
        int magicsquare[number][number];
    
    
        memset(magicsquare, 0, sizeof(magicsquare));
    
    
        int i,j;
    
    
        //putting the one
        int one = (number+1) /2 ;
        printf("%d", one);
        magicsquare[ one - 1 ][ number - 1 ] = 1;
    
    
        //filling the array
        int counter = 0, limit = number - 1, fill = 2, tempj = 0;
        i = one - 1;
        j = limit ;
    
    
        while ( fill <= (number*number)){
            i = i + 1;
            if ( i > limit) {
                i = 0 ;
                counter++;
            }
            tempj = j;//it keeps the j in case the i,j goes out
            j = j + 1;
            if ( j > limit ){
                j = 0;
                counter++;
            }
            if ( counter == 2 && magicsquare[i][j] != 0 ){//when i and j goes beyond the array
                i = limit ;
                //if ( i == -1 ){i = limit;}
                j = limit - 1;
                magicsquare[i][j] = fill;
                fill++;
            }
            else{
                if ( magicsquare[i][j] == 0){
                    magicsquare[i][j] = fill;
                    fill++;
                }
                else {
                    i = i - 1;
                    if ( i == -1 ){i = limit;}
                    j = j - 2;
                    magicsquare[i][j] = fill;
                    fill++;
                }
            }
            printf("\ncounter=%d j=%d i=%d fill=%d\n", counter, j, i, fill);
            counter = 0;
    
    
    
    
        }
    
    
    
    
        //printing the array
        printf("\n");
        for(i= 0; i < number ; i++){
            for(j =0; j < number; j++){
                printf("%d |", magicsquare[i][j]);
            }
            printf("\n");
        }
    
    
    
    
    
    
        return 0;
    }

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    commended out because they are not even being used within your code
    Code:
     //filling the array
        int counter = 0, limit = number - 1, fill = 2; //, tempj = 0;
        i = one - 1;
        j = limit ;
     
     
        while ( fill <= (number*number)){
            i = i + 1;
            if ( i > limit) {
                i = 0 ;
                counter++;
            }
         //   tempj = j;//it keeps the j in case the i,j goes out
            j = j + 1;
            if ( j > limit ){
    
    
    Code:
    term2.c:35:52: warning: variable 'tempj' set but not used [-Wunused-but-set-variable]
    therefore useless.
    Code:
    11 |10 |4 |23 |17 |
    18 |12 |6 |5 |24 |
    25 |19 |13 |7 |1 |
    2 |21 |20 |14 |8 |
    9 |3 |22 |16 |15 |
    userx@slackwhere:~/bi
    OUTPUT FOR A 5X5 MAGICSQUARE
    11 |10 |4 |23 |16 |
    18 |12 |6 |5 |24 |
    25 |19 |13 |7 |1 |
    2 |0 |20 |14 |8 |
    9 |3 |0 |21 |15 |

    WHEN 5X5 SQUARE IT SHOULD BE LIKE THIS:
    11 10 4 23 17
    18 12 6 5 24
    25 19 13 7 1
    2 21 20 14 8
    9 3 22 16 15
    remove this for get that,
    Code:
      printf("%d |", magicsquare[i][j]);
    // to
      printf("%d", magicsquare[i][j]);
    Last edited by userxbw; 11-28-2017 at 10:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2014, 06:46 PM
  2. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  3. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  4. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM

Tags for this Thread