magic square

This is a discussion on magic square within the C Programming forums, part of the General Programming Boards category; Hi, i've been asked to write a program to construct a magic square (you know, all rows, columns and the ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    2

    Unhappy magic square

    Hi, i've been asked to write a program to construct a magic square (you know, all rows, columns and the diagonals are equal..) Well i've written up to the point that the the array is printed and the row totals are calculated. this is the output i should get...

    15 8 1 24 17 65
    16 14 7 5 23 65
    22 20 13 6 4 65
    3 21 19 12 10 65
    9 2 25 18 11 65

    However, my code is obviously incrementing the range value but overriding the previous range values when it comes to printing the array and i'm getting this output....

    25 25 25 25 25 125
    25 25 25 25 25 125
    25 25 25 25 25 125
    25 25 25 25 25 125
    25 25 25 25 25 125

    and heres my code.......

    /* De la Loubčre's Magic Square Construction Method */
    #include <stdio.h>
    #include <conio.h>
    #define ARRAY_SIZE 5
    #define RANGE ARRAY_SIZE * ARRAY_SIZE
    #define MOD(a, b) (a >= 0 ? a % b : a % b + b)
    /* High Level Description...
    Initialise array
    WHILE further range values to insert
    Determine location of next entry
    Increment entry value
    Place entry in array
    Output resuts
    */
    /* Detailed Description...
    Initialize array to 0's
    Calculate middle column
    WHILE further range values to insert
    IF current range value MOD array size NOT equal to 0
    Calculate new row count
    Calculate new column count
    ELSE
    Increment row count MOD array size
    Increment range value
    Place range value in array
    Clear the screen
    Output heading text to the screen
    FOR each row in the array
    Initialize the row total
    For each column in the array
    Output array value to screen
    Accumulate totals
    Output row total and new line
    Output column totals to screen
    FOR first row to last row
    Column count is equal to row count
    Accumulate top left to bottom right diagonal total
    Column count is equal to 0
    FOR last row to first row
    Accumulate bottom left to top right diagonal total
    Increment column count
    Output Top Left - Bottom Right diagonal text and total to the screen
    Output Bottom Left - Top Right diagonal text and total to the screen
    */
    void main()
    {
    int magic_square [ARRAY_SIZE] [ARRAY_SIZE] = {0},
    row_count = 0,
    column_count,
    range_value = 1,
    row_index,
    col_index,
    row_total;


    column_count = ARRAY_SIZE/2;
    magic_square [row_count] [column_count] = range_value;

    while (range_value <= RANGE - 1)
    {
    if (range_value % ARRAY_SIZE != 0)
    {
    row_count = MOD(row_count-1, ARRAY_SIZE);
    column_count = MOD(column_count-1, ARRAY_SIZE);
    }
    else
    row_count = MOD(row_count, ARRAY_SIZE) +1;
    ++range_value;
    magic_square [row_count] [column_count] = range_value;
    }
    clrscr();
    printf("De La Loubčre's Magic Square Construction\n");
    printf("-----------------------------------------\n\n");

    for(row_index = 0; row_index <= ARRAY_SIZE - 1; ++row_index)
    {
    row_total = 0;
    for(col_index = 0; col_index <= ARRAY_SIZE - 1; ++col_index)
    {
    printf(" %3d", range_value);
    row_total += range_value;
    }
    printf(" %d\n", row_total);
    }


    Please, Please can someone help me!!! This has to be in on thursday morning and i've been staring at my computer for 3 days totally bewildered, i think i need some fresh eyes to look at it for me!

    Lots of love
    Sam.xoxo

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    2

    Unhappy by the way

    i forgot to mention how the integers are positioned in the magic square if you didnt know...... you place "1" in the middle column of the first row then the next number is placed one row up, one column back - if this position is outside the array then the number is placed on the opposite side of the array, if the position is already filled then the number goes directly underneath the previous value.

    If any of that makes any sense...

    Sam.xoxo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  2. Windows crashing Magic Square
    By KoshiB in forum C++ Programming
    Replies: 9
    Last Post: 04-19-2006, 09:02 PM
  3. help on magic square
    By katway in forum C Programming
    Replies: 2
    Last Post: 03-07-2005, 05:44 PM
  4. magic square whoes
    By caws in forum C Programming
    Replies: 9
    Last Post: 03-30-2003, 09:36 PM
  5. Help with magic square program
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-15-2002, 05:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21