当前位置:网站首页>C language: on the right shift of matrix

C language: on the right shift of matrix

2022-06-24 00:16:00 Nianchi ichthyology programming

 Insert picture description here

#include <stdio.h>
#define N 4
void fun(int (*t)[N] , int m);
int main()
{
    
	int a[][N] = {
    21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10};
	int i,j,m;
	printf("\n The original matrix is :\n");
	for(i = 0 ; i < N ; i++){
    
		for(j = 0 ; j < N ; j++){
    
			printf("%4d",a[i][j]);
		}
		printf("\n");
	}
	printf(" Please enter the number of digits to shift right m(m<=%d):",N);
	scanf("%d",&m);
	fun(a,m);
	printf("\n The matrix shifted to the right is :\n");
	for(i = 0 ; i < N ; i++){
    
		for(j = 0 ; j < N ; j++){
    
			printf("%4d",a[i][j]);
		}
		printf("\n");
	}
	return 0;
}
void fun(int (*t)[N] , int m)
{
    
	int i,j;
	for(i = 0 ; i < N ; i++){
    
		for(j = 0 ; j < N ; j++){
    
			t[i][j+m] = t[i][j];
		}
		for(j = 0 ; j < m ; j++){
    
			t[i][j] = 0;
		}
	}
}
原网站

版权声明
本文为[Nianchi ichthyology programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206232150587650.html