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

一维数组

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;
}
相关推荐
没有羊的王K32 分钟前
SSM框架学习——day1
java·学习
珊瑚里的鱼35 分钟前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上38 分钟前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
Risehuxyc1 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
林林要一直努力2 小时前
AOSP Settings模块问题初窥
android·学习·bug·android studio
景彡先生4 小时前
C++编译期计算:常量表达式(constexpr)全解析
服务器·c++
tan77º4 小时前
【Linux网络编程】应用层自定义协议与序列化
linux·运维·服务器·网络·c++·tcp/ip
余大侠在劈柴5 小时前
pdf.js 开发指南:在 Web 项目中集成 PDF 预览功能
前端·javascript·学习·pdf
悠哉清闲5 小时前
Android Studio C++/JNI/Kotlin 示例 三
c++·kotlin·android studio
WarPigs5 小时前
游戏框架笔记
笔记·游戏·架构