[Two-dimensional array]: ①Four definitions of two-dimensional array ②Use of two-dimensional array name ③ Two-dimensional array example: total scores of three persons in each subject

Four definitions of two-dimensional arrays

arr [row] [column]

#include <iostream>
using namespace std;

int main()
{
    
    
   //二维数组定义方式:

   //2行3列
   //第一种
   int arr[2][3];
   //第二种【最好的是这种】
   int arr1[2][3] =
   {
    
    
   	{
    
    2,3,4},
   	{
    
    4,5,7},
   };
   //第三种
   int arr2[2][3] = {
    
     2,3,4,5,6,7 };
   //第四种
   int arr3[][3] = {
    
     2,4,5,6,8,6 };

   return 0;
}

Two-dimensional array name purpose:

①The size of the memory space

②First address

The first address is generally in hexadecimal and converted to decimal
cout << arr << endl; //hexadecimal
cout << (int)arr << endl; //
The address access of specific elements in decimal system requires adding & ( Address character)
cout << (int)&arr[0][0] << endl;

cout << sizeof(arr) / sizeof(arr[0]) << endl;//行数
cout << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;//列数

#include <iostream>
using namespace std;

int main()
{
    
    
	//二维数组名称用途
	//①内存空间的大小 ②首地址

	int arr[2][3];

	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 << (int)arr << endl;//首地址的地址
	//具体元素的地址访问需要加 & (取址符)
	cout << (int)&arr[0][0] << endl;

	return 0;
}

Two-dimensional array example:

The total scores of Andy, Mike and Tom in each subject

#include <iostream>
#include <string>
using namespace std;

//三人各科的总成绩
int main()
{
    
    
	//定义三人的各科成绩
	int score[3][3] =
	{
    
    
		//每个人的语文,数学,英语成绩单独于一个大括号里面
		{
    
    68,79,90},
		{
    
    89, 68, 100},
		{
    
    90,91,95}
	};

	//定义三人的名字
	string name[3] = {
    
     "Andy","Mike","Tom" };

	//
	for (int i = 0; i < 3; i++)
	{
    
    
		int sum = 0;//每次内部循环的时候初始化sum值
		for (int j = 0; j < 3; j++)
		{
    
    		
			sum += score[i][j];//三次成绩相加
		}
		cout << name[i] << "的三科总成绩为:" << sum << endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_42198265/article/details/113666287