# Tutorial dan Contoh Multidimensional Arrays Pemrograman C++

Pada kesempatan ini kami akan mengulas tentang dengan judul artikel .

##### Artikel Terkait C Plus Plus Lainnya
In arrays, you learned about one dimensional array, that is, single variables specifies array. C++ allows programmer to create array of an array known as multidimensional arrays. Consider this example:
`int x;`
Here, x is a two dimensional array. This array can hold 12 elements. You can think this array as table with 3 row and each row has 4 column.
Three dimensional also array works in similar way. For example:
`float x;`
This array x can hold 24 elements. You can think this example as: Each 2 elements can hold 4 elements, which makes 8 elements and each 8 elements can hold 3 elements. Hence, total number of elements this array can hold is 24.

## Multidimensional Array Initialisation

You can initialise a multidimensional array in more than one way. Consider this examples to initialise two dimensional array.
`int test = {2, 4, -5, 9, 0, 9};`
Better way to initialise this array with same array elements as above.
`int  test = { {2, 4, 5}, {9, 0 0}};`

### Initialisation of three dimensional array

`int test = {3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23,                  2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9};`

Better way to initialise this array with same elements as above.

`int test = {                      { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },                     { {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} }                 };`

### Example 1: Two Dimensional Array

C++ Program to display all elements of an initialised two dimensional array.
``#include <iostream>using namespace std;int main() {    int test = {        {2, -5},        {4, 0},        {9, 1}    };    for(int i = 0; i < 3; ++i) {        for(int j = 0; j < 2; ++j) {            cout<< "test["<< i << "][" << ;j << "] = " << test[i][j]<<endl;        }    }    return 0;}``

`test = 2test = -5test = 4test = 0test = 9test = 1`

## Example 2: Two Dimensional Array

C++ Program to store temperature of two different cities for a week and display it.
``#include <iostream>using namespace std;const int CITY = 2;const int WEEK = 7;int main() {    int temperature[CITY][WEEK];    cout<<"Enter all temperature for a week of first city and then second city.\n";    for (int i = 0; i < CITY; ++i) {        for(int j = 0; j < WEEK; ++j) {            cout<<"City "<<i+1<<", Day "<<j+1<<" : ";            cin>>temperature[i][j];        }    }    cout<<"\n\nDisplaying Values:\n";    for (int i = 0; i < CITY; ++i) {        for(int j = 0; j < WEEK; ++j) {            cout<<"City "<<i+1<<", Day "<<j+1<<" = "<< temperature[i][j]<<endl;        }    }    return 0;}``

`Enter all temperature for a week of first city and then second city. City 1, Day 1 : 32City 1, Day 2 : 33City 1, Day 3 : 32City 1, Day 4 : 34City 1, Day 5 : 35City 1, Day 6 : 36City 1, Day 7 : 38City 2, Day 1 : 23City 2, Day 2 : 24City 2, Day 3 : 26City 2, Day 4 : 22City 2, Day 5 : 29City 2, Day 6 : 27City 2, Day 7 : 23Displaying Values:City 1, Day 1 = 32City 1, Day 2 = 33City 1, Day 3 = 32City 1, Day 4 = 34City 1, Day 5 = 35City 1, Day 6 = 36City 1, Day 7 = 38City 2, Day 1 = 23City 2, Day 2 = 24City 2, Day 3 = 26City 2, Day 4 = 22City 2, Day 5 = 29City 2, Day 6 = 27City 2, Day 7 = 23`

### Example 3: Three Dimensional Array

C++ Program to Store value entered by user in three dimensional array and display it.
``#include <iostream>using namespace std;int main() {    int test;    // this array can store 12 elements    cout<<"Enter 12 values: \n";    for(int i = 0; i < 2; ++i) {        for (int j = 0; j < 3; ++j) {            for(int k = 0; k < 2; ++k ) {                cin>>test[i][j][k];            }        }    }    cout<<"\nDisplaying Value stored:"<<endl;/* Displaying the values with proper index. */    for(int i = 0; i < 2; ++i) {        for (int j = 0; j < 3; ++j) {            for(int k = 0; k < 2; ++k ) {                cout<< "test["<<i<<"]["<<j<<"]["<<k<<"] = "<< test[i][j][k]<<endl;            }        }    }    return 0;}``
Output
`Enter 12 values: 123456789101112Displaying Value stored:test = 1test = 2test = 3test = 4test = 5test = 6test = 7test = 8test = 9test = 10test = 11test = 12`
As the number of dimension increases, the complexity also increases tremendously although the concept is quite similar.

Tag : C++ Arrays, Multidimensional Arrays, C++ Function and Array, C++ String.

Selain Sebagai Penyedia Panduan Belajar Database dan Tutorial Pemrograman, Kami Juga Membagikan Kumpulan Source Code Program Aplikasi dan Ebook Pemrograman Terlengkap yang Bisa Anda Dapatkan Secara Gratis di Halaman :

#### Penelusuran terkait C Plus Plus

##### Berlangganan artikel
Daftarkan e-mail Anda dibawah ini
Selanjutnya cek e-mail untuk verifikasi.