W tym samouczku nauczysz się pracować z tablicami. Nauczysz się deklarować, inicjalizować i uzyskiwać dostęp do elementów tablicy za pomocą przykładów.
Tablica to zmienna, która może przechowywać wiele wartości. Na przykład, jeśli chcesz przechowywać 100 liczb całkowitych, możesz utworzyć dla nich tablicę.
int data(100);
Jak zadeklarować tablicę?
dataType arrayName (arraySize);
Na przykład,
znak pływaka (5);
Tutaj zadeklarowaliśmy tablicę, mark, typu zmiennoprzecinkowego. A jego rozmiar to 5. Oznacza to, że może przechowywać 5 wartości zmiennoprzecinkowych.
Należy pamiętać, że po zadeklarowaniu nie można zmienić rozmiaru i typu tablicy.
Uzyskaj dostęp do elementów tablicy
Dostęp do elementów tablicy można uzyskać za pomocą indeksów.
Załóżmy, że zadeklarowałeś znak tablicy jak powyżej. Pierwszy element to mark (0), drugi element to mark (1) i tak dalej.
Kilka uwag :
- Tablice mają 0 jako pierwszy indeks, a nie 1. W tym przykładzie mark (0) jest pierwszym elementem.
- Jeśli rozmiar tablicy wynosi n, aby uzyskać dostęp do ostatniego elementu,
n-1
używany jest indeks. W tym przykładzie mark (4) - Załóżmy, że adres początkowy
mark(0)
to 2120d . Następnie adres osobymark(1)
będą 2124d . Podobnie adresmark(2)
będzie 2128d i tak dalej.
Dzieje się tak, ponieważ rozmiar afloat
wynosi 4 bajty.
Jak zainicjować tablicę?
Możliwe jest zainicjowanie tablicy podczas deklaracji. Na przykład,
int mark(5) = (19, 10, 8, 17, 9);
Możesz także zainicjować taką tablicę.
int mark() = (19, 10, 8, 17, 9);
Tutaj nie określiliśmy rozmiaru. Jednak kompilator wie, że jego rozmiar to 5, ponieważ inicjalizujemy go z 5 elementami.
Tutaj,
ocena (0) równa się 19 ocena (1) równa się 10 ocena (2) równa się 8 ocena (3) równa się 17 ocena (4) równa się 9
Zmień wartość elementów tablicy
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;
Elementy tablicy wejściowej i wyjściowej
Oto jak możesz pobrać dane wejściowe od użytkownika i zapisać je w elemencie tablicy.
// 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));
Oto, jak możesz wydrukować pojedynczy element tablicy.
// 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));
Przykład 1: Wejście / wyjście tablicy
// 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; )
Wynik
Wpisz 5 liczb całkowitych: 1 -3 34 0 3 Wyświetlanie liczb całkowitych: 1 -3 34 0 3
Tutaj użyliśmy for
pętli do pobrania 5 danych wejściowych od użytkownika i zapisania ich w tablicy. Następnie za pomocą kolejnej for
pętli elementy te są wyświetlane na ekranie.
Przykład 2: Oblicz średnią
// 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).