Introduction to Arrays

http://install4install.com

Fig 1 : Table

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.

Fig 2 : An integer array

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];
char grades[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.

Screen Shot 2014-03-15 at 3.15.31 PM

Fig 3 : An integer array

Store values in an array

int marks[5] = {20,40,50,10,30}; //(Fig 4)

Fig 4 : Initialized Integer Array

float percnt[5] = {95.2,45.5,20.0,30.6,67.9}; //Fig 5
Fig 6 : Initialized float array

Fig 5 : Initialized float array

char grades[5] = {a,d,c,a,b}; //Fig 6
Screen Shot 2014-03-15 at 3.31.04 PM

Fig 6 : Initialized character array

 

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++)
    {
    	printf("%s","Please enter a number: ");
        scanf("%d",&rollno[i]);
        printf("\nYou entered: %d\n", rollno[i]);
    }
}

Run

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.

Fig 8 :  i=0

Fig 7 : i=0

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.

Fig 9: i=1

Fig 8 : i=1

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,

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

Screen Shot 2014-03-15 at 3.45.53 PM

Fig 9

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

TipUse 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



Short URL: http://tinyit.cc/ce4fe9
Author: Cusp2207 on March 15, 2014
Category: Computer Science, Data Structures
Tags:

Leave a Reply

Last articles