C masīvi (ar piemēriem)

Šajā apmācībā jūs iemācīsities strādāt ar masīviem. Jūs iemācīsities deklarēt, inicializēt un piekļūt masīva elementiem, izmantojot piemērus.

Masīvs ir mainīgais, kas var saglabāt vairākas vērtības. Piemēram, ja vēlaties saglabāt 100 veselus skaitļus, varat izveidot tam masīvu.

 int data(100); 

Kā deklarēt masīvu?

 dataType arrayName (arraySize); 

Piemēram,

 pludiņa zīme (5);

Šeit mēs paziņojām par masīvu, atzīmi ar peldošā komata tipu. Un tā lielums ir 5. Tas nozīmē, ka tajā var būt 5 peldošā komata vērtības.

Ir svarīgi atzīmēt, ka masīva lielumu un veidu pēc deklarēšanas nevar mainīt.

Piekļuve masīva elementiem

Masīva elementiem var piekļūt pēc indeksiem.

Pieņemsim, ka jūs deklarējāt masīva atzīmi, kā norādīts iepriekš. Pirmais elements ir atzīme (0), otrais elements ir atzīme (1) un tā tālāk.

Dažas galvenās piezīmes :

  • Masīvu pirmais indekss ir 0, nevis 1. Šajā piemērā atzīme (0) ir pirmais elements.
  • Ja masīva lielums ir n, lai piekļūtu pēdējam elementam, n-1tiek izmantots indekss. Šajā piemērā atzīmējiet (4)
  • Pieņemsim, ka sākuma adrese mark(0)ir 2120d . Tad mark(1)testamenta adrese būs 2124d . Tāpat adrese mark(2)būs 2128d un tā tālāk.
    Tas ir tāpēc, ka a lielums floatir 4 baiti.

Kā inicializēt masīvu?

Deklarācijas laikā ir iespējams inicializēt masīvu. Piemēram,

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

Varat arī inicializēt šādu masīvu.

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

Šeit mēs neesam norādījuši izmēru. Tomēr sastādītājs zina, ka tā lielums ir 5, jo mēs to inicializējam ar 5 elementiem.

Šeit,

 atzīme (0) ir vienāda ar 19 atzīme (1) ir vienāda ar 10 atzīmi (2) ir vienāda ar 8 zīmi (3) ir vienāda ar 17 zīmi (4) ir vienāda ar 9

Mainīt masīva elementu vērtību

 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; 

Ievades un izvades masīva elementi

Lūk, kā jūs varat ņemt ievadi no lietotāja un saglabāt to masīva elementā.

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

Lūk, kā jūs varat izdrukāt atsevišķu masīva elementu.

 // 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. piemērs: Masīva ievade / izvade

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

Rezultāts

 Ievadiet 5 veselus skaitļus: 1 -3 34 0 3 Parādot veselus skaitļus: 1 -3 34 0 3 

Šeit mēs esam izmantojuši forcilpu, lai no lietotāja paņemtu 5 ievades un saglabātu tos masīvā. Pēc tam, izmantojot citu forcilpu, šie elementi tiek parādīti ekrānā.

2. piemērs: Aprēķiniet vidējo

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

Interesanti raksti...