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

一维数组

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;
}
相关推荐
汤姆和佩琦1 分钟前
LLMs基础学习(八)强化学习专题(1)
深度学习·学习·强化学习·马尔可夫决策过程
奔跑吧邓邓子41 分钟前
解锁Vscode:C/C++环境配置超详细指南
c语言·c++·vscode·配置指南
代码小将1 小时前
java中static学习笔记
java·笔记·学习
虾球xz1 小时前
CppCon 2015 学习:Reactive Stream Processing in Industrial IoT using DDS and Rx
开发语言·c++·物联网·学习
liujing102329292 小时前
Day09_刷题niuke20250609
java·c++·算法
Y3174292 小时前
python Day46 学习(日志Day15复习)
python·学习·机器学习
Bardb2 小时前
02__C++的基本语法
c++·qt
学不会就看2 小时前
selenium学习实战【Python爬虫】
python·学习·selenium
CIb0la2 小时前
Ai自动补全编程工具:llama vscode
运维·开发语言·学习·测试工具·程序人生
2301_804774492 小时前
回溯算法学习
学习