Array Homework Help
An Array is a series of elements of the same type placed in Contiguous Memory Locations that can be individually referenced by adding an index to a unique Identifier.
That means that, for example, we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different Identifier. Instead of that, using an Array we can store 5 different values of the same type, int for example, with a Unique Identifier. For example, an Array to contain 5 integer values of type int called Students_Id could be represented like this:
Students_Id:0 | 1 | 2 | 3 | 4 |
type name[element];
for example:
int students_id [5];
float payment[20];
Initializing Array:
There are various ways to initialize an array:
- int id[5] = {`121, 123, 176, 182, 202`};
- int id[] = {`121, 123, 176, 182, 202`};
- Element by element
Using method 1) we have to give elements to be stored in the array in curly brackets. The amount of values between braces { } must not be larger than the number of elements that we declare for the array between square brackets [ ].
In method 2) we don’t need to fix the no of elements. It is determined automatically and stored in the array. Alternatively we can store elements in array element by element.
Accessing Elements:We can access the element of array in the program at any point by using
Array_Name[index]
Remember that index always starts with 0. Hence to access first element we have to put 0 at the place of index.
Example:Example Of Array
Multi-Dimensional Array
Multidimensional arrays can be described as {"arrays of arrays"}. For example, a Bidimensional Array can be imagined as a bidimensional table made of elements, all of them of a same uniform data type. The Array has two subscripts. One subscript denotes the row & the other the column.
The declaration of Two Dimension Arrays is as follows:
data_type array_name[row_size][column_size];
int m[10][20]
Matrix Using Multi-Dimensional Array:
Output is shown below: