C-taulukot (esimerkkien kanssa)

Tässä opetusohjelmassa opit työskentelemään matriisien kanssa. Opit ilmoittamaan, alustamaan ja käyttämään taulukon elementtejä esimerkkien avulla.

Taulukko on muuttuja, joka voi tallentaa useita arvoja. Esimerkiksi, jos haluat tallentaa 100 kokonaislukua, voit luoda sille taulukon.

 int data(100); 

Kuinka julistaa taulukko?

 dataType arrayName (arraySize); 

Esimerkiksi,

 uimamerkki (5);

Tässä ilmoitimme taulukon, merkin, liukulukutyyppisen. Ja sen koko on 5. Tämä tarkoittaa, että siihen mahtuu 5 liukulukuarvoa.

On tärkeää huomata, että taulukon kokoa ja tyyppiä ei voi muuttaa, kun se on ilmoitettu.

Pääse matriisin elementteihin

Voit käyttää taulukon elementtejä indekseillä.

Oletetaan, että ilmoitit matriisimerkin kuten yllä. Ensimmäinen elementti on merkki (0), toinen elementti on merkki (1) ja niin edelleen.

Muutama pääpuhuja :

  • Matriisien ensimmäisenä hakemistona on 0, ei 1. Tässä esimerkissä merkki (0) on ensimmäinen elementti.
  • Jos matriisin koko on n, viimeiseen elementtiin pääsemiseksi n-1käytetään hakemistoa. Merkitse tässä esimerkissä (4)
  • Oletetaan, että kohteen aloitusosoite mark(0)on 2120d . Sitten mark(1)testamentin osoite on 2124d . Vastaavasti osoitteen osoite mark(2)on 2128d ja niin edelleen.
    Tämä johtuu siitä, että a: n koko floaton 4 tavua.

Kuinka taulukko alustetaan?

Taulukko on mahdollista alustaa ilmoituksen aikana. Esimerkiksi,

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

Voit myös alustaa tällaisen taulukon.

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

Täällä emme ole määrittäneet kokoa. Kääntäjä tietää kuitenkin, että sen koko on 5, kun aloitamme sen 5 elementillä.

Tässä,

 merkki (0) on yhtä suuri kuin 19 merkki (1) on yhtä suuri kuin 10 merkki (2) on yhtä suuri kuin 8 merkki (3) on yhtä suuri kuin 17 merkki (4) on yhtä suuri kuin 9

Muuta taulukon elementtien arvoa

 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; 

Tulo- ja lähtötaulukkoelementit

Näin voit ottaa syötteen käyttäjältä ja tallentaa sen taulukkoelementtiin.

 // 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)); 

Näin voit tulostaa matriisin yksittäisen elementin.

 // 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)); 

Esimerkki 1: Taulukon tulo / lähtö

 // 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; ) 

Tuotos

 Syötä 5 kokonaislukua: 1 -3 34 0 3 Näytetään kokonaisluvut: 1 -3 34 0 3 

Here, we have used a for loop to take 5 inputs from the user and store them in an array. Then, using another for loop, these elements are displayed on the screen.

Example 2: Calculate Average

 // 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).

Mielenkiintoisia artikkeleita...