概述:所谓数组就是一个集合,里面存放了相同类型的数据元素
特点1:数组中的每个数据元素都是相同的数据类型
特点2:数组是由连续的内存位置组成的
5.1一维数组
5.1.1一维数组定义方式
一维数组定义的三种方式:
数据类型 数组名[数组长度];
数据类型 数组名[数组长度]={值1,值2...};
数据类型 数组名[]={值1,值2...};
数组特点:
放在一块的连续的内存空间中数组中每个元素都是相同数据类型
数组的下标是从0开始
#include <iostream>
using namespace std;
int main()
{
int arr[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
cout << arr[0] << endl;
cout << arr[1] << endl;
cout << arr[2] << endl;
cout << arr[3] << endl;
cout << arr[4] << endl;
int arr2[5] = { 10,20,30,40,50 };
cout << arr2[0] << endl;
cout << arr2[1] << endl;
cout << arr2[2] << endl;
cout << arr2[3] << endl;
cout << arr2[4] << endl;
for (int i = 0; i < 5; i++)
{
cout << arr2[i] << endl;
}
int arr3[] = { 50,40,30,20,10 };
for (int i = 0; i < 5; i++)
{
cout << arr3[i] << endl;
}
system("pause");
return 0;
}
总结:数组名的命名规范与变量名命名规范一致,不要和变量重名
数组中下标是从0开始索引
5.1.2一维数组名称的用途
可以统计整个数组内存中的长度 sizeof(arr)
可以获取数组在内存中的首地址
sizeof(arr)/sizeof(arr[0])=arr中元素个数
通过数组名统计数组大小
#include <iostream>
using namespace std;
int main()
{
int arr[5] = { 10,20,30,40,50 };
cout << "每个数组占用内存空间为:" << sizeof(arr)<<endl;
cout << "每个元素占用内存空间为:" << sizeof(arr[0]) << endl;
cout << "数组中每个元素个数为:" << sizeof(arr)/sizeof(arr[0]) << endl;
system("pause");
return 0;
}
通过数组名查看数组首地址
#include <iostream>
using namespace std;
int main()
{
int arr[5] = { 10,20,30,40,50 };
cout<<"数组首地址为:" << arr << endl;
cout << "数组首地址为:" << (int)arr << endl;
cout << "数组中第一个元素地址为:" << (int)&arr[0] << endl;
system("pause");
return 0;
}
注意:数组名是一个常量,不可以进行赋值
eg:在一个数组中记录了五只小猪的体重,找出并打印最终的小猪,300,350,200,400,250
#include <iostream>
using namespace std;
int main()
{
int arr[5] = { 300,350,200,400,250 };
int max = 0;
for (int i = 0; i < 5; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
cout << "最重的小猪体重为:" << max << endl;
system("pause");
return 0;
}
eg:声明一个5个元素的数组,并且将元素逆置
#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(arr[0]) - 1;//末尾元素下标
int temp = 0;
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;
}
return 0;
}
5.1.3冒泡排序
作用:最常用的排序算法,对数组内元素进行排序
(1)比较比较相邻的元素,如果第一个比第二个大,就交换他们两个。
(2)对每一对相邻元素做同样的工作,执行完毕后,找到第一个最大值。
(3)重复以上步骤,每次比较次数-1,直到不需要比较。
#include <iostream>
using namespace std;
int main()
{
int arr[9]= {4, 2, 8, 0, 5, 7, 1, 3, 9};
cout << "排序前" << endl;
for (int i = 0; i < 9; i++)
{
cout << arr[i] << " ";
}
cout << endl;
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] << " ";
}
system("pause");
return 0;
}
总结:排序总轮数=元素个数-1
每轮对比次数=元素个数-排序轮数-1
5.2二维数组
5.2.1二维数组四种定义方式:
5.2.1.1数据类型 数组名[行数][列数];
5.2.1.2数据类型 数组名[行数][列数]={{数据1,数据2},{数据3,数据4}};
5.2.1.3数据类型 数组名[行数][列数]={数据1,数据2,数据3,数据4};
5.2.1.4数据类型 数组名[ ][列数]={数据1,数据2,数据3,数据4};
第二种代码更直观,提高代码可读性
5.2.1.1数据类型 数组名[行数][列数];
#include <iostream>
using namespace std;
int main()
{
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;
cout << arr[0][0];
cout << arr[0][1];
cout << arr[0][2];
cout << arr[1][0];
cout << arr[1][1];
cout << arr[1][2];
cout << endl;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j];
}
}
cout << endl;
system("pause");
return 0;
}
5.2.1.2数据类型 数组名[行数][列数]={{数据1,数据2},{数据3,数据4}};
#include <iostream>
using namespace std;
int main()
{
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 << arr2[i][j]<<" ";
}
cout << endl;
}
system("pause");
return 0;
}
5.2.1.3数据类型 数组名[行数][列数]={数据1,数据2,数据3,数据4};
#include <iostream>
using namespace std;
int main()
{
int arr3[2][3] ={1,2,3,4,5,6};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr3[i][j]<<" ";
}
cout << endl;
}
system("pause");
return 0;
}
5.2.1.4数据类型 数组名[ ][列数]={数据1,数据2,数据3,数据4};
#include <iostream>
using namespace std;
int main()
{
int arr3[][3] ={1,2,3,4,5,6};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr3[i][j]<<" ";
}
cout << endl;
}
system("pause");
return 0;
}
5.2.2二维数组名
查看二维数组所占空间
获取二维数组首地址
#include <iostream>
using namespace std;
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 << "二维数组的行数为:" << sizeof(arr)/sizeof(arr[0]) << endl;
cout << "二维数组的列数为:" << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;
cout << "二维数组的首地址为:" << arr << endl;
cout << "二维数组的首地址为:" << (int)arr << endl;
cout << "二维数组第一行首地址为:" << arr[0] << endl;
cout << "二维数组第二行首地址为:" << arr [1]<< endl;
cout << "二维数组的第一个元素地址为:" << &arr[0][0] << endl; //注意:查找时候需要加&
system("pause");
return 0;
}
eg:三名同学考试成绩如下,请分别输出三名同学的总成绩
#include <iostream>
using namespace std;
int main()
{
int scroes[3][3] =
{
{100,100,100},
{90,50,100},
{60,70,80}
};
string names[3] = { "张三", "李四", "王五" };
for (int i = 0; i < 3; i++)
{
int temp = 0;
for (int j = 0; j < 3; j++)
{
temp = scroes[i][j] + temp;
}
cout << names[i]<<"的总成绩为:" << temp << endl;
}
system("pause");
return 0;
}