Dans ce didacticiel, vous apprendrez à travailler avec des tableaux multidimensionnels (tableaux à deux dimensions et à trois dimensions) à l'aide d'exemples.
En programmation C, vous pouvez créer un tableau de tableaux. Ces tableaux sont appelés tableaux multidimensionnels. Par exemple,
float x(3)(4);
Ici, x est un tableau à deux dimensions (2d). Le tableau peut contenir 12 éléments. Vous pouvez considérer le tableau comme un tableau avec 3 lignes et chaque ligne a 4 colonnes.
De même, vous pouvez déclarer un tableau en trois dimensions (3D). Par exemple,
float y(2)(4)(3);
Ici, le tableau y peut contenir 24 éléments.
Initialisation d'un tableau multidimensionnel
Voici comment vous pouvez initialiser des tableaux bidimensionnels et tridimensionnels:
Initialisation d'un tableau 2D
// 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);
Initialisation d'un tableau 3D
Vous pouvez initialiser un tableau en trois dimensions de la même manière qu'un tableau en deux dimensions. Voici un exemple,
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)));
Exemple 1: tableau bidimensionnel pour stocker et imprimer des valeurs
// 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; )
Production
Ville 1, Jour 1:33 Ville 1, Jour 2:34 Ville 1, Jour 3:35 Ville 1, Jour 4:33 Ville 1, Jour 5:32 Ville 1, Jour 6:31 Ville 1, Jour 7:30 Ville 2, Jour 1:23 Ville 2, Jour 2:22 Ville 2, Jour 3:21 Ville 2, Jour 4:24 Ville 2, Jour 5:22 Ville 2, Jour 6:25 Ville 2, Jour 7:26 Affichage des valeurs : Ville 1, Jour 1 = 33 Ville 1, Jour 2 = 34 Ville 1, Jour 3 = 35 Ville 1, Jour 4 = 33 Ville 1, Jour 5 = 32 Ville 1, Jour 6 = 31 Ville 1, Jour 7 = 30 Ville 2, Jour 1 = 23 Ville 2, Jour 2 = 22 Ville 2, Jour 3 = 21 Ville 2, Jour 4 = 24 Ville 2, Jour 5 = 22 Ville 2, Jour 6 = 25 Ville 2, Jour 7 = 26
Exemple 2: somme de deux matrices
// 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; )
Production
Entrez les éléments de la 1ère matrice Entrez a11: 2; Entrez a12: 0,5; Entrez a21: -1,1; Entrez a22: 2; Entrez les éléments de la 2ème matrice Entrez b11: 0,2; Entrez b12: 0; Entrez b21: 0,23; Entrez b22: 23; Somme de la matrice: 2,2 0,5 -0,9 25,0
Exemple 3: tableau en trois dimensions
// 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; )
Production
Saisissez 12 valeurs: 1 2 3 4 5 6 7 8 9 10 11 12 Affichage des valeurs: 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