accessing variable outside the loop

Thread Solved

Join Date: Sep 2007
Posts: 11
Reputation: theraven1982 is an unknown quantity at this point 
Solved Threads: 0
theraven1982 theraven1982 is offline Offline
Newbie Poster

accessing variable outside the loop

 
1
  #1
Sep 10th, 2007
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.  
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: accessing variable outside the loop

 
0
  #2
Sep 10th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: accessing variable outside the loop

 
0
  #3
Sep 10th, 2007
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.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 11
Reputation: theraven1982 is an unknown quantity at this point 
Solved Threads: 0
theraven1982 theraven1982 is offline Offline
Newbie Poster

Re: accessing variable outside the loop

 
0
  #4
Sep 10th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: accessing variable outside the loop

 
0
  #5
Sep 10th, 2007
> 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++ )
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 11
Reputation: theraven1982 is an unknown quantity at this point 
Solved Threads: 0
theraven1982 theraven1982 is offline Offline
Newbie Poster

Re: accessing variable outside the loop

 
0
  #6
Sep 10th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: accessing variable outside the loop

 
0
  #7
Sep 10th, 2007
also, learn to use c++ cin and cout to simplify your code. you can replace printf() with cout and scanf() with cin.
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 11
Reputation: theraven1982 is an unknown quantity at this point 
Solved Threads: 0
theraven1982 theraven1982 is offline Offline
Newbie Poster

Re: accessing variable outside the loop

 
0
  #8
Sep 10th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: accessing variable outside the loop

 
0
  #9
Sep 10th, 2007
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 11
Reputation: theraven1982 is an unknown quantity at this point 
Solved Threads: 0
theraven1982 theraven1982 is offline Offline
Newbie Poster

Re: accessing variable outside the loop

 
0
  #10
Sep 10th, 2007
That's alright ... We all have our hiccups .
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC