按如下函数原型编程从键盘输入一个m行n列的二维数组,然后根据输入mode值将数组进行水平翻转,垂直翻转或转置操作。其中数组行列数m,n,操作方式mode的值由用户键盘输入。已知m和n的值都不超过10,mode=0时水平翻转数组,mode=1时垂直翻转数组,mode =2时转置数组。向二维数组输入数据,数组输出和操作数组的函数原型如 下: void InputArray(int a[][N],int m,int n); void OutputArray(int a[][N],int m, int n); int ArrayOp(int a[][N],int m,int n,int mode); 程序运行结果示例: Input m,n: 3,3↙ Input 3*3 array: 2 5 7↙ 9 0 2↙ 3 -4 9↙ Input operation mode: 0 After operation: 7 5 2 2 0 9 9 -4 3 Input m,n: 3,3↙ Input 3*3 array: 2 5 7↙ 9 0 2↙ 3 -4 9↙ Input operation mode: 1 After operation: 3 -4 9 9 0 2 2 5 7 Input m,n: 3,3↙ Input 3*3 array: 2 5 7↙ 9 0 2↙ 3 -4 9↙ Input operation mode: 2 After operation: 2 9 3 5 0 -4 7 2 9 输入格式: 输入数组大小:"%d,%d" 输入数组元素:"%d" 输入操作模式:"%d" 输出格式: 数组大小输入提示信息:"Input m,n:\n" 数组元素输入提示信息:"Input %d*%d array:\n" 操作模式输入提示信息 "Input operation mode:\n" 操作后数组输出提示信息"After operation:\n" 数组输出格式: "%4d"
时间: 2023-06-17 10:05:09 浏览: 348
函数:int **array2(int **arr,int n,int m) , C++ 输入二维数组 ,返回值二维数组
5星 · 资源好评率100%
```python
#include <stdio.h>
#define N 10
void InputArray(int a[][N], int m, int n);
void OutputArray(int a[][N], int m, int n);
int ArrayOp(int a[][N], int m, int n, int mode);
int main()
{
int a[N][N];
int m, n, mode;
printf("Input m,n:\n");
scanf("%d,%d", &m, &n);
InputArray(a, m, n);
printf("Input operation mode:\n");
scanf("%d", &mode);
printf("After operation:\n");
ArrayOp(a, m, n, mode);
OutputArray(a, m, n);
return 0;
}
void InputArray(int a[][N], int m, int n)
{
printf("Input %d*%d array:\n", m, n);
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
}
void OutputArray(int a[][N], int m, int n)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("%4d", a[i][j]);
}
printf("\n");
}
}
int ArrayOp(int a[][N], int m, int n, int mode)
{
int tmp[N][N];
switch (mode)
{
case 0: //水平翻转
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
tmp[i][n - j - 1] = a[i][j];
}
}
break;
case 1: //垂直翻转
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
tmp[m - i - 1][j] = a[i][j];
}
}
break;
case 2: //转置
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
tmp[j][i] = a[i][j];
}
}
int t = m;
m = n;
n = t;
break;
}
//将tmp中的数据拷贝回a
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = tmp[i][j];
}
}
return 0;
}
```
阅读全文