C++ 学习笔记 (9)—— 数组
概述:
就是一个集合,里面存放了相同类型的数据元素
特点1:数组中的每个元素都是相同的数据类型
特点2:数组是由连续的内存位置组成的
1 、一维数组定义方式
数据类型 数组名 [数组长度];
数组类型 数组名[数据长度] = {值1,值2,,,,};
数组类型 数组名[ ]={} ;
#include <iostream>
using namespace std;
int main()
{
cout <<"一维数组练习:" << endl;
/*
1. 数据类型 数组名 [数组长度];
2. 数据类型 数组名 [数组长度]={值1,值2.。。。};
3. 数组类型 数组名 [ ] ={值1,值2.。。。。};
*/
//1. 数据类型 数组名 [数组长度];
int iarr1[5];
iarr1[0] = 0;
iarr1[1] = 1;
iarr1[2] = 2;
iarr1[3] = 3;
iarr1[4] = 4;
for (int i = 0; i < 5; i++)
{
cout << iarr1[i] << endl;
}
cout <<"-----------"<< endl;
//2. 数据类型 数组名 [数组长度]={值1,值2.。。。};
int iarr2[5] = { 5,6,7,8,9 };
for (int i = 0; i < 5; i++)
{
cout << iarr2[i] << endl;
}
cout << "----------" << endl;
//3. 数组类型 数组名 [ ] ={值1,值2.。。。。};
int iarr3[] = { 10,11,12,13,14 };
for (int i = 0; i < 5; i++)
{
cout << iarr3[i] << endl;
}
cout << "-----------"<< endl;
system("pause");
return 0;
}
2 、一维数组名
用途:
可以统计整个数组在内存中的长度 可以获取数组在内存中的首地址
eg:
#include <iostream>
using namespace std;
int main()
{
cout << "一维数组名用途练习:" << endl;
//1.可以统计数组在内存中的长度
int iarr[5] = { 0,1,2,3,4 };
cout << "数组iarr在内存中的长度为:" << sizeof(iarr) <<"个字节" << endl;
cout << "数组iarr有" << sizeof(iarr) / sizeof(iarr[0]) << "个元素"<< endl;
//2.可以获取数组在内存中的首地址
cout << "数组iarr的首地址是:"<< &iarr[0] << endl; cout<< "数组iarr的首地址是:"<< iarr << endl;
system("pause");
return 0;
}
3、 练习案例 1 :五只小猪称体重
案例描述: 在一个数组中记录了五只小猪的体重,
如:int arr[5] = {300,250,200,400,250}; 找出并打印最重的小猪体重。
eg:
#include <iostream>
using namespace std;
int main()
{
cout << "用数据实现五只小猪称体重练习:"<< endl;
int arr[5] = { 100,25,8965,455,8755 };
int max = 0; for (int i = 0; i < 5; i++)
{
if (max < arr[i]) { max = arr[i];
}
}
cout << "最重的小猪体重是:"<< max << endl;
system("pause");
return 0;
}
4、 练习案例2——数组元素逆置
案例描述:请声明一个5个元素的数组,并且将元素逆置,
(原数组:12345,逆置后:54321)
eg:
#include <iostream>
using namespace std;
int main()
{
cout << "数组元素逆置练习:"<< endl;
//1.声明一个五个元素的数组
int array[6] = { 1,2,3,4,5,6};
int freq1 = sizeof(array) / sizeof(array[0]);
for (int i = 0; i < freq1; i++)
{
cout << array[i] << endl;
}
cout << "----------------------"<< endl;
//2.申明数组的开始下标和结束下标
int start = 0;
int end = sizeof(array) / sizeof(array[0] )- 1;
//3.定义一个变量,临时存放元素的值
int temp = 0;
//4.利用for循环进行逆置
// int freq = sizeof(array) / sizeof(array[0]) / 2;
// for (int i = 0; i < freq; i++)
// {
// temp = array[start];
// array[start] = array[end];
// array[end] = temp;
// start++;
// end--;
// }
while (start < end)
{
temp = array[start];
array[start] = array[end];
array[end] = temp;
start++;
end--;
}
cout << "start =" << start << endl;
cout << "end = "<< end << endl;
for (int i = 0; i < freq1; i++)
{
cout << array[i] << endl;
}
system("pause");
return 0;
}
5、案例练习:冒泡排序
作用:
最常用的排序算法,
对数组内元素进行排序. 比较相邻的元素,如果第一个比第二个大,就交换他们两个 对每一对相邻元素做同样的工作,执行完毕后,找到第一个最大的值 重复以上的步骤,每次比较次数-1,直到不需要比较
eg:
#include <iostream>
using namespace std;
int main()
{
cout << "冒泡排序练习:" << endl;
//利用冒泡排序实现升序序列
int arr[9] = { 4,2,8,0,5,7,1,3,9 };
cout << "交换前:"<< endl;
for (int i = 0; i < 9; i++)
{
cout << arr[i] << endl;
}
//1.让所有相邻的两个元素进行比较
int temp = 0;
for (int i = 0; i < 9; i++)
{
for (int j=0;j<9-i-1;j++)
{
if (arr[j] > arr[j+1])
{
temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
//2.因为每比完一次一定会出现一个最大值,在末尾
//3.所以每比较一次,相邻的两个元素比较次数 -1
//4.第一次循环比较元素个数减一次
cout << "交换后:"<< endl;
for (int i = 0; i < 9; i++)
{
cout << arr[i] << endl;
}
system("pause");
return 0;
}
6 、二维数组
二维数组就是在一维数组上,多加一个维度。
定义方式(4种):
1.数据类型 数组名 [行数] [列数];
2.数据类型 数组名 [行数] [列数] ={ {数据1,数据2},{数据3,数据4} } ;
3.数据类型 数组名 [行数] [列数] = { 数据1,数据2,数据3 ,数据 4};
4.数据类型 数组名 [ ] [列数] ={ 数据1,数据2,数据3 ,数据 4 };
eg:
#include <iostream>
using namespace std;
int main()
{
cout << "二维数组的定义练习:"<< endl;
/*
二维数组定义方式:
1.数据类型 数组名[行数][列数];
2.数据类型 数组名[行数][列数]={{数据1,数据2,数据3},{数据4,数据5,数据6},{数据7,数据8,数据9}};
3.数据类型 数组名[行数][列数]={数据1,数据2,数据3,数据4,数据5,数据6,数据7,数据8,数据9};
4.数据类型 数组名[][列数] ={数据1,数据2,数据3,数据4,数据5,数据6,数据7,数据8,数据9};
*/
//1.数据类型 数组名[行数][列数];
int arr1[2][3]; int num = 0;
//外层循环打印行数,内层循环打印列数
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
arr1[i][j] = num; num++;
}
}
//外层循环打印行数,内层循环打印列数
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr1[i][j] << endl;
}
}
cout << "------------------" << endl;
//2.数据类型 数组名[行数][列数] = { {数据1,数据2,数据3},{数据4,数据5,数据6},{数据7,数据8,数据9} };
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;
}
cout << "-----------------"<< endl;
//3.数据类型 数组名[行数][列数]={数据1,数据2,数据3,数据4,数据5,数据6,数据7,数据8,数据9};
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;
}
cout << "------------------" << endl;
//4.数据类型 数组名[][列数] ={数据1,数据2,数据3,数据4,数据5,数据6,数据7,数据8,数据9};
int arr4[][3] = { 1,2,3,4,5,6 };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{ cout << arr4[i][j] << " ";
}
cout << endl;
}
system("pause");
return 0;
}
6.1、二维数组数组名
查看二维数组所占内存空间
获取二维数组首地址
eg:
#include <iostream>
using namespace std;
int main()
{
cout << "二维数组数组名练习:"<< endl;
//1.查看二维数组所占内存空间
int array[2][3]= { {1,2,3}, {4,5,6} };
cout <<"二维数组所占内存空间为:" << sizeof(array) << "个字节" << endl;
cout << "第一行所占空间位:"<< sizeof(array[0]) << "个字节" << endl;
cout << "第二行所占空间位:"<< sizeof(array[1]) << "个字节"<< endl;
cout <<"第一个元素所占内存空间为:" << sizeof(array[0][0]) << "个字节"<< endl;
cout << "二维数组的行数为:" << sizeof(array) / sizeof(array[0]) <<"行" << endl;
cout << "二维数组的列数为:"<< sizeof(array[0]) / sizeof(array[0][0]) << "列" << endl;
//2.查看数组首地址
cout <<";二维数组的首地址是:" << array << endl;
cout << "二维数组的首地址是:" << &array << endl;
cout << "二维数组的首地址是:" << &array[0][0] << endl;
cout <<"二维数组的第一行首地址是:" << &array[0][0] << endl;
system("pause");
return 0;
}
6.2、二维数组应用案例 考试成绩统计:
案例描述:有三名同学(张三,李四,王五),再一次开始中的人成绩分别如下表,请分别输出三名同学的总成绩
eg:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "输出三名同学的总成绩:" << endl;
int achent[3][3] = { {100,100,100}, {90,50,100}, {60,70,80} };
string name[3] = { "张三","李四","王五" };
for (int i = 0; i < 3; i++)
{
int score = 0;
for (int j = 0; j < 3; j++)
{
score += achent[i][j];
}
cout << name[i] << "的成绩:"<< score << endl;
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性