前田稔(Maeda Minoru)の超初心者のプログラム入門
#include <stdio.h> short t3[3][3]; // Function Prototype void SetNumber(const short n, short t[][3]); void Disp(const short n, short t[][3]); //★ MAIN PROGRAM void main() { printf("\n3×3の魔方陣\n"); SetNumber(3,t3); Disp(3,t3); } |
void Disp(const short n, short t[][3]) { int x,y; for(y=0; y<n; y++) { for(x=0; x<n; x++) printf("%3d",t[y][x]); printf("\r\n"); } } |
// t[n][n] に数値を設定 void SetNumber(const short n, short t[][3]) { int i,x,y,xw,yw; for(y=0; y<n; y++) for(x=0; x<n; x++) t[y][x]= 0; x= (n-1)/2; //y,x 下段、中央 y= n-1; for(i=1; i<=n*n; i++) { t[y][x]= i; xw= (x+1)%n; //右斜め下 yw= (y+1)%n; if (t[yw][xw]) y= (y+n-1)%n; else { x= xw; y= yw; } } } |
#include <stdio.h> short t3[3][3]; short t5[5][5]; // Function Prototype void SetNumber(const short n, short *p); void Disp(const short n, short *p); //★ MAIN PROGRAM void main() { printf("\n3×3の魔方陣\n"); SetNumber(3,(short*)t3); Disp(3,(short*)t3); printf("\n5×5の魔方陣\n"); SetNumber(5,(short*)t5); Disp(5,(short*)t5); } |
void Disp(const short n, short *p) { int nn,i; nn= n*n; for(i=0; i<nn; i++) { printf("%3d",*(p+i)); if (0==(i+1)%n) printf("\r\n"); } } |