一维数组
创建一串连续的内存空间,每个数据是相同的数据类型
一维数组定义
//三种方式
//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、方便直接调用