C++程序设计之数组(谭浩强5.1-5.7)

例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=35.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;
	}
}

运行效果如下图:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值