| | |
accessing variable outside the loop
Thread Solved |
•
•
Join Date: Sep 2007
Posts: 11
Reputation:
Solved Threads: 0
Hello,
I've just started learning C, and now I have a problem (and I couldn't find a solution on the interweb
).
I want to multiply two matrices (2x2). First it 'reads' two matrices, and after that it needs to multiply it.
It reads the matrices using double for-loops; this works great. However, now I want to multiply the values of both matrices, but I can't access the variables outside the for-loops. I tried adding 'static' or 'extern' keywords before the declaration of the matrix, but that doesn't help...
Any idea would be greatly appreciated.
Thanks,
I've just started learning C, and now I have a problem (and I couldn't find a solution on the interweb
).I want to multiply two matrices (2x2). First it 'reads' two matrices, and after that it needs to multiply it.
It reads the matrices using double for-loops; this works great. However, now I want to multiply the values of both matrices, but I can't access the variables outside the for-loops. I tried adding 'static' or 'extern' keywords before the declaration of the matrix, but that doesn't help...
Any idea would be greatly appreciated.
Thanks,
C Syntax (Toggle Plain Text)
#include <stdio.h> int main() { printf("Multiply two square matrices\n"); printf("What's the size of the matrix?\n"); int size; scanf("%d", &size); while (size!=2) { printf("Only 2x2 matrices allowed at the time.\n"); printf("Just answer 2 here ^^ .\n"); scanf("%d", &size); } printf("First, matrix A\n"); int i,j; for (i = 1; i <=size; i++) { for (j = 1; j <=size ; j++) { printf("What is element [%d][%d]", i, j); float elementA[i][j]; scanf("%f", &elementA[i][j]); printf("elelemnt [%d][%d] is %f\n", i,j,elementA[i][j]); } } printf("Second, matrix B\n"); int k,m; for (k = 1; k <=size; k++) { for (m = 1; m <=size ; m++) { printf("What is element [%d][%d]", k,m); float elementB[k][m]; scanf("%f", &elementB[k][m]); printf("elelement [%d][%d] is %f\n", k,m,elementB[k][m]); } } newmatrix[1][2]=elementA[1][1]*elementB[1][2] +elementA[1][2]*elementB[2][2]; /* This doesn't work /* }
simple solution -- move lines 28 and 42 up to line 7 so that its scope is to the entire function. Also you can't declare a matrix using non-static number of elements. If you don't know the size of the matrix at compile time then you have to allocate the dimensions with malloc()
Last edited by Ancient Dragon; Sep 10th, 2007 at 12:10 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
You can only access an object if it was declared in the same scope or an enclosing scope. That's the techno babble explanation.
What it means is that if you declare something inside a loop, you can't use it outside the loop because the body of a loop counts as its own scope.
To get to x you have to declare it in at least the scope that you're using it.
By at least the scope you're using it in, that means it can be declared in a higher enclosing scope and you can use it in the nested scope. That's why
What it means is that if you declare something inside a loop, you can't use it outside the loop because the body of a loop counts as its own scope. c Syntax (Toggle Plain Text)
int i; for ( i = 0; i < 10; ++i ) { int x = i; } printf( "%d\n", x ); /* Won't work! */
c Syntax (Toggle Plain Text)
int x; int i; for ( i = 0; i < 10; ++i ) { x = i; /* Still works! */ } printf( "%d\n", x ); /* Works now! */
x = i; still works even though x is declared in the enclosing scope.
The truth does not change according to our ability to stomach it.
•
•
Join Date: Sep 2007
Posts: 11
Reputation:
Solved Threads: 0
Thank you both for your answers! They were very useful.
Now I have another problem (after that, I'll try to be quiet
).
It's about lines 39/40. If I don't include them, the program will segfault. Why is that? There's another problem with it, but I think it'll go away if this is solved first.
again, much appreciated!
Now I have another problem (after that, I'll try to be quiet
). C Syntax (Toggle Plain Text)
#include <stdio.h> int main() { printf("Multiply two square matrices\n"); printf("First, matrix A\n"); float elementB[2][2]; float elementA[2][2]; int p,q; for (p = 1; p <=2; p++) { for (q = 1; q <=2 ; q++) { printf("What is element [%d][%d]",p, q); scanf("%f", &elementA[p][q]); printf("elelemnt [%d][%d] is %f\n", p,q,elementA[p][q]); } } printf("Second, matrix B\n"); float test; int i,j; for (i = 1; i <=2; i++) { for (j = 1; j <=2 ; j++) { printf("What is element [%d][%d]",i, j); scanf("%f", &elementB[i][j]); printf("element [%d][%d] is %f\n", i,j,elementB[i][j]); } } printf("garbage:"); scanf("%f",&test); float newmatrix[2][2]; newmatrix[1][1]=elementA[1][1]*elementB[1][1]+elementA[1][2]*elementB[2][1]; newmatrix[1][2]=elementA[1][1]*elementB[1][2]+elementA[1][2]*elementB[2][2]; newmatrix[2][1]=elementA[2][1]*elementB[1][1]+elementA[2][2]*elementB[2][1]; newmatrix[2][2]=elementA[2][1]*elementB[1][2]+elementA[2][2]*elementB[2][2]; int s,t; for (s = 1; s <=2; s++) { for (t = 1; t <=2 ; t++) { printf("elelemnt [%d][%d] is %f\n", s,t,newmatrix[s][t]); } } return 0; }
It's about lines 39/40. If I don't include them, the program will segfault. Why is that? There's another problem with it, but I think it'll go away if this is solved first.
again, much appreciated!
![]() |
Similar Threads
- C++ nest loop program - Help (C++)
- Using a variable as an array's index? (C)
- Accessing a variable of another class (Java)
- while loop in c++ help needed (C++)
- Clear getchar in a loop (C)
Other Threads in the C Forum
- Previous Thread: Opening files based on their extensions
- Next Thread: matrix transpose
| Thread Tools | Search this Thread |
#include * adobe ansi array asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list locate looping lowest match matrix meter microsoft number oddnumber opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






