5 数组
时间:2023-9-18 22:30 作者:wen 分类: C++
[toc]
数组
概述
所谓数组,就是一个集合,里面存放了相同类型的数据元素
特点1:数组中的每个数据元素都是相同的数据类型。
特点2:数组是由连续的内存位置组成的。
一维数组
一维数组定义方式
一维数组定义的三种方式:
数据类型 数组名[数组长度]
数据类型 数组名[数组长度] = {值1,值2 ...}
数据类型 数组名[] = {值1,值2 ...}
示例:
#include <iostream>
using namespace std;
int main()
{
int arr2[2];
for (int i = 0; i < 2; i++)
{
cout << arr2[i] << endl;
}
// 如果在初始化数据时候,没有全部填完,会用0来填补数据
int arr[5] = { 10,20,30,40,50 };
for (int i = 0; i < 5; i++)
{
cout << arr[i] << endl;
}
// 定义数组的时候,必须有初始长度
int arr1[] = { 90,80,70,60,50,40,30,20,10 };
for (int i = 0; i < 9; i++)
{
cout << arr1[i] << endl;
}
system("pause");
return 0;
}
一维数组数组名
一维数组名称的用途:
- 可以统计整个数组在内存中的长度
- 可以获取数组在内存中的首地址
示例:
#include <iostream>
using namespace std;
int main()
{
int arr[5] = { 10,20,30,40,50 };
// 1.可以统计整个数组在内存中的长度
cout << "整个数组占用内存空间为:" << sizeof(arr) << endl;
// 2.可以获取数组在内存中的首地址
cout << "数组首地址:" << arr << endl;
cout << "数组第一个元素的地址:" << &arr[0] << endl;
// 数组名是一个常量,不可以进行赋值
system("pause");
return 0;
}
练习案例1:五只小猪陈体重
案例描述:一个数组中记录了五只小猪的体重,如:int arr[5] = {300,350,200,400,250};
找出并打印最重的小猪体重。
#include <iostream>
using namespace std;
int main()
{
int arr[5] = { 300,350,200,400,250 };
int max = arr[0];
for (int i = 1; i < 5; i++)
{
if (arr[i] > max) {
max = arr[i];
}
}
cout << "最重的小猪为:" << max << endl;
system("pause");
return 0;
}
练习案例:数组元素逆置
案例描述:请声明一个,5个元素的数组,并且将元素逆置
如原数组元素为:1,3,2,5,4,逆置后输出结果为:4,5,2,3,1
#include <iostream>
using namespace std;
int main()
{
int arr[5] = { 1,3,2,5,4 };
cout << "逆置前元素:" << endl;
for (int i = 0; i < 5; i++)
{
cout << arr[i] << endl;
}
int start = 0;
int end = sizeof(arr) / sizeof(int) - 1;
while (start > end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
cout << "逆置后元素:" << endl;
for (int i = 0; i < 5; i++)
{
cout << arr[i] << endl;
}
system("pause");
return 0;
}
冒泡排序
作用:最常用的排序算法,对数组内元素进行排序。
- 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
- 对每一对相邻元素做同样的工作,执行完毕后,找到第一个最大值。
- 重复以上步骤。每次比较次数-1,直到不需要比较。
示例:将数组{4,2,8,0,5,7,1,3,9}进行升序排序
#include <iostream>
using namespace std;
int main()
{
// 冒泡排序
int arr[] = { 4,2,8,0,5,7,1,3,9 };
for (int i = 0; i < 9 - 1; i++)
{
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] << endl;
}
system("pause");
return 0;
}
二维数组
二维数组就是在一维数组上,多加一个维度
二维数组定义的四中方式:
数据类型 数组名[行数][列数];
数据类型 数组名[行数][列数] = {{数据1,数据2},{数据3,数据4}}
数据类型 数组名[行数][列数] = {数据1,数据2,数据3,数据4}
数据类型 数组名[][列数] = {数据1,数据2,数据3,数据4}
建议:以上4种定义方式,利用第二种更加直观,提高代码的可读性
示例:
#include <iostream>
using namespace std;
int main()
{
int arr[2][3];
int arrA[2][3] = { {1,2,4},{3,4,5} };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++) {
cout << arrA[i][j] << " ";
}
cout << endl;
}
int arrB[2][3] = { 1,2,3,4,5,6 };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++) {
cout << arrB[i][j] << " ";
}
cout << endl;
}
int arrC[][3] = { 1,2,3,4,5,6 };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++) {
cout << arrB[i][j] << " ";
}
cout << endl;
}
system("pause");
return 0;
}
二维数组数组名
- 查看二维数组所占内存空间。
- 获取二维数组首地址。
示例:
#include <iostream>
using namespace std;
int main()
{
int arrA[2][3] = { {1,2,4},{3,4,5} };
// 1.查看二维数组所占内存空间。
cout << "二维数组所占内存空间为:" << sizeof(arrA) << endl;
cout << "二维数组第一行占用的内存空间为:" << sizeof(arrA[0]) << endl;
cout << "二维数组中第一个元素占用内存空间为:" << sizeof(arrA[0][0]) << endl;
// 2.获取二维数组首地址。
cout << "二维数组首地址为:" << arrA << endl;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++) {
cout << arrA[i][j] << " ";
}
cout << endl;
}
system("pause");
return 0;
}
二维数组应用案例
考试成绩统计:
案例描述:有三名同学(张三,李四,王五),在一次考试中的成绩分别如下表,请分别输出三名同学的总成绩。
语文 | 数学 | 英语 | |
---|---|---|---|
张三 | 100 | 100 | 100 |
李四 | 90 | 50 | 100 |
王五 | 60 | 70 | 80 |
示例:
#include <iostream>
using namespace std;
int main()
{
int arrA[3][3] = { {100,100,100},{90,50,100},{60,70,80} };
string names[3] = { "张三","李四","王五" };
int sum[3] = {};
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++) {
sum[i] += arrA[i][j];
}
}
for (int i = 0; i < 3; i++)
{
cout << names[i] << "总分为:" << sum[i] << endl;
}
system("pause");
return 0;
}
标签: C/C++