Polja C (z primeri)

V tej vadnici se boste naučili delati z nizi. S pomočjo primerov se boste naučili deklarirati, inicializirati in dostopati do elementov polja.

Matrika je spremenljivka, ki lahko shrani več vrednosti. Če želite na primer shraniti 100 celih števil, lahko zanj ustvarite matriko.

 int data(100); 

Kako prijaviti matriko?

 dataType arrayName (arraySize); 

Na primer

 plovec (5);

Tu smo razglasili matriko, oznako, s plavajočo vejico. Njegova velikost je 5, kar pomeni, da lahko vsebuje 5 vrednosti s plavajočo vejico.

Pomembno je vedeti, da velikosti in vrste matrike ni mogoče spremeniti, ko je deklarirana.

Dostopajte do elementov polja

Do elementov polja lahko dostopate po indeksih.

Denimo, da ste označili matriko kot zgoraj. Prvi element je oznaka (0), drugi element je oznaka (1) itd.

Nekaj ​​osrednjih besed :

  • Polja imajo kot prvi indeks 0 in ne 1. V tem primeru je oznaka (0) prvi element.
  • Če je velikost matrike n, se za dostop do zadnjega elementa uporabi n-1indeks. V tem primeru označite (4)
  • Recimo, da je začetni naslov mark(0)je 2120d . Nato bo naslov mark(1)oporoke 2124d . Podobno, naslov mark(2)bo 2128d in tako naprej.
    To je zato, ker je velikost a float4 bajta.

Kako inicializirati matriko?

Med deklaracijo je mogoče inicializirati matriko. Na primer

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

Takšno polje lahko tudi inicializirate.

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

Tu nismo določili velikosti. Vendar prevajalnik ve, da je njegova velikost 5, saj ga inicializiramo s 5 elementi.

Tukaj,

 oznaka (0) je enaka 19 oznaka (1) je enaka 10 ocena (2) je enaka 8 ocena (3) je enaka 17 oznaka (4) je enaka 9

Spremeni vrednost elementov polja

 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; 

Vhodni in izhodni elementi matrike

Tukaj je opisano, kako lahko od uporabnika vnesete vnos in ga shranite v element matrike.

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

Tukaj je opisano, kako lahko natisnete posamezen element matrike.

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

Primer 1: Vnos / izhod matrike

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

Izhod

 Vnesite 5 celih števil: 1 -3 34 0 3 Prikaz celih števil: 1 -3 34 0 3 

Tu smo uporabili forzanko, da uporabniku vzamemo 5 vhodov in jih shranimo v matriko. Nato se z drugo forzanko ti elementi prikažejo na zaslonu.

Primer 2: Izračunajte povprečje

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

Zanimive Članki...