C++入门_5数组

一维数组

创建一串连续的内存空间,每个数据是相同的数据类型

一维数组定义

//三种方式
	//1
	int ary[5];
	for (int i = 0; i < 5; i++) {
		ary[i] = i;
	}
	cout << ary[0] << endl;
	//2
	int ary2[5] = { 0,1,2,3,4 };
	cout << ary2[1] << endl;
	//3
	int ary3[] = {0,1,2,3,4,5,6,7,8,9};
	cout << ary3[7] << endl;

在这里插入图片描述

一维数组组名

数组名的作用
1、统计数组在内存中的长度
2、获取数组在内存中的首地址
3、方便直接调用
在这里插入图片描述

冒泡排序

循环n次,每次循环:只将相邻元素的大小排序,
每次循环会有一个最大或最小值在首或尾。

#include <iostream>
using namespace std;
#define week 7
#include <string>

int main()
{
	int ary[] = { 7,16,948,15,6,0,71,65,51 };
	int len = sizeof(ary) / sizeof(ary[0]);
	int temp = 0;
	for (int i = 0; i < len; i++)
	{
		for (int j = 0; j < len - i; j++)
		{
			if (ary[j] < ary[j + 1])
			{
				temp = ary[j];
				ary[j] = ary[j+1];
				ary[j+1] = temp;

			}
		}
	}
	for (int i = 0; i <= len; i++)
	{
		cout << ary[i] << endl;
	}
	system("pause");
	return 0;
}

在这里插入图片描述

二维数组

二维数组定义

//四种方法
	//1
	int ary1[2][2];
	ary1[0][0] = 1;
	ary1[0][1] = 2;
	ary1[1][0] = 3;
	ary1[1][1] = 4;

	//2
	int ary2[2][2] = { 
		{1,2},
		{3,4} 
	};

	//3
	int ary3[2][2] = { 1,2,3,4 };

	//4
	int ary4[][2] = { 1,2,3,4 };
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			cout << ary4[i][j] << " ";
		}
		cout << endl;
	}

在这里插入图片描述

二维数组名

1、统计数组在内存中的长度
2、获取数组在内存中的首地址(看元素地址 要加取址符 &)
3、方便直接调用

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值