Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Arrays: Data-Type Array-Name (Array-Size)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Arrays

An array is a collection of variables of the same data type accessed by indexing.


Each item in an array is called an element. All elements in an array are referenced by
the name of the array and are stored in a set of consecutive memory slots.

Arrays are classified as single dimensional and multidimensional arrays. Array


containing single column elements are called single dimensional array or one
dimensional or linear array. More than one column are called multidimensional array.

int x;

Declaring Single Dimensional Array

Like any other variable arrays must be declared explicitly before they are used.
The general form of declaration is:

data-type Array-Name[Array-Size];

Here, data-type is the type specifier that indicates what data type the declared array is
such as int, float double or char.

Array-Name is the name of the declared array and rules of declaring the variable
applies to array name.

Array-Size defines how many elements the array contains. This is always an integer.
Note that the brackets [ and ] are required in declaring an array. The bracket pair [ and
] is also called the array subscript operator.

For example, an array of integers is declared in the following statement,


int a[8];

a[0
]
a[1
]
a[2
]
a[3
]
a[4
]
a[5
]
a[6
]
a[7
]

Array Index

An array is implemented as continuous storage, the index-based access. To


access element n, in array, position or index of array is important the index of the first
element (sometimes called the "origin") varies by language.
There are three main implementations: zero-based, one-based, and n-based
arrays, for which the first element has an index of zero, one, or a programmer-specified
value respectively. The zero-based array is more natural and adopted by the C
programming language.

For example, consider the array declaration int num[6]; This array consists of five
elements and index of first element is 0 , second is one and so on.
This described in the below figure.
Initialization of One Dimensional Array

There are two categories of array namely


1. Static array
2. Dynamic array

Declaration for static array specifies the array size, which cannot be altered
afterwards. But for dynamic arrays the size can be altered. Using dynamic memory the
dynamic array’s size can be modified. This is discussed pointers in chapters.
Once an array is declared, it must be initialized. Otherwise array will contain the
garbage values. There are two different ways in which we can initialize the static array:
1. Compile time
2. Run time

Compile Time Initialization

This initialization is done while writing the program itself. Following is the syntax
of initialization of the array.

data-type array-name[size] = { list of values separated by comma };

For example:
int var[5] = {5, 7, 9, 3, 4};

Here, the array variables are initialized with the values given in front of it. That is,
the 0th element of array will contain 5, 1st will contain 7 and so on. Remember, the
array indexing or sub-scripting is always done from 0th position. The values of the array
will be get stored in following manner.
Each element in the array will be referred individually by its index such as var[0],
var[1], var[2], . . .

They can also be initialized individually such as:

var[0] = 5;
var[1] = 7;
var[2] = 9;
etc. after declaration of the array.

In the array declaration, the size can also be omitted. In such cases the
compiler will automatically identify the total number of elements and size will be given
to it.
For example:
int max[ ] = {8, 6, 7, 4, 5, 3, 0, 1};

This array is not declared with size, but initialized with values. Here, size of the
array will be automatically set to 8. Now it can be referred as the general array.

float x[5] = {1.2, 6.9, 4.1};


x[0]=1.2
x[1]=6.9
x[2]=4.1
x[3]=0
x[4]=0

Here, array ‘x’ is having size 5, but only 0th, 1st, and 2nd elements are declared
with values. The remaining 3rd and 4th element of the array will be automatically set to
0. This property is applicable to all numerical type of the array in C. However, if we have
more values than the declared size, the compiler will give an error.

Run Time Initialization

An array can initialized at run time by the program or by taking the input from the
keyboard. Generally, the large arrays are declared at run time in the program itself.
int sum[20];
for (i = 0;i<20;i++)
{
scanf(“%d”, &sum[i]);
}
Here, all the elements of the array ‘sum’ are initialized with the value one. The
loop control structures are applicable in these cases. The scanf function can also be
used to input the values from the user. In such cases, the loop control structure is
applicable.

For example:
int sum[10], x;
printf(“Enter 10 numbers: ”);
for(x=0;x<10;x++)
scanf(“%d”, sum[x]);

Here, the array ‘sum’ is initialized by taking the values as input from the
keyboard.

Entering Data Into The Array


To enter the data into array, consider the below lines.
for (i=0; i<= 29; i++)
{
printf (“\n Enter marks”)
scanf (“%d”, &marks [i]);
}

for (i=0; i<= 29; i++)


{
printf (“\n Mark %d is %d”, i, marks[i]);
}

The above section will read about 30 elements numbered from 0 to 29 in to the array
named marks . This will take input from the user repeatedly 30 times.

Reading Data From Array


In order to understand the working of array consider the below programs.

1. Write a program to initialize ten elements of an array.


/* Initializing an array */
#include <stdio.h>
main()
{
int i;
int list[10];
for (i=0; i<10; i++)
{
list[i] = i + 1;
printf( "list[%d] is initialized with %d.\n", i, list[i]);
}
}

Output:
list [0] is initialized with 1.
list [1] is initialized with 2.
list [2] is initialized with 3.
list [3] is initialized with 4.
list [4] is initialized with 5.
list [5] is initialized with 6.
list [6] is initialized with 7.
list [7] is initialized with 8.
list [8] is initialized with 9.
list [9] is initialized with 10.

You might also like