作者honamida (honamida)
看板C_and_CPP
標題[問題] function
時間Sat Nov 6 20:41:07 2010
我是個程式新手
對C還是有很多不懂的地方~_~
最近學到有關function 的部份
要用function寫一個奇數次魔方陣
下面是我寫的程式碼
---------------------------------------
#include <stdio.h>
int create_magic_square(int n ,int magicsquare[n][n])
{ int i;
int a=(n-1)/2;
int b=0;
for(i=1;i<n*n;i++)
{ if(magicsquare[a][b]!=Null)
b=b+1;
magicsquare[a][b]=i;
a=a+1;
b=b-1;
if(a+1>n-1)
a=0;
if(b-1<0)
b=n-1;
}
}
int main(void)
{
int p;
int q[p][p];
scanf("%d",&p);
create_magic_square(p,q);
return 0;
}
-------------------------------------------
我想要寫一個create magic square 的function
書上是說從第一行中間從1開始2.3.4.5往右一格往上一格填
遇到不是空的就往下一格填 填滿就是一個 magic square
想說先test看看compile會不會過
結果出現許多錯誤訊息
3 'n' was not declared in this scope
8 `magicsquare'undeclared (first use this function)
8 `Null' undeclared (first use this function)
4 too many arguments to function `int create_magic_square(int)'
24 at this point in file
Null那邊我的想法是如果i跑到第n次時那一格裡面如果不是空的
就把值往下一格填 不過這邊我不太懂怎麼寫@@ 果然這樣是錯的
還有為什麼第三行那邊 我試著打書上其他範例進去compile都沒問題
可是我這裡就會說 n還沒宣告?
第八行也是@@
第三個跟第四個的錯誤我完全不懂@@
旁邊也沒有人可以問@@
希望板上有人能夠指點 感謝!!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.117.199.166
※ 編輯: honamida 來自: 122.117.199.166 (11/06 20:43)
推 otira:NULL 11/07 07:28
→ otira:int magicsquare[n][n] 請在function裡面動態產生 11/07 07:28
→ otira:等一下 11/07 07:30
推 otira:這樣main應該也會錯誤才對阿 11/07 07:40
→ otira:int q[p][p]; 11/07 07:40
推 otira:然後二維陣列不能這樣傳 11/07 07:50