描述
编写一个函数,输入参数是5 * 5的二维数组,和n,m两个行下标。功能:判断n,m是否在数组范围内,如果不在,则返回0;如果在范围内,则将n行和m行交换,并返回1。
在main函数中, 生成一个5*5的矩阵,输入矩阵数据,并输入n,m的值。调用前面的函数。如果返回值为0,输出error。如果返回值为1,输出交换n,m后的新矩阵。
输入
5*5矩阵的数据,以及n和m的值。
输出
如果不可交换,则输出error;
如果可交换,则输出新矩阵
/*描述
编写一个函数,输入参数是5*5的二维数组,和n,m两个行下标。
功能:判断n,m是否在数组范围内,如果不在,则返回0;如果在范围内,则将n行和m行交换,并返回1。
在main函数中, 生成一个5*5的矩阵,输入矩阵数据,并输入n,m的值。
调用前面的函数。如果返回值为0,输出error。
如果返回值为1,输出交换n,m后的新矩阵。
输入
5*5矩阵的数据,以及n和m的值。
输出
如果不可交换,则输出error;
如果可交换,则输出新矩
*/
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <algorithm>
using namespace std;
int test (int a[5][5],int m,int n)
{
if(m>4||m<0||n>4||n<0)
{
return 0;
}
else
{
int temp [5];
for(int j=0; j<5; j++)
{
temp[j]=a[n][j];
a[n][j]=a[m][j];
a[m][j]=temp[j];
}
return 1;
}
}
int main()
{
int a[5][5];
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cin>>a[i][j];
}
}
int m,n;
cin>>m>>n;
int t=test(a,m,n);
if(t==0)
cout<<"error"<<endl;
if(t==1)
{
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
cout<< setw(4)<<a[i][j];
cout<<endl;
}
}
}