Array
An array is a fixed-size sequenced collection of elements of the same data type.
One-Dimmensional Array
A list of items can be given one variable name using only one subscript and such a variable is called a single-subscripted variable or a one-dimensional array.
If we want to represent a set of five numbers, say (20, 30, 10, 55, 05), by an array variable number, then we may declare the variable number as follows:
int number[ 5 ];
and the computer reserve five storage locations as shown below:
The value to the array elements can be assigned as follows:
This would cause the array number to store the value as shown below:
Declaration of One-Dimensional Array
where
type
|
- |
specifies type of data
|
variable-name
|
- |
user-defined name of array
|
size
|
- |
indicate maximum number of elements that can be stored inside the array.
|
Examples
|
|
|
|
float height[50]; /*array contain 50 real element */ |
|
int group[10]; /* array contain 10 integer element */
|
|
char name[10]; /*array contain 10 character*/ */
|
Initialization of one-dimensional array
After an array is declared, its elements must be initialized. Otherwise, they will contain “garbage”.
An array can be initialized at either of the following stages: |
|
At compile time |
|
At run time |
Compile Time Initialization
When we set up an array in C language at compile time. We decide on the array’s size and values while writing the code. This is like planning the array before the program starts running.
It should be good when we know exactly how big the array should be and what values it should have from the beginning.
where
type
|
- |
specifies type of data
|
array-name
|
- |
user-defined name of array
|
size
|
- |
define count of element define in array |
list of value |
|
different values of an array value in the list are separated by comma |
Examples
|
|
|
|
int number[3] = {0,0,1}; |
|
float total[5] = {0.0, 15.75, -10.0};
|
|
char name[ ] = {‘j’, ‘o’,’h’, ‘n’, ‘\0’};
|
The size may be omitted. In such cases, the compiler allocates enough space for all initialized elements |
google Ads.
Two-Dimmensional Array
- A two-dimensional array is a collection of elements placed in m rows and n columns.
- The two-dimensional array is also called matrix.
Declaration of Two-dimensional array
where
type
|
- |
define array datatype.
|
array-name
|
- |
user-defined name of array
|
row
|
- |
represent no. of rows of array |
column |
|
represents no. of column of array |
Initializing of Two-dimensional array
- Declaration with a list of initial values enclosed in braces:
-
When the array is complete initialized with all values, explicitly, we need specify the size of the first dimension.
-
If the values are missing in an initializer, they are automatically set to zero.
- the statement will initialize the first row to one, the first element of the second row to two,
and all other elements two zero.
- When all the elements are to be initialized to zero, the following short-cut method may be used.
Example:
google Ads.