二维数组定义的四种方式
//二维数组定义方式
/*
1. 数据类型 数组名[行数][列数];
2. 数据类型 数组名[行数][列数]={{数据1,数据2},{},{}};
3. 数据类型 数组名[行数][列数]={数据1,数据2。。}
4. 数据类型 数组名[][列数]={数据1,数据2,数据3};
*/
// 1. 数据类型 数组名[行数][列数];
/*int arr[2][2];
arr[0][0] = 4;
arr[0][1] = 3;
arr[1][0] = 9;
arr[1][1] = 7;*/
//2. 数据类型 数组名[行数][列数] = { {数据1,数据2},{},{} };
/*int arr[2][3] =
{
{1,2,3},{4,5,6}
};*/
//3. 数据类型 数组名[行数][列数] = { 数据1,数据2 }
// 系统自动分配
/*int arr[2][3] =
{ 1,2,3,4,5,6 };*/
//4. 数据类型 数组名[][列数] = { 数据1,数据2,数据3 }
int arr[][3] = { 1,2,3,4,5,6 };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j] << "\t";
}
cout << endl;
}
cout << endl;
二维数组名称用途
// 二维数组名称用途
//1.可以查看内存空间大小
// 某一行/个元素占用的内存
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 << "二维数组行数:";
cout << sizeof(arr) / sizeof(arr[0]) << endl;
cout << "二维数组列数:";
cout << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;
//2.可以查看二维数组的首地址
cout << "二维数组首地址为:";
cout << arr << endl;
cout << "二维数组第二行首地址:";
cout << arr[1] << endl;
cout << "二维数组第一个元素地址:";
cout << &arr[0][0] << endl;
打印分数
int score[3][3] =
{
{100,100,100},
{90,89,93},
{70,90,96}
};
string name[3] = { "张三","李四","王五" };
string total[5] = { "姓名","语文","数学","英语","总分"};
for (int i = 0; i < 5; i++)
{
cout << total[i] << '\t';
}
cout << endl;
for (int i = 0; i < 3; i++)
{
cout << name[i] << "\t";
int sum = 0;
for (int j = 0; j < 3; j++)
{
sum += score[i][j];
cout << score[i][j] << "\t";
}
cout << sum;
cout << endl;
}
cout << endl;
// 1.定义指针
int a = 10;
int* p = &a;
cout << *p << " " << p << endl;
int& t = a;
cout << t << " " << &t << endl;