943,696 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2364
  • C RSS
Sep 10th, 2007
1

accessing variable outside the loop

Expand Post »
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,

  1.  
  2.  
  3. #include <stdio.h>
  4.  
  5. int main()
  6. {
  7. printf("Multiply two square matrices\n");
  8.  
  9. printf("What's the size of the matrix?\n");
  10. int size;
  11. scanf("%d", &size);
  12. while (size!=2)
  13. {
  14. printf("Only 2x2 matrices allowed at the time.\n");
  15. printf("Just answer 2 here ^^ .\n");
  16. scanf("%d", &size);
  17. }
  18.  
  19.  
  20. printf("First, matrix A\n");
  21.  
  22. int i,j;
  23. for (i = 1; i <=size; i++)
  24. {
  25. for (j = 1; j <=size ; j++)
  26. {
  27. printf("What is element [%d][%d]", i, j);
  28. float elementA[i][j];
  29. scanf("%f", &elementA[i][j]);
  30. printf("elelemnt [%d][%d] is %f\n", i,j,elementA[i][j]);
  31. }
  32. }
  33.  
  34. printf("Second, matrix B\n");
  35.  
  36. int k,m;
  37. for (k = 1; k <=size; k++)
  38. {
  39. for (m = 1; m <=size ; m++)
  40. {
  41. printf("What is element [%d][%d]", k,m);
  42. float elementB[k][m];
  43. scanf("%f", &elementB[k][m]);
  44. printf("elelement [%d][%d] is %f\n", k,m,elementB[k][m]);
  45. }
  46. }
  47.  
  48. newmatrix[1][2]=elementA[1][1]*elementB[1][2] +elementA[1][2]*elementB[2][2]; /* This doesn't work /*
  49.  
  50. }
  51.  
Similar Threads
Reputation Points: 29
Solved Threads: 0
Newbie Poster
theraven1982 is offline Offline
11 posts
since Sep 2007
Sep 10th, 2007
0

Re: accessing variable outside the loop

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Sep 10th, 2007
0

Re: accessing variable outside the loop

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.
  1. int i;
  2.  
  3. for ( i = 0; i < 10; ++i ) {
  4. int x = i;
  5. }
  6.  
  7. printf( "%d\n", x ); /* Won't work! */
To get to x you have to declare it in at least the scope that you're using it.
  1. int x;
  2. int i;
  3.  
  4. for ( i = 0; i < 10; ++i ) {
  5. x = i; /* Still works! */
  6. }
  7.  
  8. printf( "%d\n", x ); /* Works now! */
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 x = i; still works even though x is declared in the enclosing scope.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Sep 10th, 2007
0

Re: accessing variable outside the loop

Thank you both for your answers! They were very useful.
Now I have another problem (after that, I'll try to be quiet ).

  1.  
  2. #include <stdio.h>
  3.  
  4. int main()
  5. {
  6. printf("Multiply two square matrices\n");
  7.  
  8. printf("First, matrix A\n");
  9.  
  10. float elementB[2][2];
  11. float elementA[2][2];
  12.  
  13. int p,q;
  14. for (p = 1; p <=2; p++)
  15. {
  16. for (q = 1; q <=2 ; q++)
  17. {
  18. printf("What is element [%d][%d]",p, q);
  19. scanf("%f", &elementA[p][q]);
  20. printf("elelemnt [%d][%d] is %f\n", p,q,elementA[p][q]);
  21. }
  22. }
  23.  
  24. printf("Second, matrix B\n");
  25.  
  26. float test;
  27. int i,j;
  28. for (i = 1; i <=2; i++)
  29. {
  30. for (j = 1; j <=2 ; j++)
  31. {
  32. printf("What is element [%d][%d]",i, j);
  33. scanf("%f", &elementB[i][j]);
  34. printf("element [%d][%d] is %f\n", i,j,elementB[i][j]);
  35.  
  36. }
  37. }
  38.  
  39. printf("garbage:");
  40. scanf("%f",&test);
  41.  
  42.  
  43. float newmatrix[2][2];
  44. newmatrix[1][1]=elementA[1][1]*elementB[1][1]+elementA[1][2]*elementB[2][1];
  45. newmatrix[1][2]=elementA[1][1]*elementB[1][2]+elementA[1][2]*elementB[2][2];
  46. newmatrix[2][1]=elementA[2][1]*elementB[1][1]+elementA[2][2]*elementB[2][1];
  47. newmatrix[2][2]=elementA[2][1]*elementB[1][2]+elementA[2][2]*elementB[2][2];
  48.  
  49. int s,t;
  50. for (s = 1; s <=2; s++)
  51. {
  52. for (t = 1; t <=2 ; t++)
  53. {
  54. printf("elelemnt [%d][%d] is %f\n", s,t,newmatrix[s][t]);
  55. }
  56. }
  57.  
  58. return 0;
  59. }

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!
Reputation Points: 29
Solved Threads: 0
Newbie Poster
theraven1982 is offline Offline
11 posts
since Sep 2007
Sep 10th, 2007
0

Re: accessing variable outside the loop

> for (p = 1; p <=2; p++)
Arrays start at subscript 0, not 1.
You're trashing all over someone else's memory, hence the segfaults at some point.

Given an array of size N, the normal loop would be
for ( i = 0 ; i < N ; i++ )
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 10th, 2007
0

Re: accessing variable outside the loop

Thanks. It's a bit silly I didn't think of it ... Well, now I'll never forget .

Thanks a lot to all of you; not only for this thread, but in all other threads you helped people. Much appreciated!
Last edited by theraven1982; Sep 10th, 2007 at 5:15 pm.
Reputation Points: 29
Solved Threads: 0
Newbie Poster
theraven1982 is offline Offline
11 posts
since Sep 2007
Sep 10th, 2007
0

Re: accessing variable outside the loop

also, learn to use c++ cin and cout to simplify your code. you can replace printf() with cout and scanf() with cin.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Sep 10th, 2007
0

Re: accessing variable outside the loop

I started learning C++ a long long time ago (never finished it though), but just started learning C with a tutorial. The tutorial is not very elaborate, and I didn't know i could use cin cout as well in C.
Thanks again.
Reputation Points: 29
Solved Threads: 0
Newbie Poster
theraven1982 is offline Offline
11 posts
since Sep 2007
Sep 10th, 2007
0

Re: accessing variable outside the loop

Oops! my error -- you are correct that you can not use cin and cout in C. I had c++ in mind. Sorry for that gaff.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Sep 10th, 2007
0

Re: accessing variable outside the loop

That's alright ... We all have our hiccups .
Reputation Points: 29
Solved Threads: 0
Newbie Poster
theraven1982 is offline Offline
11 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Opening files based on their extensions
Next Thread in C Forum Timeline: matrix transpose





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC