



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




each stores its own value each is independent of the other but the group can be allocated as a whole
arrays, stacks, queues structures linked list, tree, hash table, graph, โฆ
Homogeneous data structure vs Heterogeneous data structure Statically sized or Dynamically sized Single Unit or Linked Data Structures
C provides a special syntax for accessing cells in an array Allocation of space for array named a: int a[5]; This creates five int variables โnamedโ::a[0],a[1],a[2],a[3],a[4] To modify contents of cell #2 to 6 and cell #1 to 74: a[2] = 6; a[1] = 74; To use the contents of cell #2 and cell #1 : printf(โvalue = %d\nโ,(a[1]-a[2])); This access mechanism to the individual elements is called array indexing In Java / C / C++, array cells are indexed beginning at 0 and going up to n-1 (n is number of cells) Beware: start at 0! and end at one less than the size!!
Array object creation int a[10]; Array indexing a[0]
assuming SIZE was previously #defined as 10
They may be read: x = a[3]; They may be written: a[2] = 7;
int id[MAXSIZE]; float score[MAXSIZE]; char grade[MAXSIZE];
Declared with two dimensions of size rows and columns two pair of square brackets Indexed with two individual indexes
#define ROWMAX 5 #define COLMAX 6 int main(){ int matrix[ROWMAX][COLMAX]; โฆ for (row = 0; row < ROWMAX; row++) for (col = 0; col < COLMAX; col++) matrix[row][col] = row * col; โฆ }
Three or more dimensions #define SIZE1 5 #define SIZE2 3 #define SIZE3 4 โฆ int arr[SIZE1][SIZE2][SIZE3]; for (row = 0; row < SIZE1; row++){ for (col = 0; col < SIZE2; col++){ for (depth = 0; depth < SIZE3; depth++){ printf(โ%.2f โ,arr[row][col][depth]); } printf(โ\nโ); } printf(โ----------------------------------------\nโ); }
For a single dimensional array โ the size does not need to be indicated in the parameter list. For a multi dimensional array โ only the first dimension of size can be omitted in the parameter list.
For the argument when passing a whole array โ no indices are given at all. For the argument when passing a single element โ all indices need to be indicated (in the correct range).