C massiivid (koos näidetega)

Selles õpetuses õpitakse massiividega töötama. Õpid näidete abil massiivi elemente deklareerima, initsialiseerima ja neile juurde pääsema.

Massiiv on muutuja, mis suudab salvestada mitu väärtust. Näiteks kui soovite salvestada 100 täisarvu, saate selle jaoks luua massiivi.

 int data(100); 

Kuidas massiivi deklareerida?

 dataType arrayName (arraySize); 

Näiteks,

 ujukimärk (5);

Siin kuulutasime massiivi, märgi, ujukoma tüüpi. Ja selle suurus on 5. See tähendab, et see mahutab 5 ujukoma väärtust.

Oluline on märkida, et massiivi suurust ja tüüpi ei saa pärast selle deklareerimist muuta.

Juurdepääs massiivi elementidele

Massiivi elementidele pääseb juurde indeksite järgi.

Oletame, et deklareerisite massiivi märgi nagu ülal. Esimene element on märk (0), teine ​​element on märk (1) ja nii edasi.

Vähesed põhisõnumid :

  • Massiivide esimese indeksina on 0, mitte 1. Selles näites on märk (0) esimene element.
  • Kui massiivi suurus on n, kasutatakse viimasele elemendile juurdepääsemiseks n-1indeksit. Selles näites märkige (4)
  • Oletame, et algusaadressmark(0) on 2120d . Siis mark(1)saab testamendi aadress 2124d . Samamoodi aadress mark(2)on 2128d ja nii edasi.
    Seda seetõttu, et a suurus floaton 4 baiti.

Kuidas massiivi lähtestada?

Deklaratsiooni ajal on võimalik massiivi lähtestada. Näiteks,

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

Sellise massiivi saate ka lähtestada.

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

Siin pole me suurust täpsustanud. Koostaja teab aga, et selle suurus on 5, kuna initsialiseerime selle 5 elemendiga.

Siin,

 märk (0) on võrdne 19 märgiga (1) on võrdne 10 märgiga (2) on võrdne 8 kaubamärgiga (3) on võrdne 17 märgiga (4) on võrdne 9-ga

Massiivi elementide väärtuse muutmine

 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; 

Sisend- ja väljundmassiivi elemendid

Siit saate teada, kuidas saate kasutajalt sisendi võtta ja massiivi elementi salvestada.

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

Massiivi üksiku elemendi saate printida järgmiselt.

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

Näide 1: massiivi sisend / väljund

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

Väljund

 Sisestage 5 täisarvu: 1–3 34 0 3 Täisarvude kuvamine: 1–3 34 0 3 

Siin oleme kasutanud fortsüklit, et võtta kasutajalt 5 sisendit ja salvestada need massiivi. Seejärel forkuvatakse teise silmuse abil need elemendid ekraanil.

Näide 2: arvutage keskmine

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

Huvitavad Artiklid...