已知二维int数组a[5][5],完成下列要求
(1)数组生成:数组每个元素的值随机产生(0-100的随机数)。
(2)显示数组各元素,要求整齐排列。
(3)将第1与第5行对调后,再显示新的数组。(4)求出每行元素的最大值,并指出其行号和列号。
运行结果及代码如下,如有帮助,请帮忙采纳一下,谢谢。
代码:
#include <iostream>
#include <math.h>
#include <time.h>
#include <iomanip>
using namespace std;
int main()
{
int a[5][5];
int i,j;
srand((unsigned int)time(NULL));
cout << "生成随机数组:"<<endl;
for (i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
a[i][j] = rand()%101;
cout << setw(4) << a[i][j];
}
cout << endl;
}
//对调
for (i=0;i<5;i++)
{
int t = a[0][i];
a[0][i] = a[4][i];
a[4][i] = t;
}
cout << "对调后数组:"<<endl;
for (i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
cout << setw(4) << a[i][j];
}
cout << endl;
}
//求每行的最大值
for (i=0;i<5;i++)
{
int max = a[i][0];
int index = 0;
for(j=1;j<5;j++)
{
if(a[i][j] > max)
{
max = a[i][j];
index = j;
}
}
cout <<"第"<<i+1 << "行的最大值是" << max <<",行号"<< i+1 <<",列号"<< index+1<< endl;
}
return 0;
}