Arrays
- Storing multiple data items of the same data type that can be represent by a common name
- The data items are allocated in continuous memory locations and can be referred by a single name
- Example:
- int studentMark [5]
Index | Value | Memory Location |
0 | 75 | 100000 |
1 | 80 | 100002 |
2 | 85 | 100004 |
3 | 90 | 100006 |
4 | 95 | 100008 |
- Each individual data item is referred as an array element
- studentMark[0] = 75
- Each array element is referred to by the array name followed by one or more subscripts with each subscript enclosed in square brackets
- studentMark[0]
- Each subscripts must be expressed as non-negative integer
- An array must be declared and define before it can be used
- Declaration and definition tell the compiler the name of the array, the type of data and the number of elements
- Example
- An array name studentMark type integer and size 5
Data Type------Array Name-------Size
int--------------studentMark-------[5]
# include
int main( )
{
int n[10] = {32, 27, 64, 18, 95, 14, 90, 70, 60, 37};
int i;
printf("%s%13s\n", "Element", "Value");
for(i = 0; i <= 9; i++) printf("%7d%13d\n", i, n[i]); return 0; }
Output:
Element | Value |
0 | 32 |
1 | 27 |
2 | 64 |
3 | 18 |
4 | 95 |
5 | 14 |
6 | 90 |
7 | 70 |
8 | 60 |
9 | 37 |
Multi Dimensional Array
- The number of subscripts determines the dimensionality of the array
- The multi dimensional arrays are defined in much the same manner as one-dimensional arrays
- A pair of square brackets is required for each dimension
- A two dimensional array will require two pairs of square brackets
- int studentMark [5][10]
No comments:
Post a Comment