Tableaux C (avec exemples)

Dans ce didacticiel, vous apprendrez à travailler avec des tableaux. Vous apprendrez à déclarer, initialiser et accéder aux éléments d'un tableau à l'aide d'exemples.

Un tableau est une variable qui peut stocker plusieurs valeurs. Par exemple, si vous souhaitez stocker 100 entiers, vous pouvez créer un tableau pour celui-ci.

 int data(100); 

Comment déclarer un tableau?

 dataType arrayName (arraySize); 

Par exemple,

 marque flottante (5);

Ici, nous avons déclaré un tableau, marque, de type virgule flottante. Et sa taille est de 5. Cela signifie qu'il peut contenir 5 valeurs à virgule flottante.

Il est important de noter que la taille et le type d'un tableau ne peuvent pas être modifiés une fois qu'il est déclaré.

Accéder aux éléments de la baie

Vous pouvez accéder aux éléments d'un tableau par des indices.

Supposons que vous ayez déclaré une marque de tableau comme ci-dessus. Le premier élément est mark (0), le deuxième élément est mark (1) et ainsi de suite.

Quelques keynotes :

  • Les tableaux ont 0 comme premier index, pas 1. Dans cet exemple, mark (0) est le premier élément.
  • Si la taille d'un tableau est n, pour accéder au dernier élément, l' n-1index est utilisé. Dans cet exemple, marquez (4)
  • Supposons que l'adresse de départ de mark(0)soit 2120d . Ensuite, l'adresse du mark(1)testament sera 2124d . De même, l'adresse de mark(2)sera 2128d et ainsi de suite.
    C'est parce que la taille de a floatest de 4 octets.

Comment initialiser un tableau?

Il est possible d'initialiser un tableau lors de la déclaration. Par exemple,

 int mark(5) = (19, 10, 8, 17, 9);

Vous pouvez également initialiser un tableau comme celui-ci.

 int mark() = (19, 10, 8, 17, 9);

Ici, nous n'avons pas spécifié la taille. Cependant, le compilateur sait que sa taille est de 5 car nous l'initialisons avec 5 éléments.

Ici,

 repère (0) est égal à 19 repère (1) est égal à 10 repère (2) est égal à 8 repère (3) est égal à 17 repère (4) est égal à 9

Modifier la valeur des éléments du tableau

 int mark(5) = (19, 10, 8, 17, 9) // make the value of the third element to -1 mark(2) = -1; // make the value of the fifth element to 0 mark(4) = 0; 

Éléments de matrice d'entrée et de sortie

Voici comment vous pouvez prendre une entrée de l'utilisateur et la stocker dans un élément de tableau.

 // take input and store it in the 3rd element scanf("%d", &mark(2)); // take input and store it in the ith element scanf("%d", &mark(i-1)); 

Voici comment vous pouvez imprimer un élément individuel d'un tableau.

 // print the first element of the array printf("%d", mark(0)); // print the third element of the array printf("%d", mark(2)); // print ith element of the array printf("%d", mark(i-1)); 

Exemple 1: entrée / sortie de matrice

 // Program to take 5 values from the user and store them in an array // Print the elements stored in the array #include int main() ( int values(5); printf("Enter 5 integers: "); // taking input and storing it in an array for(int i = 0; i < 5; ++i) ( scanf("%d", &values(i)); ) printf("Displaying integers: "); // printing elements of an array for(int i = 0; i < 5; ++i) ( printf("%d", values(i)); ) return 0; ) 

Production

 Entrez 5 entiers: 1 -3 34 0 3 Affichage des entiers: 1 -3 34 0 3 

Ici, nous avons utilisé une forboucle pour prendre 5 entrées de l'utilisateur et les stocker dans un tableau. Ensuite, à l'aide d'une autre forboucle, ces éléments sont affichés à l'écran.

Exemple 2: calculer la moyenne

 // Program to find the average of n numbers using arrays #include int main() ( int marks(10), i, n, sum = 0, average; printf("Enter number of elements: "); scanf("%d", &n); for(i=0; i  

Output

 Enter n: 5 Enter number1: 45 Enter number2: 35 Enter number3: 38 Enter number4: 31 Enter number5: 49 Average = 39 

Here, we have computed the average of n numbers entered by the user.

Access elements out of its bound!

Suppose you declared an array of 10 elements. Let's say,

 int testArray(10);

You can access the array elements from testArray(0) to testArray(9).

Now let's say if you try to access testArray(12). The element is not available. This may cause unexpected output (undefined behavior). Sometimes you might get an error and some other time your program may run correctly.

Hence, you should never access elements of an array outside of its bound.

Multidimensional arrays

In this tutorial, you learned about arrays. These arrays are called one-dimensional arrays.

In the next tutorial, you will learn about multidimensional arrays (array of an array).

Articles intéressants...