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,

#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 /*

}
Ancient Dragon commented: Thanks for correctly using code tags :) +18

Recommended Answers

All 9 Replies

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.

int i;

for ( i = 0; i < 10; ++i ) {
  int x = i;
}

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.

int x;
int i;

for ( i = 0; i < 10; ++i ) {
  x = i; /* Still works! */
}

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. :)

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()

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

#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!

> 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++ )

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!

also, learn to use c++ cin and cout to simplify your code. you can replace printf() with cout and scanf() with cin.

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.

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. :icon_redface:

That's alright ... We all have our hiccups ;).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.