第6章 数组、指针与字符串
![](https://img2022.cnblogs.com/blog/1892247/202210/1892247-20221015022858323-353651576.png)
注意new的使用
lab6_2.cpp
#include <iostream>
using namespace std;
void swap(int &a, int &b)
{
/********** Begin **********/
int tmp = a;
a = b;
b = tmp;
/********** End **********/
}
int main()
{
/********** Begin **********/
cout << "输入9个整数作为矩阵元素值" << endl;
int **a;
a = new int *[3];
for (int i = 0; i < 3; ++i)
a[i] = new int[3];
cout << "初始矩阵:" << endl;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
cin >> a[i][j], cout << a[i][j] << ' ';
cout << endl;
}
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i; ++j)
swap(a[i][j], a[j][i]);
cout << "转置矩阵:" << endl;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
cout << a[i][j] << ' ';
cout << endl;
}
/********** End **********/
return 0;
}
lab6_3.cpp
#include <iostream>
#include <cstring>
using namespace std;
char *link(char *a, char *b)
{
/********** Begin **********/
int lena = strlen(a), lenb = strlen(b), len = lena + lenb;
static char res[256];
for (int i = 0; i < lena; ++i)
res[i] = a[i];
for (int i = 0; i < lenb; ++i)
res[i + lena] = b[i];
return res;
/********** End **********/
}
int main()
{
char a[128], b[128];
cin >> a >> b;
cout << link(a, b) << endl;
return 0;
}
EntyEnty520~