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

一维数组

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;
}
相关推荐
程序喵大人39 分钟前
【C++入门】内存模型与资源管理 - 09 shared_ptr 和 weak_ptr 解决共享,也带来代价
开发语言·c++·资源管理·智能指针·内存模型
一木 之林2 小时前
Dify学习笔记 03 · RAG检索增强生成:三代范式、评估与落地(第40-45、55-56集)
笔记·小程序·交互·dify
apple-mapping2 小时前
网络RTK定位学习总结
学习·网络rtk
jimy13 小时前
C++ 中的 `T&&`:右值引用与转发引用
开发语言·c++
楚山之石3 小时前
【Lancet Neurology 】多发性硬化认知障碍的 SDMT 筛查、灰质萎缩标志与认知康复循证路径
图像处理·人工智能·学习·mri
会编程的小孩5 小时前
C++ 定时器
c++
海海不掉头发5 小时前
CSDN博客_时序预测9模型复现对比
人工智能·学习·软件工程
沫璃染墨6 小时前
从零入门计算机网络系列(十一):HTTP协议进阶——从重定向原理到GET/POST与Fiddler抓包
linux·服务器·开发语言·网络·c++·tcp/ip·html
蓝祁子yoky7 小时前
Qt学习日记 Day13:网络编程(一)-TCP基础与Socket实战
网络·qt·学习
箓维7 小时前
TCP Socket的进阶实现
网络·笔记·tcp/ip