学习笔记之C++数组基础知识,一维数组的三种定义方式,二维数组的四种定义方式,一维数组名和二维数组名的用途,以及一维数组和二维数组的经典案例,包括冒泡排序法,元素逆置等

最近在学习C++数组基础知识,关于一维数组以及二维数组系统的了解了一些,学习笔记分享如下:

数组

概念:所谓数组就是一个集合,里面存放了很多相同类型的数据元素

两个特点:特点1:数组中的每个数据元素都是相同的数据类型
特点2:数组是由连续的内存位置组成的

一维数组

一维数组的三种定义方式

方式一语法: 数据类型 数组名 [数组长度];

#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;

    system("pause");

    return 0;
}

方式二语法: 数据类型 数组名 [数组长度] = {值1,值2,值3 …};

#include<iostream>
using namespace std;

int main() {
	//定义数组
	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;

    system("pause");

    return 0;
}

上述代码可在访问元素时进行程序简化:
(改成循环输出 如果在初始化的时候没有全部填写完,将会用0来补)

#include<iostream>
using namespace std;

int main() {
	//定义数组
	int arr2[5] = {10,20,30,40,50};
	
	//访问元素
	for (int i = 0; i < 5; i++)
	{
		cout << arr2[i] << endl;
	}

    system("pause");

    return 0;
}

方式三语法: 数组名[ ]= {值1,值2,值3 …};

#include<iostream>
using namespace std;

int main() {
	//定义数组
	int arr3[] = { 30,90,80,30,50,60,70,100 };
	
	//访问元素
	for (int i = 0; i < 8; i++)
	{
		cout << arr3[i] << endl;
	}

    system("pause");

    return 0;
}

一维数组名称的用途

一维数组名称的用途:1.可以统计整个数组在内存中的长度2.可以获取数组在内存中的首地址

举例:

#include<iostream>
using namespace std;

int main() {

	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };

	cout << "整个数组占用的内存空间为    " << sizeof(arr) << endl;
	
	cout << "每个元素占用内存空间为    " << sizeof(arr[0]) << endl;

	cout << "数组中元素个数为    " << sizeof(arr) / sizeof(arr[0]) << endl;

	cout << "数字的首地址为    " << (int)arr << endl;//使用int 把十六进制的强化转为十进制

	cout << "数组中第一个元素地址为   " << (int)&arr[0] << endl;

	//注意:数组名是常量,不可以进行赋值

	system("pause");

	return 0;
}

关于一维数组的案例-五只小猪称体重

案例描述:在一个数组中记录了五只小猪的体重,如: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 = 0;

	for (int i = 0; i < 5; i++)
	{
		//cout << arr[i] << endl;

		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)

思路:start 和 end下标元素互换

#include<iostream>
using namespace std;

int main() {
	
	//1.创建数组

	int arr[5] = { 1,3,2,5,4 };

	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(arr) / sizeof(arr[0]) - 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.比较相邻的元素。如果第一个比第二个大,就交换他们两个。2.对每一对相邻元素做同样的工作,执行完毕后,找到第一个最大值。3.重复以上的步骤,每次比较次数-1,直到不需要比较。

#include<iostream>
using namespace std;

int main() {

	int arr[] = { 4,2,8,0,5,7,1,3,9 };

	cout << "排序前的数组为 " << endl;
	
	for (int i = 0; i < 9; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;

	//开始冒泡排序
	// 总共排序轮数为 元素个数-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] << " ";
	}
	cout << endl;

	system("pause");

	return 0;
}

二维数组

概念:二维数组就是一维数组多了一个维度
二维数组有四种定义方式。

二维数组的四种定义方式

方式一语法:数据类型 数组名[行数] [列数]

#include<iostream>
using namespace std;

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;

	//cout << arr[0][0] << endl;
	//cout << arr[0][1] << endl;
	
	//可以用循环来做,外层打印行,内层打印列

	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << arr[i][j] << endl;
		}
	}
system("pause");

return 0;
}

方式二:

#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;
}

方式三:

#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;
}

方式四:不给出数组的行数也可以 但是要有列数

#include<iostream>
using namespace std;

int main() {

    //4.不给出行数也可以 但是要有列数
	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;
}

二维数组名的用途

二维数组数组名用途:
1.查看二维数组所占内存空间
2.获取二维数组首地址

#include<iostream>
using namespace std;

int main() {

	int arr[2][3] =
	{
		{1,2,3},
		{4,5,6}
	};

	//1.可以查看内存空间大小

	cout << "二维数组占用的空间内存为: " << sizeof(arr) << endl;//24

	cout << "二维数组第一行占用的空间内存为: " << sizeof(arr[0]) << endl;//12

	cout << "二维数组第一个元素占用的空间内存为: " << sizeof(arr[0][0]) << endl;//4

	cout << "二维数组行数为: " << sizeof(arr) / sizeof(arr[0]) << endl;///2

	cout << "二维数组列数为: " << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;//3
	
	//2.可以查看二维数组的首地址

	cout << "二维数组的首地址为: " << (int)arr << endl;//十六进制显示;改为(int)arr可强制转为十进制数字  158332824
	cout << "二维数组的第一行首地址为: " << (int)arr[0] << endl; //158332824
	cout << "二维数组的第二行首地址为: " << (int)arr[1] << endl; //158332836 第二行与第一行差12,即3个字节

	cout << "二维数组的第一个元素的首地址为: " << (int)&arr[0][0] << endl;//查看元素地址要加&   116390648 第一个元素地址=第一行首地址=二位数组的首地址
	cout << "二维数组的第二个元素的首地址为: " << (int)&arr[0][1] << endl;// 116390652 与第一个元素差4
	system("pause");

	return 0;
}

二维数组的案例–考试成绩统计

案例:考试成绩统计,根据成绩单,统计出每个同学的总成绩
步骤:1.创建二维数组 3行3列 2.统计和

#include<iostream>
using namespace std;

int main() {

	//1.创建数组

	int score[3][3] =
	{
		{100,100,100},
		{90,50,100},
		{60,70,80}
	};

	//2.统计每个人的分数总和
	for (int i = 0; i < 3; i++)
	{
		int sum = 0;

		for (int j = 0; j < 3; j++)
		{
			sum += score[i][j];
			//cout << score[i][j] << "  ";
		}
		cout << "第" << i + 1 << "个人的总分为  " << sum << endl;
	}
	
	system("pause");

	return 0;
}

但是上述代码只能输出第一名同学 第二名同学…如果我们想输出每个同学的姓名,则需要写一个定义名字的数组,同时为其写一个循环,优化后的代码如下:

#include<iostream>
using namespace std;
#include<string>


int main() {

	//1.创建数组

	int score[3][3] =
	{
		{100,100,100},
		{90,50,100},
		{60,70,80}
	};

	string names[3] = { "张三","李四","王五" };

	//2.统计每个人的分数总和
	for (int i = 0; i < 3; i++)
	{
		int sum = 0;

		for (int j = 0; j < 3; j++)
		{
			sum += score[i][j];
			//cout << score[i][j] << "  ";
		}
		cout <<names[i] << " 的总分为  " << sum << endl;
	}

	system("pause");

	return 0;
}

以上就是关于数组的内容总结,如有误,欢迎指出!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值