C Mehrdimensionale Arrays (2d- und 3d-Array)

In diesem Tutorial lernen Sie anhand von Beispielen, wie Sie mit mehrdimensionalen Arrays (zweidimensionale und dreidimensionale Arrays) arbeiten.

In der C-Programmierung können Sie ein Array von Arrays erstellen. Diese Arrays werden als mehrdimensionale Arrays bezeichnet. Beispielsweise,

 float x(3)(4);

Hier ist x ein zweidimensionales (2d) Array. Das Array kann 12 Elemente enthalten. Sie können sich das Array als Tabelle mit 3 Zeilen vorstellen und jede Zeile hat 4 Spalten.

Ebenso können Sie ein dreidimensionales (3d) Array deklarieren. Beispielsweise,

 float y(2)(4)(3);

Hier kann das Array y 24 Elemente enthalten.

Initialisieren eines mehrdimensionalen Arrays

So können Sie zweidimensionale und dreidimensionale Arrays initialisieren:

Initialisierung eines 2d-Arrays

 // Different ways to initialize two-dimensional array int c(2)(3) = ((1, 3, 0), (-1, 5, 9)); int c()(3) = ((1, 3, 0), (-1, 5, 9)); int c(2)(3) = (1, 3, 0, -1, 5, 9); 

Initialisierung eines 3D-Arrays

Sie können ein dreidimensionales Array auf ähnliche Weise wie ein zweidimensionales Array initialisieren. Hier ist ein Beispiel:

 int test(2)(3)(4) = ( ((3, 4, 2, 3), (0, -3, 9, 11), (23, 12, 23, 2)), ((13, 4, 56, 3), (5, 9, 3, 5), (3, 1, 4, 9)));

Beispiel 1: Zweidimensionales Array zum Speichern und Drucken von Werten

 // C program to store temperature of two cities of a week and display it. #include const int CITY = 2; const int WEEK = 7; int main() ( int temperature(CITY)(WEEK); // Using nested loop to store values in a 2d array for (int i = 0; i < CITY; ++i) ( for (int j = 0; j < WEEK; ++j) ( printf("City %d, Day %d: ", i + 1, j + 1); scanf("%d", &temperature(i)(j)); ) ) printf("Displaying values: "); // Using nested loop to display vlues of a 2d array for (int i = 0; i < CITY; ++i) ( for (int j = 0; j < WEEK; ++j) ( printf("City %d, Day %d = %d", i + 1, j + 1, temperature(i)(j)); ) ) return 0; ) 

Ausgabe

 Stadt 1, Tag 1: 33 Stadt 1, Tag 2: 34 Stadt 1, Tag 3: 35 Stadt 1, Tag 4: 33 Stadt 1, Tag 5: 32 Stadt 1, Tag 6: 31 Stadt 1, Tag 7: 30 Stadt 2, Tag 1: 23 Stadt 2, Tag 2: 22 Stadt 2, Tag 3: 21 Stadt 2, Tag 4: 24 Stadt 2, Tag 5: 22 Stadt 2, Tag 6: 25 Stadt 2, Tag 7: 26 Anzeigen von Werten : Stadt 1, Tag 1 = 33 Stadt 1, Tag 2 = 34 Stadt 1, Tag 3 = 35 Stadt 1, Tag 4 = 33 Stadt 1, Tag 5 = 32 Stadt 1, Tag 6 = 31 Stadt 1, Tag 7 = 30 Stadt 2, Tag 1 = 23 Stadt 2, Tag 2 = 22 Stadt 2, Tag 3 = 21 Stadt 2, Tag 4 = 24 Stadt 2, Tag 5 = 22 Stadt 2, Tag 6 = 25 Stadt 2, Tag 7 = 26

Beispiel 2: Summe zweier Matrizen

 // C program to find the sum of two matrices of order 2*2 #include int main() ( float a(2)(2), b(2)(2), result(2)(2); // Taking input using nested for loop printf("Enter elements of 1st matrix"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) ( printf("Enter a%d%d: ", i + 1, j + 1); scanf("%f", &a(i)(j)); ) // Taking input using nested for loop printf("Enter elements of 2nd matrix"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) ( printf("Enter b%d%d: ", i + 1, j + 1); scanf("%f", &b(i)(j)); ) // adding corresponding elements of two arrays for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) ( result(i)(j) = a(i)(j) + b(i)(j); ) // Displaying the sum printf("Sum Of Matrix:"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) ( printf("%.1f ", result(i)(j)); if (j == 1) printf(""); ) return 0; )

Ausgabe

Elemente der 1. Matrix eingeben Geben Sie a11: 2 ein; Geben Sie a12: 0,5 ein; Geben Sie a21 ein: -1,1; Geben Sie a22: 2 ein; Elemente der 2. Matrix eingeben Enter b11: 0.2; Geben Sie b12: 0 ein; Geben Sie b21 ein: 0,23; Geben Sie b22: 23 ein; Summe der Matrix: 2,2 0,5 -0,9 25,0

Beispiel 3: Dreidimensionales Array

 // C Program to store and print 12 values entered by the user #include int main() ( int test(2)(3)(2); printf("Enter 12 values: "); for (int i = 0; i < 2; ++i) ( for (int j = 0; j < 3; ++j) ( for (int k = 0; k < 2; ++k) ( scanf("%d", &test(i)(j)(k)); ) ) ) // Printing values with proper index. printf("Displaying values:"); for (int i = 0; i < 2; ++i) ( for (int j = 0; j < 3; ++j) ( for (int k = 0; k < 2; ++k) ( printf("test(%d)(%d)(%d) = %d", i, j, k, test(i)(j)(k)); ) ) ) return 0; )

Ausgabe

 Geben Sie 12 Werte ein: 1 2 3 4 5 6 7 8 9 10 11 12 Anzeigen von Werten: Test (0) (0) (0) = 1 Test (0) (0) (1) = 2 Test (0) (1) (0) = 3 Test (0) (1) (1) = 4 Test (0) (2) (0) = 5 Test (0) (2) (1) = 6 Test (1) (0) (0) ) = 7 Test (1) (0) (1) = 8 Test (1) (1) (0) = 9 Test (1) (1) (1) = 10 Test (1) (2) (0) = 11 Test (1) (2) (1) = 12

Interessante Beiträge...