gpt4 book ai didi

c - 在二维矩阵中向右移动部件

转载 作者:行者123 更新时间:2023-11-30 15:45:51 25 4
gpt4 key购买 nike

我希望得到您的帮助来理解并完成我的程序。

这是我必须做的:

"You must exercise program that:
First. An absorbing two - dimensional integer arr [M] [N]. M - number of rows N - number of columns. (Matrix size was received from the user)
Two. The program uses auxiliary functions "shift" moves the values ​​of the matrix to the right one place, as shown in the picture (2 entered instead of 1, 3 instead of 2, 4 instead of 3, ... 20 instead of 19, first place 20).
Shift have to write a function and call her three times in the sample matrix loop .."

示例 enter image description here

我的问题是:

  1. 我不知道如何处理用户输入大小的矩阵二维整数数组。我只知道行和列的定义大小
  2. 我的函数与真实情况并不接近,因此我希望获得帮助来完成我的函数。

我的输出:

my execution

我的代码:

#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#define M 4
#define N 5
void shift (int arr[M][N], int length);

void main()
{
int arr[M][N];
int i,j,length;

printf("Enter %d rows \n",M);
for (i=0 ; i<M ; i++ )
{
printf("Enter %d numbers:\n",N);
for(j=0 ; j<N ; j++ )
{
scanf("%d" , &arr[i][j] );
}
length=N+M;
}
shift (arr,length);
system("pause");
return ;
}

void shift (int arr[M][N], int length)
{
int i,j,temp;
temp=arr[0][0];
for(i=0; i<M; i++)
{
for(j=0; j<N-1 ; j++)
{
printf("%d ",arr[i][j]);

}
arr[i][j]=temp;
printf("\n");
}
}

编辑:调整图片大小

最佳答案

Shifts all columns to the right. 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


void shift_columns_right(int M[100][100], int rows, int cols) {

int tmp_lastcol;
int j, k;

for (j = 0; j<rows; j++){
tmp_lastcol = M[j][cols-1];
for (k = cols-1; k > 0; k-- ){
M[j][k] = M[j][k-1];
}
M[j][0] = tmp_lastcol;
}

}
int main(void){

int B[100] [100] = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16},
{17,18,19,20},
};

shift_columns_right(B,5,4);

return 0;

}

关于c - 在二维矩阵中向右移动部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18788245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com