Array: An array is a group of related data items that share a common name.
(or) Homogenous collection of data items that share a common name.
A particular value in an array is identified using its “index number” or “subscript”.
Advantage
The ability to use a single name to represent a collection of items and to refer to an item by specifying the item number enables the user to develop concise and efficient programs.
Declaring for declaration of an array :
Syntax :for declaring array:
Example:
float height [50]
This declare the ‘height’ to be an array containing 50 float elements
int group (10)
This declares the ‘group’ as an array to contain a maximum of 10- integer constants
Individual elements are identified using “ array subscripts”
While complete set of values are referred to as an array, individual values are called “elements”
Example:
To represent a set of 5 numbers by an array, it can be declared as follows
int a[5];
Then computer reserves 5 storage locations each of 2 bytes.
First element is identified by subscript ‘zero’ i.e., a[0] represents first element of the array.
If there are ‘n’ elements in array then subscripts range from 0 to n-1
Initialization
To store values into an array it can be done as follows.
An array can also be initialized at the time of declaration as follows
int a[5] = { 10,20,30,40,50};
Types of Arrays
Arrays are broadly classified into 3 types. They are
One – dimensional arrays
Two – dimensional arrays
Multi – dimensional arrays
Please Login For Further Details...
Q1 : What is the advantage of an array over individual variables?
When storing multiple related data, it is a good idea to use arrays. This is because arrays are named using only 1 word followed by an element number. For example: to store the 10 test results of 1 student, one can use 10 different variable names (grade1, grade2, grade3… grade10). With arrays, only 1 name is used, the rest are accessible through the index name (grade[0], grade[1], grade[2]… grade[9]).
Q2 : How do you access the values within an array?
Arrays contain a number of elements, depending on the size you gave it during variable declaration. Each element is assigned a number from 0 to number of elements-1. To assign or retrieve the value of a particular element, refer to the element number. For example: if you have a declaration that says “intscores[5];”, then you have 5 accessible elements, namely: scores[0], scores[1], scores[2], scores[3] and scores[4].
Q3 : What are the characteristics of arrays in C?
1.An array holds elements that have the same data type.
2. Array elements are stored in subsequent memory locations.
3. Two-dimensional array elements are stored row by row in subsequent memory locations.
4. Array name represents the address of the starting elements.
Q4 : How we can declare an array?
We can declare an array by specify its data type, name and the number of elements the array holds between square brackets immediately following the array name.
syntax: data_type array_name[size];
Q5 : How do you access the values within an array?
Arrays contain a number of elements, depending on the size you gave it during variable declaration. Each element is assigned a number from 0 to number of elements-1. To assign or retrieve the value of a particular element, refer to the element number. For example: if you have a declaration that says “intscores[5];”, then you have 5 accessible elements, namely: scores[0], scores[1], scores[2], scores[3] and scores[4].
Q6 : What do you mean by an array?
Array is a set of similar data type.Arrays objects store multiple variables with the same type.
It can hold primitive types and object references.Arrays are always fixed.
Q7 : How to create an Array?
An Array is declared similar to how a variable is declared, but you need to add [] after the type.
Example: int [] intArray;
We can declare Java array as a field, static field, a local variable, or parameter, like any other variable. An array is a collection of variables of that type.
0 Doubts's