C masyvai (su pavyzdžiais)

Šioje pamokoje išmoksite dirbti su masyvais. Išmoksite deklaruoti, inicializuoti ir pasiekti masyvo elementus naudodamiesi pavyzdžiais.

Masyvas yra kintamasis, galintis saugoti kelias reikšmes. Pavyzdžiui, jei norite išsaugoti 100 sveikųjų skaičių, galite sukurti tam skirtą masyvą.

 int data(100); 

Kaip deklaruoti masyvą?

 dataType arrayName (masyvo dydis); 

Pavyzdžiui,

 plūduriuojantis ženklas (5);

Čia mes paskelbėme slankiojo kablelio masyvą, ženklą. Jo dydis yra 5. Tai reiškia, kad jame gali būti 5 slankiojo kablelio vertės.

Svarbu pažymėti, kad masyvo dydis ir tipas negali būti pakeisti, kai jis bus deklaruotas.

Prieigos masyvo elementai

Galite pasiekti masyvo elementus pagal indeksus.

Tarkime, kad jūs deklaravote masyvo ženklą, kaip nurodyta aukščiau. Pirmasis elementas yra ženklas (0), antrasis elementas yra ženklas (1) ir pan.

Keletas pranešimų :

  • Masyvuose pirmasis indeksas yra 0, o ne 1. Šiame pavyzdyje ženklas (0) yra pirmasis elementas.
  • Jei masyvo dydis yra n, norint pasiekti paskutinį elementą, naudojamas n-1indeksas. Šiame pavyzdyje pažymėkite (4)
  • Tarkime, kad pradinis adresas mark(0)yra 2120d . Tada mark(1)testamento adresas bus 2124d . Panašiai adresas mark(2)bus 2128d ir pan.
    Taip yra todėl, kad a dydis floatyra 4 baitai.

Kaip inicijuoti masyvą?

Deklaruojant galima inicijuoti masyvą. Pavyzdžiui,

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

Taip pat galite inicijuoti tokį masyvą.

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

Čia mes nenurodėme dydžio. Tačiau kompiliatorius žino, kad jo dydis yra 5, nes mes inicijuojame jį su 5 elementais.

Čia

 ženklas (0) yra lygus 19 ženklui (1) yra lygus 10 ženklui (2) yra lygus 8 ženklui (3) yra lygus 17 ženklui (4) yra lygus 9

Keisti masyvo elementų vertę

 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; 

Įvesties ir išvesties masyvo elementai

Štai kaip galite paimti iš vartotojo įvestį ir išsaugoti ją masyvo elemente.

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

Štai kaip galite atspausdinti atskirą masyvo elementą.

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

1 pavyzdys: masyvo įvestis / išvestis

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

Rezultatas

 Įveskite 5 sveikus skaičius: 1 -3 34 0 3 Rodomi skaičiai: 1 -3 34 0 3 

Čia mes panaudojome forkilpą, kad paimtume iš vartotojo 5 įvestis ir išsaugotume juos masyve. Tada, naudojant kitą forkilpą, šie elementai rodomi ekrane.

2 pavyzdys: apskaičiuokite vidurkį

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

Įdomios straipsniai...