3.1 - Array's
Array - What are Array's?
- An array is a collection of similar data type
- Elements occupy consecutive memory locations (addresses)
- First element with lowest address and the last element with highest address
- Elements are indexed from 0 to SIZE – 1. Example : 5 elements array (say array[5]) will be indexed from 0 to 4
- Accessing out of range array elements would be “illegal access” Example : Do not access elements array[-1] and array[SIZE]
- Array size can't be altered at run time
Array Reading:
#include
int main()
{
int array[5] = {1, 2, 3, 4, 5};
int index;
index = 0;
do
{
printf(“Index %d has Element %d\n ”, index, array[index]);
index++;
} while (index < 5);
return 0;
}
Array Sorting:
#includeint main() { int array[5]; int index; for (index = 0; index < 5; index++) { scanf(“%d”, &num_array[index]); } return 0; }
Array Initialising:
#include}int main() { int array1[5] = {1, 2, 3, 4, 5}; int array2[5] = {1, 2}; int array3[] = {1, 2}; int array4[]; /* Invalid */ printf(“ %u\n ”, sizeof (array1)); printf(“ %u\n ”, sizeof (array2)); printf(“ %u\n ”, sizeof (array3)); return 0; }
Post a Comment