[ create a new paste ] login | about

Link: http://codepad.org/wz1ntxB5    [ raw code | output | fork ]

C, pasted on May 20:
int main(int argc, char ** argv)
{
	int i,j,k;
	char buff[100];
	printf("input n;\n");
	fgets(buff, sizeof(buff), stdin);
	int n = atoi(buff);
	
	if(n < 0){printf("you must die.\n"); exit(-1);}
	
	double * pdbl1 = (double *)malloc(sizeof(double)*n*n);
	if(pdbl1 == NULL){printf("you are not lucky!\n");exit(-1);}
	double * pdbl2 = (double *)malloc(sizeof(double)*n*n);
	if(pdbl2 == NULL){printf("you are the most unlucky man in the world!\n");exit(-1);}

	for(i = 0 ; i < n ; i++)
	{
		for(j = 0 ; j < n ; j++)
		{
			fgets(buff, sizeof(buff), stdin);
			pdbl1[j + i * n] = atol(buff);
		}
	}
	for(i = 0 ; i < n ; i++)
	{
		for(j = 0 ; j < n ; j++)
		{
			fgets(buff, sizeof(buff), stdin);
			pdbl2[j + i * n] = atol(buff);
		}
	}
	printf("matrix A = \n");
	for(i = 0 ; i < n ; i++)
	{
		for(j = 0 ; j < n ; j++)
		{
			printf("%lf,", pdbl1[j + i * n]);
		}
		printf("\n");
	}
	printf("\n");
	printf("matrix B = \n");
	for(i = 0 ; i < n ; i++)
	{
		for(j = 0 ; j < n ; j++)
		{
			printf("%lf,", pdbl2[j + i * n]);
		}
		printf("\n");
	}
	
	printf("\n");
	printf("matrix A*B = \n");
	for(i = 0 ; i < n ; i++)
	{
		for(j = 0 ; j < n ; j++)
		{
			double total = 0.0;
			for(k = 0 ; k < n ; k++)
			{
				total += pdbl1[i * n + k]*pdbl2[k * n + j];
			}
			printf("%lf,", total);
		}
		printf("\n");
	}
	free(pdbl1);
	free(pdbl2);
	return 0;	
}


Output:
1
2
3
4
5
6
input n;
matrix A = 

matrix B = 

matrix A*B = 


Create a new paste based on this one


Comments: