473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

grid output pattern for magic square in c

in the below program we can generate magic square of size 3-160
but i want to print the output like
for magic size n

************************
* * * * *
* 5 * 8 * 7 * 6 *
************************
that is in
grid
format
itshould make generalized grid according to size of magic square and print
the numbers into the grid
help me for the same








#define SIZE 160 /* Consider a memory for increasing the size. */

#include "stdio.h"
#include "stdlib.h"


int m[SIZE][SIZE];

void odd_num(int n);
void even_num(int n);
void output(int n);
void _swap(int i1, int j1, int i2, int j2);
int main (int argc, char ** argv)
{
int n;
printf("\n**************************************** ***********************************");
printf("\n* < < < MAGIC SQUARE > > >; *");
printf("\n**************************************** ***********************************");

if (argc < 2)
{
printf("%s n\n", *argv);
exit(-1);
}
else
n = atoi(argv[1]); /* Convert String to int */

/* if( n < 3 || n > SIZE )
break;*/
if(n%2) odd_num(n);
else even_num(n);
output(n);


printf("\n\n* .. Quit .. . [[ < MAGIC SQUARE > ]] . .. *\n\n\n");

}
void odd_num(int n)
{
int i,j,num=1;
int nn=n*3/2;

for(i=0; i < n; i++)
for(j=0; j < n; j++)
m[(j-i+nn)%n][(i*2-j+n)%n]=num++;
}

void even_num(int n)
{
int i,j,num=1;
int nminus=n-1,nmiddle=n/2,nn=n*n+1;
int osl=0;
int switch_row[2];
int last_switch_column;
int first_block=(n-2)/4,second_block=nminus-first_block;
int first_inside=n/4,second_inside=nminus-first_inside;

for(j=0; j < n; j++)
for(i=0; i < n; i++) {
if(i >= first_inside && i <= second_inside && j >= first_inside && j <= second_inside)
m[i][j]=num;
else if((i > first_block && i < second_block) || (j > first_block && j < second_block))
m[i][j]=nn-num;
else m[i][j]=num;
num++;
}
if(!(n%4)) return;

switch_row[0]=random(nmiddle-1)+first_block+1;
switch_row[1]=random(nmiddle-1);
if(switch_row[1] >= first_block) switch_row[1]+=(nmiddle+1);
last_switch_column=random(nmiddle-1);
if(last_switch_column >= first_block) last_switch_column+=(nmiddle+1);

/* Simply, you can write as follows..
switch_row[0]=nmiddle;
switch_row[1]=0;
last_switch_column=0;
*/

for(i=0; i < nmiddle; i++) {
if(i==first_block || i==second_block) {
osl=1-osl;
continue;
}
_swap(second_block, i, second_block, nminus-i);
_swap(i, first_block, nminus-i, first_block);
_swap(i, second_block, nminus-i, second_block);
_swap(i, switch_row[osl], nminus-i, switch_row[osl]);
}
for(i=first_block+1; i < second_block; i++) {
_swap(first_block, i, second_block, i);
_swap(i, first_block, i, second_block);
}
_swap(first_block, nmiddle, second_block, nmiddle);
_swap(last_switch_column, first_block, last_switch_column, second_block);
}

void output(int n)
{
int i,j,ch,err=0;
unsigned long sum,sl,sc,sd1=0,sd2=0;

sum=(unsigned long)n*(n*n+1)/2;
printf(";-) SUM = %lu. * ..check sum... . . ",sum);
for(j=0; j < n; j++) {
sd1+=m[j][j];
sd2+=m[j][n-j-1];
sl=0;
sc=0;
for(i=0; i < n; i++) {
sl+=m[i][j];
sc+=m[j][i];
}
if(sl!=sum) {
err++;
printf("\n> Sum of the row-(%d) is %lu. It's incorrect..",j+1,sl);
}
if(sc!=sum) {
err++;
printf("\n> Sum of the column-(%d) is %lu. It's incorrect..",j+1,sc);
}
}
if(sd1!=sum) {
err++;
printf("\n> Sum of the diagonal-(\\) is %lu. It's incorrect..",j+1,sd1);
}
if(sd2!=sum) {
err++;
printf("\n> Sum of the diagonal-(/) is %lu. It's incorrect..",j+1,sd2);
}
if(err) printf("\n\n* %d errors are happened.",err);
else printf("- O.K. -");
printf("\n\n:-? Do you want to display(Y/n)? ");
ch=getch();
if(ch=='n' || ch=='N') return;
printf("\n---+---------------------------------------------------------------------------");
for(j=0; j < n; j++) {
printf("\n%2d)] ",j+1);
for(i=0; i < n; i++)
printf("%4d,",m[i][j]);
}
printf("\n---+---------------------------------------------------------------------------");
}

void _swap(int i1, int j1, int i2, int j2)
{
int k;

k=m[i1][j1];
m[i1][j1]=m[i2][j2];
m[i2][j2]=k;
}
Sep 1 '07 #1
1 4148
JosAH
11,448 Expert 8TB
in the below program we can generate magic square of size 3-160
but i want to print the output like for magic size n in grid format
Note that you add a 'field width' specifier in your format string, e.g.

Expand|Select|Wrap|Line Numbers
  1. printf("%4d", num); // use 4 positions for 'num'
  2.  
kind regards,

Jos
Sep 1 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Kelmen Wong | last post by:
Greeting, I want to extract all "" from a string, what pattern should I used? eg. = - return array or test1, or test2
4
by: winnerpl | last post by:
Hey guys I'm trying to get a magic square but I'm stuck with it printing out only 1's and 0's in random places. Hope you experts can can provide some input on what I'm doing wrong. #include...
0
by: Workgroups | last post by:
Not sure the best way to go about making this: I have 3 user defineable values - Width, Height, and Size. These values need to define a "grid" of some kind in the following manner: The Width is...
9
by: totalgeekdom | last post by:
Background: The problem I'm trying to solve is. There is a 5x5 grid. You need to fit 5 queens on the board such that when placed there are three spots left that are not threatened by the queen. ...
1
by: Eric | last post by:
I use RegEx to search pattern. Script works fine in the situation when there is a colon after each word and it fetch the rest of the word from that line. Now the pattern is in square bracket and i...
1
by: shaveta | last post by:
pls guide me to write a program in c to generate a magic square of size n*n , where n is odd A magic square is an n*n matrix of integer from 1 to n^2, where n is odd, such that the sum of every...
8
KoreyAusTex
by: KoreyAusTex | last post by:
I am pretty new at programming and need some feedback as to why this program is not working??? It was a pretty big undertaking for me but I can't seem to figure out what is wrong: import...
2
by: jyck91 | last post by:
i have done the magic square: #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 13 main() { FILE *fp; int i, j, n, row, column;
1
by: magic man | last post by:
I am 50 years old ...and am working physical models of the math structure called a magic square .. for my own interest. My present problem is this. I have a topograhical model for the square...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.