# Introduction to Arrays

Well, we all know what a multiplication table does(Fig 1). We pick any number and multiply with Series from 1-10,and hence we get the resultant series. For example If we want to store this result sequence 5 10 15 20 in computer memory using any programming language, what would we do?

To store this or any sequence in the programming world I can use Arrays as my data structure.Array is storehouse/collection of similar data items. By Data Items I mean the entries or the actual data, which needs to be stored. In this case 5 10 15 20 are the data items.
To store items in an array, they must be of same Data Type(Fig 2). If an array stores integer values all the items must be of integer type. Any other data type like float or char is not to be used with integer data type and the case hold for vice-versa also.

The elements of array are stored in consecutive memory locations called as  Index.

#### Declaration of an Array

Specify three things in order to declare an array:

1. Data type: - Example: int , char, float
2. Name of array
3. Size: - Maximum no of values that array can store.

Examples:

int marks [5];  //(Fig 3)
float  percnt[5];

In the first example:

1. Data Type of Array is int, which means it is meant to store integer values.

2. Name of array is marks.

3.The maximum no of values that can be stored in this array is 5.

#### Store values in an array

• ###### Initialization

The values of array are directly initialized/given by the user at the time of program creation.

int marks[5] = {20,40,50,10,30}; //(Fig 4)
float percnt[5] = {95.2,45.5,20.0,30.6,67.9}; //Fig 5
char grades[5] = {a,d,c,a,b}; //Fig 6

• ###### Input the values

The user enters the values of array at run time. It is done through the concept of loops.

Example

In C language:

/* visit http://letslearncs.com for complete Computer Science tutorials*/
#include <stdio.h>

void main(){
int   i ;
int  rollno[5] ;
for(i=0;i<5;i++)
{
scanf("%d",&rollno[i]);
printf("\nYou entered: %d\n", rollno[i]);
}
}

1. Variable i is declared of integer type to use in loop.

2. Array rollno is declared to store roll numbers of 5 students as the size specified is 5. Since rollno is an integer value, we took int as the data type.

3. The For loop is used to read five values from the user at run time using the standard scanf function of stdio.h library.

###### Functionality

Initial value of i =0 which is less than 5 (the upper bound of the array rollno). Thus the for loop condition is true and it will enter the loop.

Now, rollno[i] is rollno[0]. Say user enters 25.So when i=0, the array would like as shown in fig 7.

Value of i is incremented to 1. Now when the value of  i=1 which is still less than 5. Now, say user enters 30. The array would like as shown in fig 8.

The same process goes till the value of i  becomes 5 and the condition becomes false (remember: we start the loop from 0 not 1). So it wont enter the loop and will thus stop reading the next value from user.

#### Calculating the Memory Address of array's elements

The address of data item can be found by formula:

A[k] = BA(A) + w(k - lower bound)
Here,

• A is an array
• k is the index of data item for which we need to find address. By address I mean the location of data item in computer memory (Note :address and index are two different things in arrays).
• BA is the base address of array A.
• w is the word size of one element in memory. For e.g. size of int is 2,float is 4 and char is 1 byte.
• Lower bound is starting index of an array.

Consider an array(Fig 9), which stores percentage of 4 students.

Let the BA of percnt array be 2000 and w is 4 of type float.

Use int BA = &percnt to calculate the actual base address in C.

If we need to find memory location of the value 30.6 which is at index 3 then we can use the following formula :
A[k] = BA(A) + w( k - lower bound)
percnt[3] = 2000 + 4(3 - 0)
=2000 + 12
= 2012

5 (100%) 1 vote