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

一维数组

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 arr5 =(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;
}
相关推荐
一尘之中5 小时前
从C语言底层设计到系统架构评估:软件架构知识体系全景
学习·系统架构·ai写作
NiceCloud喜云5 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
cjhbachelor5 小时前
c++继承
c++
肩上风骋6 小时前
C++14特性
开发语言·c++·c++14特性
sheeta19986 小时前
LeetCode 每日一题笔记 日期:2026.05.29 题目:3300. 最小元素
笔记·leetcode
中屹指纹浏览器6 小时前
2026指纹浏览器代理链路适配原理与多线路集群调度方案
经验分享·笔记
星夜夏空996 小时前
FreeRTOS学习(4)——内存映射
数据库·学习·mongodb
不羁的木木6 小时前
ArkWeb实战学习笔记05-综合实战:构建混合应用
笔记·学习·harmonyos
橙橙笔记6 小时前
Python的学习第一部分
python·学习
bush47 小时前
嵌入式linux学习记录二
linux·运维·学习