数组(个人学习笔记黑马学习)

一维数组

1、定义方式

cpp 复制代码
#include <iostream>
using namespace std;

int main() {

	//三种定义方式
	
	//1.
	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;*/


	//2.
	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 << arr[i] << endl;
	}*/


	//3.
	int arr3[] = { 90,80,70,60,50,40,30,20,10 };
	for (int i = 0; i < 9; i++)
	{
		cout << arr3[i] << endl;
	}



	system("pause");
	return 0;
}

2、数组名

cpp 复制代码
#include <iostream>
using namespace std;

int main() {

	//1、通过数组名统计整个数组占用内存大小
	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;


	//2、可以通过数组名查看数组首地址
	cout << "数组首地址为:" << arr << endl;
	cout << "数组中第一个元素地址为:" << &arr[0] << endl;

	system("pause");
	return 0;
}

3、练习案例1: 五只小猪称体重

案例描述:

在一个数组中记录了五只小猪的体重

如: int arr[5] =(300,350,200,400,250):

找出并打印最重的小猪体重。

cpp 复制代码
#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 (max < arr[i]) {
			max = arr[i];
		}
	}
	cout << "最重的小猪体重为:" << max << endl;

	system("pause");
	return 0;
}

4、练习案例2:数组元素逆置

案例描述: 请声明一个5个元素的数组,并且将元素逆置(如原数组元素为: 1,3,2,5,4;逆置后输出结果为:4,5,2,3,1

cpp 复制代码
#include <iostream>
using namespace std;

int main() {

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

	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j <=i; j++)
		{
			int t = 0;
			t = arr[i];
			arr[i] = arr[j];
			arr[j] = t;
		}

	}

	for (int i = 0; i < 5; i++)
	{
		cout << arr[i];
	}
	system("pause");
	return 0;
}

5、冒泡排序

作用: 最常用的排序算法,对数组内元素进行排序

1.比较相邻的元素。如果第一个比第二个大,就交换他们两个。

2.对每一对相邻元素做同样的工作,执行完毕后,找到第一个最大值

3.重复以上的步骤,每次比较次数-1,直到不需要比较

cpp 复制代码
#include <iostream>
using namespace std;

int main() {

	int arr[9] = {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;

			}
		}
	}
	for (int i = 0; i < 9; i++)
	{
		cout << arr[i]<<",";
	}
	cout << endl;
	system("pause");
	return 0;
}

二维数组

1、定义方式

cpp 复制代码
#include <iostream>
using namespace std;

int main() {

	//1、
	int arr[2][3];



	//2、
	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;
	}*/


	//3、
	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;
	}*/

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

2、数组名

cpp 复制代码
#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 << "二维数组第一行的首地址为:" << arr[0] << endl;
	cout << "二维数组第二行的首地址为:" << arr[1] << endl;
	cout << "二维数组第一给元素的首地址为:" << &arr[0][0] << endl;



	system("pause");
	return 0;
}

3、 二维数组应用案例

考试成绩统计:

案例描述:有三名同学(张三,李四,王五),在一次考试中的成绩分别如下表,请分别输出三名同学的总成绩

语文 数学 英语

张川 100 100 100

李四 90 50 100

王五 60 70 80

cpp 复制代码
#include <iostream>
using namespace std;

int main() {

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

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

	system("pause");
	return 0;
}
相关推荐
minji...29 分钟前
Linux 线程同步与互斥(三) 生产者消费者模型,基于阻塞队列的生产者消费者模型的代码实现
linux·运维·服务器·开发语言·网络·c++·算法
CoderCodingNo3 小时前
【GESP】C++三级真题 luogu-B4499, [GESP202603 三级] 二进制回文串
数据结构·c++·算法
炽烈小老头3 小时前
【 每天学习一点算法 2026/04/12】x 的平方根
学习·算法
阿杰学AI3 小时前
AI核心知识115—大语言模型之 自监督学习(简洁且通俗易懂版)
人工智能·学习·ai·语言模型·aigc·监督学习·自监督学习
hetao17338374 小时前
2026-04-09~12 hetao1733837 的刷题记录
c++·算法
九英里路4 小时前
OS学习之路——动静态库制作与原理
linux·学习·操作系统·unix·进程·编译·动静态库
6Hzlia4 小时前
【Hot 100 刷题计划】 LeetCode 136. 只出现一次的数字 | C++ 哈希表&异或基础解法
c++·算法·leetcode
汉克老师5 小时前
GESP2024年6月认证C++三级( 第二部分判断题(1-10))
c++·数组·位运算·补码·gesp三级·gesp3级
red_redemption5 小时前
自由学习记录(160)
学习
南無忘码至尊5 小时前
Unity学习90天-第2天-认识Unity生命周期函数并用 Update 控制物体移动,FixedUpdate 控制物理
学习·unity·游戏引擎