Array is a data structure consists of a group of elements of same data type. We can think it as collection of variable of similar data type.
Suppose you have a team of 10 employees. Id is given for every employee. You want to store id, age and salary of each employee. How will you do that? From the concept of variable, you can declare 10 variables to store individual id, like: intemployeeId0, employeeId1, employeeId2, employeeId3, employeeId4, employeeId5, employeeId6, employeeId7, employeeId8, employeeId9;
Isn’t that a hazard? You are declaring ten variables only for storing ten ids.
It is the case where array facilitates you. Instead of ten variables, you can declare only one variable and store the same type of data in it.
So, if array is able to address different data of same type with same variable name, how does it maintain it? Indexing does the task. For the array declared above, every employee id can be accessed by the index of the array. The elements are: employeeId, employeeId, employeeId, employeeId, employeeId, employeeId, employeeId, employeeId, employeeId, employeeId
0,1,2…9 are the index of individual element. If the size of the array is n, then the index of array elements are from 0 to n-1.
You can use array of age and salary of the employees as well.
The whole array is stored in a single contiguous memory block. Let, you have an array of integer:
Address of the first element is called the base address of the array. Address of ith element of the array can be achieved by following formula:
Address of i element = Address of base + size of element * i;
There are different ways to assign values to elements of an array .
Suppose you have an array for age of employee, int employeeAge . You want to take input and store the value in the array. You can use for loop to initialize the array.
first element of the array, arr get initialized with 20 and other elements are initialized with 0.
Suppose you have an array, which is initialized. Now you want to see the value of each element. Here is an example for doing this stuff:
There are two ways of doing this:
If you want to send some element of an array to a function, you can specify the elements by their index. In the example given below, 1st and 2nd element of the array byteVal are sent to addVal function. The parameters are byteVal and byteVal. Accordiingly, 2nd and 5th element of byteVal array are sent to subtractVal function.
So, when you want to call a function with an array as argument, just write the array name. The parameter passing procedure is just like the variable passing. The difference is in how the called function receives the array. To receive an array, you have to write the array name with array notation. It means that the received parameter is an array. You do not need to define the array size here.
In the example given below, array byteVal is passed to the function showByteVal by only the name and received with an integer array, int getByte .
Suppose you send an array or any array elements to a function. Then you make some operation on the array elements and change value of the element. When the function returns, the data passed as array parameter is changed. Let’s see:
When an array is sent to a function, it is the base address of the array is sent. The base address received by the called function makes operation on the variables memory and so the original value gets changed. So, you will always get the changed value.
Array can be multi dimensional. In this section, we will discuss about two-dimensional array.
Prototype of 2d array is:
Elements of this 2d array are:
Here, 3 is team number and 5 is number of members of every team. You can initialize and access age of each member of each team by this 2d array.
2d array is initialized in the same ways as array.
2d array can be statically initialized in 2 different ways:
2. Assigning values from Standard Input:
How to initialize every element of a 2d array from standard input? Let’s see:
This is same as single dimension array where the specified values are initialized and rest is all 0.
If you have an initialized 2d array and want to get the values, you can act like this:
Array is used for different operations. In some searching and sorting algorithm, array is very important and useful. We are going to watch how array facilitates linear search operation.
Let you have a list of roll numbers who have passed in the last exam. Now, some students come to you, tell their rolls and ask you whether they are in the pass list. The list is not sorted in increasing or decreasing order yet. How will you search their roll? For every student, you will start from the beginning of your list and search every element of the list throughout the end. If roll number is found, you will stop and tell that he/she has passed. Otherwise you would inform the negative result. This type of searching is called linear search.
Here is a way of implementing linear search using array:
2d array can be used for different advanced operations. Out of them, most common example is matrix operation. We can perform any matrix operation using 2d arrays. We are going to explain matrix addition operation using 2d arrays.
Suppose A and B are two matrices. We have to perform matrix addition and save the result in another matrix. Let the new matrix is C. So, the operation we are going to perform is:
C = A + B.
|All times are GMT +5.5. The time now is 17:34.|