二维数组
1.二维数组
2.数据名
1.二维数组
(1)数据类型 数组名[行数][列数];
(2)数据类型 数组名[行数][列数] = { { 数据1,数据2 },{ 数据3,数据4 } };
(3)数据类型 数组名[行数][列数] = { 数据1,数据2,数据3.数据4 };
(4)数据类型 数组名[ ][列数] = { 数据1,数据2,数据3.数据4 };
#include<iostream>
using namespace std;
int main()
{
int arr1[2][3];
arr1[0][0] = 1;
arr1[0][1] = 2;
arr1[0][2] = 3;
arr1[1][0] = 4;
arr1[1][1] = 5;
arr1[1][2] = 6;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++) {
cout << arr1[i][j] << endl;
}
}
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;
}
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;
}
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.数组名
(1)查看所占用内存空间
(2)查看二维数组的首地址
#include<iostream>
using namespace std;
int main()
{
int arr[2][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] << " ";
}
cout << endl;
}
cout << "二维数组内存为 :" << sizeof(arr) << endl;
cout << "第一行占用的内存 :" << sizeof(arr[0]) << endl;
cout << "第一个元素占用的内存 :" << sizeof(arr[0][0]) << endl;
cout << "二维数组的首地址 : " << (int)arr << endl;
cout << "二维数组中第一行的首地址 :" << (int)arr[0] << endl;
cout << "二维数组中第二行的首地址 :" << (int)arr[1] << endl;
cout << "二维数组中第一个元素的首地址 :" << (int)&arr[0][0] << endl;
system("pause");
return 0;
}
举例:
#include<iostream>
#include<string>
using namespace std;
int main()
{
int arr[3][3] =
{
{1,2,3},
{4,5,6},
{7,8,9},
};
string name[3] = { "第一组","第二组","第三组" };
for (int i = 0; i < 3; i++)
{
int sum = 0;
for (int j = 0; j < 3; j++) {
sum += arr[i][j];
//cout << arr[i][j] << " ";
}
cout << name[i] << "组" << "的总分 : " << sum << endl;
}
system("pause");
return 0;
}