二维数组实现n阶方阵

1.二维数组实现n阶方阵

#include
int main() {
	int  n,x,i,j, a[6][6];
	printf("请输入一个整数n,其中1<=n<=6\n");
	scanf("%d",&n);
	printf("请输入一个n阶矩阵\n");
	for (i=0;i<n;i++) {
		for (j=0;j<n;j++) {
			scanf("%d",&x);
			a[i][j]=x;
		}
	} 
	for (i=0;i<n;i++) {
		for (j=0;j<n;j++) {
			printf("%d ",a[i][j]);
			if (a[i][j]>=0&&a[i][j]<10)
	     	printf("  ");
	     else
		    printf(" ");
		} 
	  printf("\n");	 	
	}
	
	
}

2.转置矩阵

#include
int main() {
	int  n,x,i,j, a[6][6];
	printf("请输入一个整数n,其中1<=n<=6\n");
	scanf("%d",&n);
	printf("请输入一个n阶矩阵\n");
	for (i=0;i<n;i++) {
		for (j=0;j<n;j++) {
			scanf("%d",&x);
			a[i][j]=x;
		}
	} 
	for (j=0;j<n;j++) {
		for (i=0;i<n;i++) {
			printf("%d ",a[i][j]);
			if (a[i][j]>=0&&a[i][j]<10)
	     	printf("  ");
	     else
		    printf(" ");
		} 
	  printf("\n");	 	
	}
	
	
}

3.矩阵加法

#include
   
int main() {
	int A,B, n,x,i,j,a[6][6],b[6][6];
	printf("请输入一个整数n,其中1<=n<=6\n");
	scanf("%d",&n);
	printf("请输入一个n阶矩阵\n");
	for (i=0;i<n;i++) {
		for (j=0;j<n;j++) {
			scanf("%d",&x);
			a[i][j]=x;
		}
	}
		printf("请输入一个整数n,其中1<=n<=6\n");
	scanf("%d",&n);
	printf("请输入一个n阶矩阵\n");
	for (i=0;i<n;i++) {
		for (j=0;j<n;j++) {
			scanf("%d",&x);
			b[i][j]=x;
		}
	}
	for (i=0;i<n;i++) {
		for (j=0;j<n;j++) {
			A=a[i][j];
			B=b[i][j];
			a[i][j]=A+B;
			printf("%d ",a[i][j]);
			if (a[i][j]>=0&&a[i][j]<10)
	     	printf("  ");
	     else
		    printf(" ");
		}
			  printf("\n");	
	}
	

	
	
}

3.矩阵乘法

#include
   
int main() {
	int A,B, n,x,i,j,k,a[6][6],b[6][6],c[6][6];
	printf("请输入一个整数n,其中1<=n<=6\n");
	scanf("%d",&n);
	printf("请输入一个n阶矩阵\n");
	for (i=0;i<n;i++) {
		for (j=0;j<n;j++) {
			scanf("%d",&x);
			a[i][j]=x;
		}
	}
		printf("请输入一个整数n,其中1<=n<=6\n");
	scanf("%d",&n);
	printf("请输入一个n阶矩阵\n");
	for (i=0;i<n;i++) {
		for (j=0;j<n;j++) {
			scanf("%d",&x);
			b[i][j]=x;
		}
	}
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];
		}
	}
}
	
	for (i=0;i<n;i++) {
		for (j=0;j<n;j++) {
			printf("%d ",c[i][j]);
			if (c[i][j]>=0&&c[i][j]<10)
	     	printf("  ");
	     else
		    printf(" ");
		}
			  printf("\n");	
	}
	
	

	
	
}

你可能感兴趣的:(二维数组)