我想得到你的帮助来理解和完成我的程序。
这是我必须做的:
“您必须执行以下程序:首先。一个吸收二维整数 arr [M] [N]。M - 行数 N - 列数。(从用户那里收到矩阵大小)二。程序使用辅助函数“shift”将矩阵的值右移一位,如图(输入2代替1,输入3代替2,输入4代替3,... 20代替19,第1位20 ). Shift 必须编写一个函数并在示例矩阵循环中调用她 3 次.."
示例图片:
错误信息:
在尝试解决我放弃的问题后,我希望得到您的帮助,以了解我的代码中有什么问题。和记忆有关吗?这是我的代码:
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "malloc.h"
void shift (int **arr,int rows,int cols);
void freemalloc ( int **arr,int rows);
void main()
{
int **arr,cols,rows;
int i,j;
printf("please insert rows and columns of the matrix: ");
scanf_s("%d%d",&rows,&cols);
arr=(int **)malloc(rows*sizeof(int *));
for(i=0; i<rows; i++)
arr[i]=(int *)malloc(cols*sizeof(int));
for (i=0; i<rows; i++)
for (j=0; j<cols; j++)
{
printf("rows %d , cols %d :\n", i, j);
scanf_s("%d", &arr[i][j]);
}
shift (arr,rows,cols);
freemalloc (arr,rows);
system("pause");
return ;
}
void shift (int **arr,int rows,int cols)
{
int i,j,temp=0;
for(i=0; i<rows ; i++ )
for( j=0 ; j<cols ; j++);
{
temp=arr[i][j];
arr[i][j]=arr[i][cols-1];
arr[i][cols-1]=temp;
}
for(i=0; i<rows ; i++)
{
for(j=0; j<cols ; j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
void freemalloc ( int **arr,int rows)
{
int i;
for (i=0 ; i<rows ; i++)
{
free(arr[i]);
}
free(arr);
}