c++中二维数组的四种定义方式及其打印输出;二维数组数组名的用途;二维数组应用案例

二维数组的四种定义方式:

* 1、数组类型 数组名 [行数][列数];
* 2、数组类型 数组名 [行数][列数]={{数据1,数据2},{数据3,数据4}};
* 3、数组类型 数组名 [行数][列数]={数据1,数据2,数据3,数据4};
* 4、数组类型 数组名 [ ][列数]={数据1,数据2,数据3,数据4};

第二种定义方式更加直观,提高代码的可读性。

#include<iostream>
using namespace std;
int main()
{
	/*二维数组:在一维数组上多加一个维度
	* 二维数组的4种定义方式:
	* 1、数组类型 数组名 [行数][列数];
	* 2、数组类型 数组名 [行数][列数]={{数据1,数据2},{数据3,数据4}};   此种定义方式更加直观,提高代码的可读性
	* 3、数组类型 数组名 [行数][列数]={数据1,数据2,数据3,数据4};
	* 4、数组类型 数组名 [ ][列数]={数据1,数据2,数据3,数据4};
	*/

	int arr1[2][2];
	arr1[0][0] = 101; arr1[0][1] = 102; arr1[1][0] = 103; arr1[1][1] = 104;
	cout << "二维数组的第一种定义方式:int arr1[2][2]" << endl;
	cout << endl;
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			cout << arr1[i][j] << "  ";
		}
		cout << endl;
	}
	cout << endl;

	int arr2[3][3] = { {1,2,3},{4,5,6} };
	cout << "二维数组的第二种定义方式:int arr2[3][3] = { {1,2,3},{4,5,6} }" << endl;
	cout << endl;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << arr2[i][j] << "  ";
		}
		cout << endl;
	}
	cout << endl;

	int arr3[2][5] = { 1,2,3,4,5,6,7,8,9,0 };
	cout << "二维数组的第三种定义方式:int arr3[2][5] = { 1,2,3,4,5,6,7,8,9,0 }" << endl;
	cout << endl;
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			cout << arr3[i][j] << "  ";
		}
		cout << endl;
	}
	cout << endl;

	int arr4[][3] = { 11,22,33,44,55,66,77,88,99 };
	cout << "二维数组的第四种定义方式:int arr[][3] = { 11,22,33,44,55,66,77,88,99 }" << endl;
	cout << endl;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << arr4[i][j] << "  ";
		}
		cout << endl;
	}
	cout << endl;

	system("pause");
	return 0;
}

在这里插入图片描述

二维数组数组名的用途

#include<iostream>
using namespace std;
int main()
{
	/*二维数组的数组名的用途
	* 1、统计占用内存空间的大小
	* 2、查看二维数组的首地址
	*/
	int arr[3][2] = { 1,2,3,4,5,6 };
	cout << "二维数组为:" << endl;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			cout << arr[i][j]<<"  ";
		}
		cout << endl;
	}
	cout << endl;
	cout << "二维数组占用的内存空间为:" << sizeof(arr) << endl << endl;
	cout << "二维数组第一行占用的内存空间为:" << sizeof(arr[0]) << endl << endl;
	cout << "二维数组第一个元素占用的内存空间为:" << sizeof(arr[0][0]) << endl << endl;
	cout << "二维数组的行数为:" << sizeof(arr) / sizeof(arr[0]) << endl << endl;
	cout << "二维数组的列数为:" << sizeof(arr[0]) / sizeof(arr[0][0]) << endl << endl;
	cout << "二维数组的首地址(十六进制)为:" << arr << endl << endl;
	cout << "二维数组的首地址(十进制)为:" << (int)arr << endl << endl;
	//cout << "二维数组的首地址(十进制)为:" << (int)arr[0][0] << endl << endl;

	system("pause");
	return 0;
}

在这里插入图片描述

二维数组应用案例

#include<iostream>
using namespace std;
int main()
{
	/*二维数组应用案例
	* 输出3名同学的总成绩
	* 输出3门课程的平均成绩
	*/
	int arr[3][3] = { {100,100,100},{90,50,100},{60,70,80} };
	int score = 0;

	for (int i = 0; i < 3; i++)
	{
		int score = 0;
		cout << "第" << i+1 << "名同学的总成绩为:";
		for (int j = 0; j < 3; j++)
		{
			score = score + arr[i][j];
		}
		cout << score <<  endl;
	}
	cout << endl;

	for (int i = 0; i < 3; i++)
	{
		int score = 0;
		cout << "第" << i+1 << "门课的平均成绩为:";
		for (int j = 0; j < 3; j++)
		{
			score = score + arr[j][i];
		}
		cout << score/3 << endl;
	}
	cout << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ ,我们可以使用二维数组和指针来表示一个二维数组。 1. 二维数组 二维数组是一个由多个一维数组组成的数组。它可以看做是一个表格,其每个元素都有两个下标,表示在表格的行和列。定义一个二维数组可以使用以下语法: ```c++ int arr[n][m]; // 定义一个 n 行 m 列的二维数组 ``` 其,n 和 m 分别表示二维数组的行数和列数。我们可以使用两层循环来遍历二维数组: ```c++ for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { // 处理 arr[i][j] } } ``` 2. 指针 指针是一个变量,它存储了一个地址,这个地址指向了内存的一个位置。我们可以使用指针来访问这个位置的值。定义一个指针可以使用以下语法: ```c++ int *ptr; // 定义一个指向 int 类型变量的指针 ``` 我们可以使用指针来访问一个变量的值: ```c++ int a = 10; int *ptr = &a; // 将指针 ptr 指向变量 a 的地址 cout << *ptr; // 输出变量 a 的值 10 ``` 3. 二维数组与指针的关系 二维数组在内存是连续存储的,我们可以使用指针来访问二维数组的元素。定义一个指向二维数组的指针可以使用以下语法: ```c++ int (*ptr)[m]; // 定义一个指向 n 行 m 列的二维数组的指针 ``` 其,括号是必须的,因为 `*` 的优先级比 `[]` 低。我们可以使用指针来遍历二维数组: ```c++ int arr[n][m]; int (*ptr)[m] = arr; // 将指针 ptr 指向二维数组 arr 的首地址 for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { // 处理 ptr[i][j] cout << ptr[i][j] << " "; } cout << endl; } ``` 上述代码,`ptr[i][j]` 实际上等价于 `*(*(ptr + i) + j)`,即指针加上偏移量再取值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值