编写一个包含函数add(int a[4][4],int b)的程序,将一个4*4的矩阵中每列的最大值放到数组b中并转置
时间: 2024-05-13 21:18:31 浏览: 62
以下是包含函数add(int a[4][4],int b)的程序,其功能是将一个4*4的矩阵中每列的最大值放到数组b中并转置:
```cpp
#include <iostream>
using namespace std;
void add(int a[4][4], int b[]) {
// 找出每列的最大值并存储到数组b中
for (int j = 0; j < 4; j++) {
int maxVal = a[0][j];
for (int i = 1; i < 4; i++) {
if (a[i][j] > maxVal) {
maxVal = a[i][j];
}
}
b[j] = maxVal;
}
// 转置矩阵
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (j == i) {
continue;
}
int temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
}
int main() {
int a[4][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
int b[4];
add(a, b);
cout << "转置后的矩阵为:" << endl;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
cout << "每列的最大值为:" << endl;
for (int i = 0; i < 4; i++) {
cout << b[i] << " ";
}
return 0;
}
```
输出结果如下:
```
转置后的矩阵为:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
每列的最大值为:
13 14 15 16
```
阅读全文