其他分享
首页 > 其他分享> > 046.数组-二维数组定义方式

046.数组-二维数组定义方式

作者:互联网

#include <iostream>
using namespace std;
int main()
{
    //二维数组定义
    /*
    1.数据类型 数组名[行数][列数];
    2.数据类型 数组名[行数][列数]={{数据1,数据2},{数据2=3,数据4}};
    3.数据类型 数组名[行数][列数]={数据1,数据2,数据3,数据4};
    4.数据类型 数组名[][列数]={数据1,数据2},{数据2};
    */

    //1.数据类型 数组名[行数][列数];
    cout << "第一种方式" << endl;
    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;
    //外层循环打印行数,内层循环打印列数
    for (size_t i = 0; i < 2; i++)
    {
        for (size_t j = 0; j < 3; j++)
        {
            cout << arr[i][j] << "  ";
        }
        cout << endl;
    }



    //2.数据类型 数组名[行数][列数] = { {数据1,数据2},{数据3,数据4} };
    cout << "第二种方式" << endl;
    int arr2[2][3] =
    {
        {1,2,3},
        {4,5,6}
    };
    for (size_t i = 0; i < 2; i++)
    {
        for (size_t j = 0; j < 3; j++)
        {
            cout << arr2[i][j] << "  ";
        }
        cout << endl;
    }


    //3.数据类型 数组名[行数][列数] = {数据1,数据2,数据3,数据4} ;
    cout << "第三种方式" << endl;
    int arr3[2][3] = { 1,2,3,4,5,6 };
    for (size_t i = 0; i < 2; i++)
    {
        for (size_t j = 0; j < 3; j++)
        {
            cout << arr3[i][j] << "  ";
        }
        cout << endl;
    }



    //4.数据类型 数组名[][列数] = {数据1,数据2},{数据2} ;
    cout << "第四种方式" << endl;
    int arr4[][3] = { 1,2,3,4,5,6 };
    for (size_t i = 0; i < 2; i++)
    {
        for (size_t j = 0; j < 3; j++)
        {
            cout << arr4[i][j] << "  ";
        }
        cout << endl;
    }



    system("pause");
    return 0;
}

 

标签:数据类型,数组名,二维,数组,列数,046,数据
来源: https://www.cnblogs.com/ceovs/p/15226381.html