数组
数组就是一个集合,里面存放了相同类型的数据元素
特点:数组中给个元素都是相容的数据类型,在内存中连续存放
一维数组的的三种定义方式:
1. 数据类型 数组名[数组长度]
2. 数据类型 数组名[数组长度] = {值1, 值2, 值3}
3. 数组类型 数组名[] = {值1, 值2, 值3…}
#include<iostream>
using namespace std;
// time 系统时间头文件包含
#include<ctime>
int main()
{
// 1. 数据类型 数组名[数组长度]
int arr[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
// 访问数组元素
cout << arr[0] << endl;
cout << arr[0] << endl;
// 2. 数据类型 数组名[数组长度] = {值1, 值2, 值3}
// 如果在初始化数据的时候,没有全部填写,会用0填补
int arr2[5] = {10, 20, 30, 40, 50};
//cout << arr[1] << endl;
//cout << arr[2] << endl;
for (int i=0; i < 5; i++)
{
cout << arr2[i] << endl;
}
// 3. 数组类型 数组名[] = {值1, 值2, 值3...}
// 定义数组的时候,必须有定义初始的长度
int arr3[] = {1, 2, 3, 4, 5, 6, 7, 8};
for (int i = 0; i < 5; i++)
{
cout << arr3[i] << endl;
}
system("pause");
return 0;
}
2.一维数组名的用处
可以统计整个数组在内存中的长度,可以获取数组在内存中的首地址
#include<iostream>
using namespace std;
// time 系统时间头文件包含
#include<ctime>
int main()
{
// 数组名的用途,
// 1. 可以通过数组名统计整个数组占内存空间的大小
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
cout << sizeof(arr) << endl; // 输出40
cout << "每个元素占的空间大小" << sizeof(arr[0]) << endl;
cout << "数组中元素的个数" << sizeof(arr) / sizeof(arr[0]) << endl;
// 2. 可以通过数组名查看数组首地址
cout << "数组首地址为" << int(arr) << endl;
// 输出:数组首地址为10484088
cout << "数组中第一个元素的地址:" << &arr[0] << endl;
// 输出:数组中第一个元素的地址:008FFB20
cout << "转成十进制后,数组中第一个元素的地址:" << int(&arr[0]) << endl;
// 输出;数组中第一个元素的地址:10484088
cout << "转成十进制后,数组中第二个元素的地址:" << int(&arr[1]) << endl;
// 输出,给上面的加4
// 数组名是常量,不可以进行赋值操作
system("pause");
return 0;
}
两道练习题目
#include<iostream>
using namespace std;
// time 系统时间头文件包含
#include<ctime>
int main()
{
// 案例描述,在一个数组中记录了五只小猪的体重,如int arr[5] = {300, 350,200, 400, 250};
// 找出并打印最重的小猪体重
int arr[5] = {300, 350, 200, 400, 250};
int tem = 0;
for (int i = 0; i < 5; i++)
{
if (arr[i] > tem)
{
tem = arr[i];
}
}
cout << tem << endl;
// 案例练习2,数组逆置:声明一个5个元素的数组,并且将数组逆置
int arr2[5] = {1, 2, 3, 4, 5};
int arr3[5] = {};
// 自己写的答案
for (int i = 4; i>=0; i--)
{
arr3[4-i] = arr2[i];
}
cout << arr3 << endl;
// 老师的答案
// 实现数组逆置
// 1. 创建数组
int arr4[5] = { 1, 2, 3, 4, 5 };
cout << "数组逆置前" << endl;
for (int i = 0; i < 5; i++)
{
cout << arr[i] << endl;
}
// 2. 实现逆置
// 2.1 记录起始下标的位置
// 2.2 记录结束下标的位置
// 2.3 起始下标和结束下标元素的互换
// 2.4 起始位置++, 结束位置--
// 2.5 循环执行2.1的操作,直到起始位置 >= 结束位置
int start = 0; // 起始位置
int end = sizeof(arr4) / sizeof(arr4[0]) - 1; // 结束下标
while (start < end)
{
// 实现元素额互换
int temp = arr4[start];
arr4[start] = arr4[end];
arr4[end] = temp;
// 下标更行
start++;
end--;
}
// 3. 打印逆置后的数组
cout << "数组元素逆置后" << endl;
for (int i = 0; i < 5; i++)
{
cout << arr4[i] << endl;
}
system("pause");
return 0;
}
冒泡排序:
#include<iostream>
using namespace std;
// time 系统时间头文件包含
#include<ctime>
int main()
{
// 冒泡排序, 实现升序
int arr[] = { 4, 6, 7,1, 2, 3, 8, 9, 5 };
cout << "排序前的数组" << endl;
for (int i = 0; i < 9; i++)
{
cout << arr[i] << " ";
}
// 每轮对比个数:元素个数 - 排序的轮数 - 1
for (int i = 0; i < 9 - 1; i++)
{
// 内层循环对比,排序总轮数: 元素个数 - 1
for (int j = 0; j < 9 - i - 1; j++)
{
// 如果第一个数字比第二个数字大,就交换他们的位置
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "排序后的数组为" << endl;
for (int i = 0; i < 9; i++)
{
cout << arr[i] << " ";
}
system("pause");
return 0;
}
二维数组
// 1. 数据类型 数组名[行数][列数];
// 2. 数据类型 数组名[行数][列数] = {{数据1,数据2},{数据3, 数据4}}
// 3. 数据类型 数组名[行数][列数] = {数据1, 数据2, 数据3, 数据4}
// 4. 数据类型 数组名[][列数] = {数据1, 数据2, 数据3, 数据4}
#include<iostream>
using namespace std;
// time 系统时间头文件包含
#include<ctime>
int main()
{
// 四种定义方式
// 建议使用第二种方式,这样代码更直观
// 1. 数据类型 数组名[行数][列数];
int arr[2][3];
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[1][0] = 4;
arr[1][1] = 5;
arr[1][2] = 6;
// 打印: 外层循环打印行数,内层循环打印列数
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j] << endl;
}
}
// 2. 数据类型 数组名[行数][列数] = {{数据1,数据2},{数据3, 数据4}}
int arr2[2][3] =
{
{1, 2, 3},
{4, 5,6}
};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j] << endl;
}
}
// 3. 数据类型 数组名[行数][列数] = {数据1, 数据2, 数据3, 数据4}
// 程序会根据输入的行列数进行判断
int arr3[2][3] = {1, 2, 3, 4, 5, 6};
// 4. 数据类型 数组名[][列数] = {数据1, 数据2, 数据3, 数据4}
int arr4[][3] = { 1, 2, 3, 4, 5, 6 };
system("pause");
return 0;
}
二维数组名
查看二维二维数组所占内存空间
获取二维数组首地址
#include<iostream>
using namespace std;
// time 系统时间头文件包含
#include<ctime>
int main()
{
// 查看二维二维数组所占内存空间
int arr[2][3] =
{
{1, 2, 3},
{4, 5, 6}
};
cout << "二维数组占用的内存空间" << sizeof(arr) << endl;
cout << "二维数组第一行占用的内存为" << sizeof(arr[0]) << endl;
cout << "二维数组每个元素占的内存空间" << sizeof(arr[0][0]) << endl;
// 获取二维数组首地址
cout << "二维数组的首地址是:" << (int)arr << endl;
cout << "二维数组第一行的首地址:" << (int)arr[0] << endl;
cout << "二维数组第一个元素的首地址" << (int)&arr[0][0] << endl;
system("pause");
return 0;
}