C++基础入门学习笔记之【数组】(四)

1、数组

定义:所谓数组就是一个集合,里面存放了相同类型的数据元素

在这里插入图片描述

2、 一维数组

在这里插入图片描述
注意:
数据类型 数组名[ ] = {值1,值2…},数组长度由后面的值决定
在这里插入图片描述

#include<iostream>
using namespace std;
#include<string>
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 << "------" << endl;
	//2、数据类型 数组名 [数组长度]={数组元素....}
	//如果数组元素没有填充完毕,则用0代替
	int arr2[5] = { 10,20,30,40,50 };
	//访问数组元素
	cout << arr2[0] << endl;
	cout << "------" << endl;
	//访问数组元素
	for (int i = 0; i < 5; i++)
	{
		cout << arr2[i] << endl;
	}
	cout << "------" << endl;
	//3、数据类型 数组名 [ ]={数组元素....}
	//定义该数组的时候,必须有初始长度
	int arr3[]= {10,20,30,40,50 };
	for (int i = 0; i < 5; i++)
	{
		cout << arr3[i] << endl;
	}
	system("pause");
	return 0;
}

输出:

10
------
10
------
10
20
30
40
50
------
10
20
30
40
50
2.1、 一维数组数组名

作用:
1、可以统计整个数组在内存中的长度
2、可以获取数组在内存中的首地址,一般用16进制
3、数组名是一个常量,不可以重新赋值

在这里插入图片描述
数组名是一个常量,不可以重新赋值如图:
在这里插入图片描述

#include<iostream>
using namespace std;
#include<string>
int main() {
	//数组名的用途 可以用数组名统计所占内存空间
	int arr[] = { 10, 20, 30, 40, 50 };
	cout <<"整个数组占用的内存空间"<< sizeof(arr) << endl;
	//数组首地址
	cout << "数组首地址:" << (int)arr << endl;//16进制变为10进制
	cout << "数组第1个元素的地址为:" << (int)&arr[0] << endl;
	cout << "数组第2个元素的地址为:" << (int)&arr[1] << endl;
	//arr = 100;
	system("pause");
	return 0;
}

输出:

整个数组占用的内存空间20
数组首地址:2123365608
数组第1个元素的地址为:2123365608
数组第2个元素的地址为:2123365612
请按任意键继续. . .
2.2 一维数组案例—求最大值

五只小猪称体重,选出最重的体重

#include<iostream>
using namespace std;
#include<string>
int main() {
	//1、创建5只小猪的体重
	int arr[] = { 300,350,200,400,250 };
	//2、从数组中找到最大值
	int max = 0;
	for (int i = 0; i < 5; i++)
	{
		if (max < arr[i])
		{
			max = arr[i];
		}
	}
	//3、打印最大值
	cout << max << endl;
	system("pause");
	return 0;
}

输出:

400
2.3 一维数组案例—元素倒置

数组元素逆置
第一种方法:

#include<iostream>
using namespace std;
#include<string>
int main() {
	//
	int arr[] = { 1,3,2,5,4 };
	for (int i = 0; i < 5; i++)
	{
		arr[i] = arr[5 - i - 1];
	}
	
	for (int i=0; i<5;i++)
	{
		cout << arr[i] << endl;
	}
	system("pause");
	return 0;
}

输出:

4
5
2
5
4

第二种方法:

#include<iostream>
using namespace std;
#include<string>
int main() {
	//输出原数组
	int arr[] = { 1,3,2,5,4 };
	int num = sizeof(arr) / sizeof(arr[0]) - 1;
	for (int i = 0; i < num+1; i++)
	{
		cout << arr[i] << endl;
	}
	cout << "-----" << endl;
	//进行数组元素互换
	int start = 0;
	int end = num;
	while (start < end)
	{
		int temp = arr[start];
		arr[start] = arr[end];
		arr[end] = temp;
		start++;
		end--;
	}
	//输出生成的数组
	for (int i = 0; i < num + 1; i++)
	{
		cout << arr[i] << endl;
	}
	system("pause");
	return 0;
}

输出:

1
3
2
5
4
-----
4
5
2
3
1
2.4 一维数组案例—冒泡排序
#include<iostream>
using namespace std;
#include<string>
int main() {
	//冒泡排序
	int arr[] = { 1,3,2,5,4 };
	int num = sizeof(arr) / sizeof(arr[0]);
	for (int i = 0; i < num-1; i++)
	{
		for (int j = 0; j < num - i-1; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
	//打印排序后的数组
	for (int j = 0; j < num; j++)
	{
		cout << arr[j] << endl;
	}

	system("pause");
	return 0;
}

输出:

1
2
3
4
5

3、二维数组

二维数组就是在一维数组的基础上增加一个维度

在这里插入图片描述

#include<iostream>
using namespace std;
#include<string>
int main() {
	//二维数组定义方式
	//1.数据类型 数组名 [行数][列数]
	int arr[2][3];
	arr[0][0] = 1;
	arr[0][1] = 2;
	arr[0][2] = 3;
	arr[1][0] = 4;
	arr[1][1] = 5;
	arr[1][2] = 6;
	//2.输出 外层打印行数,内层循环打印列数
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << arr[i][j] << endl;
		}
	}
	cout << "-----" << endl;
	//2.数据类型 数组名 [行数][列数]={{数据1,数据2},{数据3,数据4}}
	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;
	}
	cout << "-----" << endl;
	//3.数据类型 数组名 [行数][列数]={数据1,数据2,数据3,数据4}
	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;
	}
	cout << "-----" << endl;
	//4.数据类型 数组名 [][列数]={数据1,数据2,数据3,数据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;
	}
	cout << "-----" << endl;
	system("pause");
	return 0;
}

输出:

1
2
3
4
5
6
-----
1  2  3
4  5  6
-----
1  2  3
4  5  6
-----
1  2  3
4  5  6
-----
3.1、 二维数组数组名

作用:
1、查看二维数组所占内存空间
2、可以获取二维数组在内存中的首地址,一般用16进制
3、数组名是一个常量,不可以重新赋值

#include<iostream>
using namespace std;
#include<string>
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 << "二维数组的首地址为:" << (int)arr << endl;
	cout << "二维数组的第一行的地址为:" << (int) & arr[0] << endl;
	cout << "二维数组的第一行第一列的地址为:" << (int)&arr[0][0] << endl;

	system("pause");
	return 0;
}

输出:

二维数组所占空间为:24
二维数组第一行所占空间为:12
二维数组第一个元素所占空间为:4
二维数组行数为:2
二维数组列数为:3
二维数组的首地址为:1081080920
二维数组的第一行的地址为:1081080920
二维数组的第一行第一列的地址为:1081080920
请按任意键继续. . .
3.2、 二维数组应用案例

在这里插入图片描述

#include<iostream>
using namespace std;
#include<string> //使用string字符
int main() {
	int arr[3][3] = { {100,100,100},{90,50,100},{60,70,80} };
	string names[3] = { "张三","李四","王五" };
	for (int i = 0; i < 3; i++)
	{
		int sum = 0;
		for (int j = 0; j < 3; j++)
		{
			sum += arr[i][j];
		}
		cout <<names[i]<<"的总分为:" << sum << endl;
		cout << "---" << endl;
	}
	system("pause");
	return 0;
}

输出:

张三的总分为:300
---
李四的总分为:240
---
王五的总分为:210
---
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值