0% found this document useful (0 votes)
10 views

BCSC 1204 - Lecture 2

Uploaded by

munyagomoris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

BCSC 1204 - Lecture 2

Uploaded by

munyagomoris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

BCSC 1204- Data Structures & Algorithms

LECTURE 2 – ARRAY DATA STRUCTURE


Array Data Structure - Definition

 Arrays are defined as the collection of similar type of data items stored at contiguous
memory locations.
 Arrays are the derived data type in C programming language which can store the primitive
type of data such as int, char, double, float, etc.
 Array is the simplest data structure where each data element can be randomly accessed by
using its index number.
 For example, if we want to store the marks of a student in 6 subjects, then we don't need to
define different variable for the marks in different subject. instead of that, we can define an
array which can store the marks in each subject at a the contiguous memory locations.
 The array marks[10] defines the marks of the student in 10 different subjects where each
subject marks are located at a particular subscript in the array i.e. marks[0] denotes the
marks in first subject, marks[1] denotes the marks in 2nd subject and so on.

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 2


Properties of the Array

 Each element is of same data type and carries a same size i.e. int = 4 bytes.
 Elements of the array are stored at contiguous memory locations where the first element is
stored at the smallest memory location.
 Elements of the array can be randomly accessed since we can calculate the address of each
element of the array with the given base address and the size of data element.
Example, in C language, the syntax of declaring an array is like following:
int arr[10];
char arr[10];
float arr[5];

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 3


Need of using Array

 In computer programming, the most of the cases requires to store the large number of data
of similar type. To store such amount of data, we need to define a large number of variables.
It would be very difficult to remember names of all the variables while writing the programs.
 Instead of naming all the variables with a different name, it is better to define an array and
store all the elements into it.
 In the following example, we have marks of a student in six different subjects. The problem
intends to calculate the average of all the marks of the student.
 In order to illustrate the importance of array, we have created two programs, one is without
using array and other involves the use of array to store marks.

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 4


Program without array:
#include <stdio.h>
void main ()
{
int marks_1 = 56, marks_2 = 78, marks_3 = 88, marks_4 = 76, marks_5 = 56, marks_6 = 89;
float avg = (marks_1 + marks_2 + marks_3 + marks_4 + marks_5 +marks_6) / 6 ;
printf(avg);
}

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 5


Program by using array:
#include <stdio.h>
void main ()
{
int marks[6] = {56,78,88,76,56,89);
int i;
float avg;
for (i=0; i<6; i++ )
{
avg = avg + marks[i];
}
printf(avg);
}

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 6


Complexity of Array operations

Time and space complexity of various array operations are described in the following table.
Time Complexity

Algorithm Average Case Worst Case

Access O(1) O(1)


Search O(n) O(n)
Insertion O(n) O(n)
Deletion O(n) O(n)

Space Complexity: In array, space complexity for worst case is O(n).

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 7


Advantages of Array

 Array provides the single name for the group of variables of the same type therefore, it is
easy to remember the name of all the elements of an array.
 Traversing an array is a very simple process, we just need to increment the base address of
the array in order to visit each element one by one.
 Any element in the array can be directly accessed by using the index.

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 8


Memory Allocation of the array

As we have mentioned, all the data elements of an array are stored at contiguous locations in
the main memory. The name of the array represents the base address or the address of first
element in the main memory. Each element of the array is represented by a proper indexing.
The indexing of the array can be defined in three ways.
o 0 (zero - based indexing) : The first element of the array will be arr[0].
o 1 (one - based indexing) : The first element of the array will be arr[1].
o n (n - based indexing) : The first element of the array can reside at any random index
number.
In the following image, we have shown the memory allocation of an array arr of size 5. The
array follows 0-based indexing approach. The base address of the array is 100th byte. This will
be the address of arr[0]. Here, the size of int is 4 bytes therefore each element will take 4 bytes
in the memory.

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 9


In 0 based indexing, If the size of an array is n then the maximum index number, an element can have
is n-1. However, it will be n if we use 1 based indexing.

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 10


Accessing Elements of an array

To access any random element of an array we need the following information:


 Base Address of the array.
 Size of an element in bytes.
 Which type of indexing, array follows.

Address of any element of a 1D array can be calculated by using the following formula:
Byte address of element A[i] = base address + size * ( i - first index)

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 11


Example :
 In an array, A[-10 ..... +2 ], Base address (BA) = 999, size of an element = 2 bytes,
find the location of A[-1].
L(A[-1]) = 999 + [(-1) - (-10)] x 2
= 999 + 18
= 1017

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 12


2D Array

 2D array can be defined as an array of arrays.


 The 2D array is organized as matrices which can be represented as the collection of rows and
columns.
 However, 2D arrays are created to implement a relational database look alike data structure.
 It provides ease of holding bulk of data at once which can be passed to any number of
functions wherever required.

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 13


How to declare 2D Array

 The syntax of declaring two dimensional array is


very much similar to that of a one dimensional
array, given as follows.
int arr[max_rows][max_columns];
however, It produces the data structure which looks like
following.
The image shows the two dimensional array, the
elements are organized in the form of rows and
columns.
First element of the first row is represented by a[0][0]
where the number shown in the first index is the
number of that row while the number shown in the
second index is the number of the column.

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 14


How do we access data in a 2D array

 Due to the fact that the elements of 2D arrays can be random accessed. Similar to one
dimensional arrays, we can access the individual cells in a 2D array by using the indices of the
cells. There are two indices attached to a particular cell, one is its row number while the
other is its column number.
 However, we can store the value stored in any particular cell of a 2D array to some variable x
by using the following syntax.
int x = a[i][j];
 where i and j is the row and column number of the cell respectively.

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 15


We can assign each cell of a 2D array to 0 by using the following code:
for ( int i=0; i<n ;i++)
{
for (int j=0; j<n; j++)
{
a[i][j] = 0;
}
}

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 16


Initializing 2D Arrays

 We know that, when we declare and initialize one dimensional array in C programming
simultaneously, we don't need to specify the size of the array.
 However this will not work with 2D arrays. We will have to define at least the second
dimension of the array.
 The syntax to declare and initialize the 2D array is given as follows.
int arr[2][2] = {0,1,2,3};
 The number of elements that can be present in a 2D array will always be equal to (number of
rows * number of columns).

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 17


Example : Storing User's data into a 2D array and
printing it. - C Example :
#include <stdio.h> }
void main () printf("\n printing the elements ....\n");
{ for(i=0;i<3;i++)
int arr[3][3],i,j; {
for (i=0;i<3;i++) printf("\n");
{ for (j=0;j<3;j++)
for (j=0;j<3;j++) {
{ printf("%d\t",arr[i][j]);
printf("Enter a[%d][%d]: ",i,j); }
scanf("%d",&arr[i][j]); }
} }

BCSC 1204 - DATA STRUCTURES & ALGORITHMS 18

You might also like