Sorry for the minor problem but I need help!! I need to hand in this program in a few hours but I can't seem to find the parce error!!!
i get this when i compile itCode:#include<stdio.h> #include <stdlib.h> #include <time.h> main() { int a; int num[3][3]; srand((unsigned)time(NULL)); a = rand() % (7 + 1); /* getting random numbers 0-7*/ switch(a){ case 0: /* if a=0 and so on*/ num[3][3]={{8, 1, 6}, {3, 5, 7}, {4, 9, 2}}; printf("%d", num[3][3]); break; case 1: num[3][3]={{4, 3, 8}, {9, 5, 1}, {2, 7, 6}}; printf("%d", num[3][3]); break; case 2: num[3][3]={{2, 9, 4}, {7, 5, 3}, {6, 1, 8}}; printf("%d", num[3][3]); break; case 3: num[3][3]={{6, 7, 2}, {1, 5, 9}, {8, 3, 4}}; printf("%d", num[3][3]); break; case 4: num[3][3]={{6, 1, 8}, {7, 5, 3}, {2, 9, 4}}; printf("%d", num[3][3]); break; case 5: num[3][3]={{8, 3, 4}, {1, 5, 9}, {6, 7, 2}}; printf("%d", num[3][3]); break; case 6: num[3][3]={{4, 9, 2}, {3, 5, 7}, {8, 1, 6}}; printf("%d", num[3][3]); break; case 7: num[3][3]={{2, 7, 6}, {9, 5, 1}, {4, 3, 8}}; printf("%d", num[3][3]); break; } }
Code:Æ@DDK5LN1X ~ $ gcc 56.c 56.c: In function `main': 56.c:14: error: parse error before '{' token 56.c:18: error: parse error before '{' token 56.c:22: error: parse error before '{' token 56.c:26: error: parse error before '{' token 56.c:30: error: parse error before '{' token 56.c:34: error: parse error before '{' token 56.c:38: error: parse error before '{' token 56.c:42: error: parse error before '{' token
sorry for such a newbie question and programAll it is doing is randomly showing the possible answers to a 3x3 magic square according to the random number that is generated and inserted into int a.
You can not give new values to arrays, using the {}-operators. You can only do that, when you're initializing. You've to use the =-operator explicit.
Code:num[3][3] = 8; // Allowed num[3][3] = {{8, 1, 6}, {3, 5, 7}, {4, 9, 2}}; // Disallowed
thank you v0id for helping me everytime!!! It's always very helpful and I learned what I was doing wrong, but it still isn't working. This is what I've changed so far..
the program runs fine, but it only outputs one number. I really don't know how I can output the whole matrix...sorry for so many questionsCode:#include<stdio.h> #include <stdlib.h> #include <time.h> main() { int a,i,j; int num[2][2]; srand((unsigned)time(NULL)); a = rand() % (7 + 1); switch(a){ case 0: num[0][0]=8; num[0][1]=1; num[0][2]=6; num[1][0]=3; num[1][1]=5; num[1][2]=7; num[2][0]=4; num[2][1]=9; num[2][2]=2; for(i=0;i<2;i++){ for(j=0;j<2;j++){ printf("%3d",num[i][j]);}} break; case 1: num[0][0]=4; num[0][1]=3; num[0][2]=8; num[1][0]=9; num[1][1]=5; num[1][2]=1; num[2][0]=2; num[2][1]=7; num[2][2]=6; for(i=0;i<2;i++){ for(j=0;j<2;j++){ printf("%3d",num[i][j]);}} break; case 2: num[0][0]=2; num[0][1]=9; num[0][2]=4; num[1][0]=7; num[1][1]=5; num[1][2]=3; num[2][0]=6; num[2][1]=1; num[2][2]=8; for(i=0;i<2;i++){ for(j=0;j<2;j++){ printf("%3d",num[i][j]);}} break; case 3: num[0][0]=6; num[0][1]=7; num[0][2]=2; num[1][0]=1; num[1][1]=5; num[1][2]=9; num[2][0]=8; num[2][1]=3; num[2][2]=4; for(i=0;i<2;i++){ for(j=0;j<2;j++){ printf("%3d",num[i][j]);}} break; case 4: num[0][0]=6; num[0][1]=1; num[0][2]=8; num[1][0]=7; num[1][1]=5; num[1][2]=3; num[2][0]=2; num[2][1]=9; num[2][2]=4; for(i=0;i<2;i++){ for(j=0;j<2;j++){ printf("%3d",num[i][j]);}} break; case 5: num[0][0]=8; num[0][1]=3; num[0][2]=4; num[1][0]=1; num[1][1]=5; num[1][2]=9; num[2][0]=6; num[2][1]=7; num[2][2]=2; for(i=0;i<2;i++){ for(j=0;j<2;j++){ printf("%3d",num[i][j]);}} break; case 6: num[0][0]=4; num[0][1]=9; num[0][2]=2; num[1][0]=3; num[1][1]=5; num[1][2]=7; num[2][0]=8; num[2][1]=1; num[2][2]=6; for(i=0;i<2;i++){ for(j=0;j<2;j++){ printf("%3d",num[i][j]);}} break; case 7: num[0][0]=2; num[0][1]=7; num[0][2]=6; num[1][0]=9; num[1][1]=5; num[1][2]=1; num[2][0]=4; num[2][1]=3; num[2][2]=8; for(i=0;i<2;i++){ for(j=0;j<2;j++){ printf("%3d",num[i][j]);}} break; } }
I've responded to your other thread. In the response I provided an example, including how to fill and print matrices.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks