Arrays
Hello people, in this article we are going to study about a
data structure.
To study this, we need to consider a situation when we have
10 students in a class and we need to read and write marks of 10 students. So
in generic way we need to declare 10 integer variables, for example.
int marks1, marks2,
marks3, marks4, marks5, marks6, marks7, marks8, marks9, marks10;
And for scaning and printing the values of these variables we
need to use “cin>>….” And “cout<<…” for ten ten different times.
But is it possible to do so when we have a large number of
students in a class, e.g, 500 or 1000. No, So in that case we use arrays.
An array is a
collection of similar data elements. These data elements have the same data
type. The elements of the array are stored in consecutive memory locations and
are referenced by an index(also known as subscript). The subscript is an ordinal
number which is used to identify an element of the array.
DECLARATION
OF ARRAYS
Declaraing of array requires following three specifications;
1. DATA TYPE :
The kind of values it can store, e.g., int, char, float, double.
2. NAME : to identify the array
3. SIZE : the
maximum number of values that the array can hold.
Syntax for declaring arrays :
type name[size];
the size indicates the
number of elements that can be stored in an array.
If the statement is
int marks[10];
In C/C++ array indexing
starts from zero. The first element will be stored in marks[0], second element
will be stored in marks[1], and so on. Therefore, the last element, that is the
10th element will be stored in marks[9].
These 0,1,…10 written within
the brackets are indexes.
Here, 0 is the lower bound
and 9 is the upper bound of this array.
Different ways of declaring arrays :
1. Array declaration by
specifying size :
int arr[10];
2. Array declaration by
initializing elements :
int
arr[] = {10,67,90,87,12};
3. Array declaration by
specifying size and initializing elements :
int
arr[5] = {12,15};
Here, arr[2] = arr[3] = arr[4] =0.
Q.) Calculate the address of marks for marks[4] in the
marks[] = {99,87,54,21,36,54}
if the base
address = 1000, in the 64-bit machine.
Ans. The base address of the marks[4] = 1000 + 4 *(4-0)
= 1016
Accessing the elements in an Array
To access any particular array we usually call them with
the name of array and there index as a subscript. E.g., Arr[0], Arr[1], Arr[2],etc.
For accessing each and every
item of an array we need to use loops, there is no such function, with the help
of which we can print and write for each and every element of an array.
For Example –
For scanning ‘n’ number of elements of an array, we use ‘for’
loop like this
for(i=0;i<n;i++)
{
scanf(“%d”,&arr[i]);
}
For printing ‘n’ number of elements of an array, we use ‘for’
loop like this
for(j=0;j<n;j++)
{
printf(“%d”,arr[i]);
}
Now have a look at the
complete program to understand these two implementations
MERGING OF TWO ARRAYS –
14 |
23 |
58 |
46 |
- Array 1
45 |
56 |
25 |
78 |
12 |
- Array 2
14 |
23 |
58 |
46 |
45 |
56 |
25 |
78 |
12 |
- Array 3
We are coming with a series of posts regarding various data structures and their implementations. Do subscribe to our daily blog update by clicking here.
We want to acknowledge, Reema Thareja Ma'am, her book Data Structures Using C helped us alot during our research for this article, also this is the best one, you should go for.
If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via instagram "@coding.winds".
We also have an article on space complexity and time complexity , do give them a read.
No comments:
Post a Comment