例5.1定义一个数组并按逆序输出:
#include<iostream>
using namespace std;
int max(int, int);
int main()
{
int i,a[10];
for (i = 0; i <= 9; i++)
a[i] = i; //使a[0]-a[9]的值为0-9
for (i = 9; i >= 0; i--) // 逆序输出
cout << a[i] << " ";
return 0;
}
执行结果:
例5.2用数组来处理Fibonacci数列问题:
#include<iostream>
using namespace std;
#include<iomanip>
int max(int, int);
int main()
{
int i;
int f[20] = { 1, 1 };
for (i = 2; i < 20; i++)
f[i] = f[i - 2] + f[i - 1];
for (i = 0; i < 20; i++)
{
if (i % 5 == 0)cout << endl;//输出5个数后换行
cout << setw(8) << f[i]; //每个数据占8列宽度
}
cout << endl;
return 0;
}
执行效果如图:
例5.3用冒泡法对10个数从小到大排序:
有n个数要进行n轮比较,第j轮要进行n-j次比较:`
#include<iostream>
using namespace std;
#include<iomanip>
int main()
{
int a[11];
int i, j, t;
cout << "input 10 numbers :" << endl;
for (i = 1; i < 11; i++)
cin >> a[i];
cout << endl;
for (j = 1; j <= 9; j++) //共进行9轮比较
for (i = 1; i <= 10 - j;i++) //第j轮要进行10-j次比较
if (a[i]>a[i + 1])
{
t = a[i]; a[i] = a[i + 1]; a[i + 1] = t;
}
cout << "the sorted numbers:" << endl;
for (i = 1; i < 11; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}
执行效果如图:
例5.4将一个2x3的二维数组变成3x2的数组:
#include<iostream>
using namespace std;
#include<iomanip>
int main()
{
int a[2][3] = { { 1, 2, 3 },{4,5,6} };
int b[3][2], i, j;
cout << "array a:" << endl;
for (i = 0; i <= 1; i++)
{
for (j = 0; j <= 2;j++)
{
cout << a[i][j] << " ";
b[j][i] = a[i][j];//将a的i行j列值赋给b的j列i行
}
cout << endl;
}
cout << "array b:" << endl;
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= 1; j++)
cout << b[i][j] << " ";//按行输出b数组
cout << endl;
}
return 0;
}
例5.5,3x4矩阵求出其中最大的数并输出行号和列号:
#include<iostream>
using namespace std;
#include<iomanip>
int main()
{
int i, j, row = 0, colum = 0, max;
int a[3][4] = { { 5, 12, 23, 56 }, { 19, 28, 37, 46 }, { -12, -34, 6, 8 } };
max = a[0][0];
for (i = 0; i <= 2; i++)
for(j = 0; j <= 3;j++)
if (a[i][j]>max)
{
max = a[i][j];
row = i;
colum = j;
}
cout << "max=" << max << ",roe=" << row << ",colum=" << colum << endl;
return 0;
}
```输出结果如下:
max56,row=0,colum=3
例5.6。例5.5还可以用调用函数实现:
```cpp
#include<iostream>
using namespace std;
#include<iomanip>
int main()
{
int max_value(int x, int max);
int i, j, row = 0, colum = 0, max;
int a[3][4] = { { 5, 12, 23, 56 }, { 19, 28, 37, 46 }, { -12, -34, 6, 8 } };
max = a[0][0];
for (i = 0; i <= 2; i++)
for(j = 0; j <= 3;j++)
{
max = max_value(a[i][j], max);
if (max == a[i][j])
{
row = i;
colum = j;
}
}
cout << "max=" << max << ",roe=" << row << ",colum=" << colum << endl;
}
int max_value(int x, int max)
{
if (x > max)return x;
else return max;
}
```结果是一样的
例5.7用选择法对数组十个整数由小到大排序:
```cpp
#include<iostream>
using namespace std;
int main()
{
void select_sort(int array[], int n);
int a[10], i;
cout << "enter the originl array:" << endl;
for (i = 0; i < 10; i++)
cin >> a[i];
cout << endl;
select_sort(a, 10);//函数调用,数组名做实参
cout << "the sorted array:" << endl;
for (i = 0; i < 10; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}
void select_sort(int array[], int n)
{
int i, j, k, t;
for (i = 0; i < n - 1; i++)
{
k = i;
for (j = i + 1; j < n;j++)
if (array[j] < array[k])
k = j;
t = array[k]; array[k] = array[i]; array[i] = t;
}
}
运行效果如下图: