DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

A^p Recursiv

11.19.2008
| 1926 views |
  • submit to reddit
        
#include "stdafx.h"


void mat(int A[10][10], int p, int B[10][10], int n )
{
	int i, j, k, C[10][10];
	if (p==0) {
		// B = In;
		for (i=0;i<n;i++)
			for (j=0;j<n;j++) {
				if (i==j)
					B[i][j] = 1;
				else
					B[i][j] = 0;
			};
	}
	else {
		mat(A, p-1, B, n);
		// C = A*B
		for (i=0;i<n;i++) 
			for (j=0;j<n;j++) {
				C[i][j] = 0;
				for (k=0;k<n;k++)
					C[i][j]+=A[i][k] * B[k][j];
			};
		// B = C;
		for (i=0;i<n;i++)
			for (j=0;j<n;j++)
				B[i][j] = C[i][j];

		
	}

}

int _tmain(void)
{
	int i, j, n, p, A[10][10], B[10][10];

	printf("n=");scanf("%d",&n);
	
	printf("p=");scanf("%d",&p);

	for (i=0;i<n;i++)
		for (j=0;j<n;j++) {
			printf("A[%d][%d]=",i,j); scanf("%d",&A[i][j]);
		};

	mat(A, p, B, n);
	// afisare
		printf("\n");
		for (i=0;i<n;i++) {
			printf("\n");
			for (j=0;j<n;j++) {
				printf(" %d", B[i][j]);
			}
		}

	return 0;
}