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

2_Array

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

2_Array

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

DATA STRUCTURE USING C (BE2106)

ARRAY 2ND SEMESTER

C.V.Raman College of Engineering


OUTLINES

• Need of Array
• Introduction
• One-Dimensional Array
• Two-Dimensional Array
• Sorting Technique
– Bubble Sort
• Multi-Dimensional Array
• Sparse Matrix
• Question Answer & Discussion

C.V.Raman College of Engineering


Why need array ?
"We have a list of 1000 students' marks of an
integer type. If using the basic data type (int),
we will declare something like the following…"

int studMark0, studMark1, studMark2, ..., studMark999;

C.V.Raman College of Engineering


Contd…

Can you imagine how long we have to write the declaration part by using normal
variable declaration?

int main(void)
{
int studMark1, studMark2, studMark3,
studMark4, …, …, studMark998,
stuMark999, studMark1000;


return 0;
}

C.V.Raman College of Engineering


Contd…

 By using an array, we just declare like this,


int studMark[1000];

It will reserve 1000 contiguous memory locations for storing the


students’ marks.

C.V.Raman College of Engineering


Introduction

• An array is an useful data structure to store


homogeneous data elements.

• Array is a derived data type.

• All the array elements are stored in contiguous


memory locations.

C.V.Raman College of Engineering


Types of Arrays

One - Dimensional Array

Two - Dimensional Array

Multi - Dimensional Array

C.V.Raman College of Engineering


One-Dimensional Array
 A collections of data elements given by
one variable name using only one
subscript
Declaration: data_type Array_name[size]

data_type : valid data type like int, float or char


Arrayname : valid identifier
size : maximum number of elements that can be stored in array
Example:

int arr[5];
C.V.Raman College of Engineering
One-Dimensional Array

• An elements of an array must be initialized,


otherwise they may contain “garbage” value.
• An array can be initialized at either of the
following stages
– At compile time
– At run time

C.V.Raman College of Engineering


One-Dimensional Array

 Compile Time Initialization:

int a[5]={1,2,3,4,5}

Element a[0] a[1] a[2] a[3] a[4]


Value 1 2 3 4 5
Address 2050 2052 2054 2056 2058

C.V.Raman College of Engineering


Compile Time Initialization

 We can initialize the elements of arrays as:


type array_name[size] = {list of values};
For example,
int number[3] = {5,10,15};
float total[5] ={0.0, 15.75, -10.9};
 The size may be omitted
int counter[] = {1,1,1,1};

 The character array may be initialized in the similar manner


char name[] = {‘C’,’V’,’R’,’C’,’E’,’\
0’};
char name[] = “CVRCE”;

C.V.Raman College of Engineering


Accessing of 1D Array Elements

index element

C.V.Raman College of Engineering


Memory Representation of an Array

• The array elements are stored in contigous


memory location
A[0] A[1] A[2] A[3] A[4]

1000 1002 1004 1006 1008

Address of the ith element(Address(A[i]))=B+w*(i-LB)


where B=Base address of the array A
w=Size of the each element
LB =Lower index of the array
C.V.Raman College of Engineering
Address Calculation:

Ex:
int marks[]={99,67,78,56,88,90,34,85}
Calculate the address of mark[5] if the base
address=1000.
Ans:
marks[5]=1000+2*(5-0)=1010

C.V.Raman College of Engineering


Run-Time initialization
 An array can be explicitly initialized at run time.
void main()
{
int arr[5],i; 1000
52
43
printf(“Enter
0
1 array elements:”);
for(i=0;i<5;i++) 1002
{
scanf(“%d”,&arr[i]);
1004
}
}
1006
Enter Array elements:
10 20 30 40 50
1008

C.V.Raman College of Engineering


Run-Time initialization
printf(“Array elements:”);
Array elements:
for(i=0;i<5;i++)
{ 10

printf(“%d”,arr[i]); 20
}
30
}
40

50

C.V.Raman College of Engineering


PROGRAMS
Program to input elements for an array and display the array elements.
#include<stdio.h>
void main( ){
int a[5],i;
printf(“Enter the elements for the array:”);
for(i=0;i<5;i++)
scanf(“%d”,&a[i]); //reading the array elements
printf(“The array elements are:\t”);
for(i=0;i<5;i++)
printf(“%d\t”,a[i]); //displaying the array
elements
}
Output: Enter the elements for the array: 1 2 3 4 5
The array elements are: 12345

C.V.Raman College of Engineering


PROGRAMS
Program to input elements for an array and calculating the sum of all
array elements.
#include<stdio.h>
void main( ){
int a[5],I,sum=0;
printf(“\nEnter the elements of the array:”);
for(i=0;i<5;i++)
scanf(“%d”,&a[i]); //reading the array elements
for(i=0;i<5;i++)
sum=sum+a[i];
printf(“\nThe array elements are:\t”);
for(i=0;i<5;i++)
printf(“%d\t”,a[i]); //displaying the array elements
printf(“\nThe sum of elements =%d”,sum);
}
Output: Enter the elements of the array: 1 2 3 4 5
The array elements are:1 2 3 4 5
Sum C.V.Raman
of elements=15College of Engineering
ARRAY OPERATION

• Insertion (Inserting an element in to an existing array)


• Deletion (Removing an element from an array)
• Searching (Searching an element in an array)
– Linear (Sequential) Search
– Binary Search
• Merging(Combining two different arrays in to a single
array)
• Sorting(Arranging the array elements either in
ascending or descending order)

C.V.Raman College of Engineering


INSERTION OPERATION
ALGORITHM: INSERT(A,N,POS,VAL)
Input: A is the linear array with N elements.
POS is the positive integer such that POS<=N
Output: This algorithm inserts an element VAL in to the POSth
position in the array A.
Step 1: Set I=N
Step 2: Repeat Step 3 and Step 4 while I>=POS
Step 3: Set A[I+1]=A[I]
Step 4: Set I=I-1
[End of Step 2 Loop]
Step 5: Set A[POS]=VAL
Step 6: Set N=N+1
Step 7: Exit

C.V.Raman College of Engineering


INSERTION OPERATION

a[0] 10

a[1] 20

a[2] 30

a[3] 70

a[4] 25

a[5]

a[6]

C.V.Raman College of Engineering


INSERTION OPERATION

a[0] 10
Insert 45
a[1] 20
Position
a[2] 30

a[3] 70

a[4] 25

a[5]

a[6]

C.V.Raman College of Engineering


INSERTION OPERATION

a[0] 10
Insert 45
a[1] 20
Position
a[2] 30

a[3] 70

a[4] 25

a[5]

a[6]

C.V.Raman College of Engineering


INSERTION OPERATION

a[0] 10
Insert 45
a[1] 20
Position
a[2] 30

a[3] 70

a[4]

a[5] 25

a[6]

C.V.Raman College of Engineering


INSERTION OPERATION

a[0] 10
Insert 45
a[1] 20
Position
a[2] 30

a[3]

a[4] 70

a[5] 25

a[6]

C.V.Raman College of Engineering


INSERTION OPERATION

a[0] 10
Insert 45
a[1] 20

a[2] 30

a[3]

a[4] 70

a[5] 25

a[6]

C.V.Raman College of Engineering


INSERTION OPERATION

a[0] 10
Insert
a[1] 20

a[2] 30

a[3] 45

a[4] 70

a[5] 25

a[6]

C.V.Raman College of Engineering


DELETION OPERATION
ALGORITHM: DELETE(A,N,POS,VAL)
Input: A is the linear array with N elements and POS is the
positive integer such that POS<=N
Output: This algorithm deletes the POSth element from the array
A and stores in the variable VAL
Step 1: Set VAL=A[POS]
Step 2: Set I=POS
Step 3: Repeat Step 4 to Step 5 while I<=N-1
Step 4: Set A[I]=A[I+1]
Step 5: Set I=I+1
[End of Step 2 Loop]
Step 6: Set N=N+1
Step 7: Exit
C.V.Raman College of Engineering
DELETION OPERATION

a[0] 20
53

a[1] 14
40

a[2] 50
10

a[3] 70
8

a[4] 25

a[5] 65
35

a[6]

C.V.Raman College of Engineering


DELETION OPERATION

a[0] 20
53
Insert
Delete
a[1] 14
40

a[2] 50
10 Position

a[3] 70
8

a[4] 25

a[5] 65
35

a[6]

C.V.Raman College of Engineering


DELETION OPERATION

a[0] 20
53
Insert
Delete
a[1] 14
40

a[2] 50
10 Position

a[3] 70
8

a[4] 25

a[5] 65
35

a[6]

C.V.Raman College of Engineering


DELETION OPERATION

a[0] 20
53
Insert
Delete
a[1] 14
40

a[2] 50
70 Position

a[3] 8

a[4] 25

a[5] 65
35

a[6]

C.V.Raman College of Engineering


DELETION OPERATION

a[0] 20
53
Insert
Delete
a[1] 14
40

a[2] 50
70 Position

a[3] 25
8

a[4] 25

a[5] 65
35

a[6]

C.V.Raman College of Engineering


DELETION OPERATION

a[0] 20
53
Insert
Delete
a[1] 14
40

a[2] 50
70 Position

a[3] 25
8

a[4] 25
35

a[5] 65

a[6]

C.V.Raman College of Engineering


DELETION OPERATION

a[0] 20
53

a[1] 14
40

a[2] 50
70

a[3] 25
8

a[4] 25
35

a[5] 65

a[6]

C.V.Raman College of Engineering


SORTING TECHNIQUES

Bubble Sort

Insertion Sort

Selection Sort

Quick Sort

Merge Sort

Radix Sort

Heap sort

C.V.Raman College of Engineering


BUBBLE SORT

IN BUBBLE SORT, EACH ELEMENT IS COMPARED WITH ITS ADJECENT


ELEMENT.IF 1st ELEMENT IS LARGER THAN THE 2nd ONE THE
POSITION GETS INTERCHANGED,OTHERWISE NOT.

AND NEXTELEMENT IS COMPARED WITH ITS ADJECENT ELEMENT AND


SAME PROCESS IS REPEATED

EXAMPLE

INITIAL ELEMENTS (without sorting)


11 15 2 13 6

C.V.Raman College of Engineering


EXAMPLE

ENTERED ARRAY a[0] a[1] a[2] a[3] a[4]


11 15 2 13 6

PASS 1 11 15 2 13 6

PASS 2 11 2 13 6 15

PASS 3 2 11 6 13 15

PASS 4 2 6 13 11 15

RESULT 2 6 11 13 15
ASSIGNMENTS
1.Program to take an integer array and calculate the sum and average of all elements.

2.Program to input and array and check how many even and odd numbers are
present in the array.

3.Program to input an array and display the reverse of the array.

4.Program to take input for an array and calculate the number of prime elements
present in an array.

5.Program to calculate binary equivalent of a decimal number.

6.Program to sort an array elements in an ascending order .

C.V.Raman College of Engineering


Two-Dimensional Array
• A collection of a fixed number of components arranged in
rows and columns.
• All components are in same type.
• Data is sometimes in table form (difficult to represent using a
one-dimensional array).
10 11 21 45
20 22 42 34
30 33 66 21
40 44 84 32
50 55 105 13
60 66 126 21
70 77 147 33
80 88 168 22
90 99 189 123

C.V.Raman College of Engineering


Two-Dimensional Array
 There could be situations where a table of values will have
to be stored.
Name/ Science Maths English
Marks
Ramesh 45 48 42
Gopal 48 44 44
Sita 20 14 2

 C allows us to define such tables of items by using two-


dimensional arrays.
 The two dimensional array are declared as follows
type array_name[row_size][column_size];

C.V.Raman College of Engineering


Two-Dimensional Array
int MyArray[3][3];

0,0 0,1 0,2


1,0 1,1 1,2
2,0 2,1 2,2

0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2

C.V.Raman College of Engineering


Initialization Two-Dimensional Array

int MyArray[3][3] = {0,0,0,1,1,1,2,2,2};

int MyArray[3][3] = {{0,0,0},{1,1,1},


{2,2,2}};

int MyArray[][] = {
{0,0,0},
{1,1,1},
{2,2,2}
}

C.V.Raman College of Engineering


Initialization Two-Dimensional Array

int MyArray[2][3] = {{1,1}, {2}};


In above, the remaining elements will be initialized to zero.

int MyArray[3][5] = {{0},{0},{0}};


When all the elements are to be initialized to zero, the
above statement can be used.

int MyArray[][] = {0,0};


The first two elements will be initialized explicitly while the
other elements will be initialized implicitly to zero.

C.V.Raman College of Engineering


Initialization Two-Dimensional Array

int MyArray[2][3] = {{1,1}, {2}};


The remaining elements will be initialized to zero.

int MyArray[3][5] = {{0},{0},{0}};


When all the elements are to be initialized to zero, the
above statement can be used.

int MyArray[][] = {0,0};


The first two elements will be initialized explicitly while
the other elements will be initialized implicitly to zero.

C.V.Raman College of Engineering


Initialization Two-Dimensional Array

• 2-Dimensional array can be initialized during


declaration
Eg.
int arr[ ][ ] = { {2,3,1},
{15,25,13},
{20,4,7},
{11,18,14}};

C.V.Raman College of Engineering


Initialization Two-Dimensional Array

Initialization:

for(i=0;i<row;i++)
for(j=0;j<col;j++)
matrix[i][j]=10;

C.V.Raman College of Engineering


Memory Representation of Matrix

• Matrices are stored in contigous memory


locations
• The matrices are stored in the computer
memory in two different representations:
– Row-major Order Representation
– Column-major Order Representation

C.V.Raman College of Engineering


Memory Representation of Matrix

2
• Row-major Order Representation: a[0][0]

Row 0 a[0][1] 3
– All the matrix elements 1
a[0][2]
are stored row-by-row basis. a[1][0] 15

Row 1 a[1[1] 25

a[1][2] 13

a[2][0] 20

Row 2 a[2[1] 4

a[2][2] 7

a[3][0] 21
Row 3 a[3][1] 18

a[3][2] 14
C.V.Raman College of Engineering
Memory Representation of Matrix

2
• Column-major Order Representation: a[0][0]

a[1][0] 15
– All the matrix elements Col 0
a[2][0] 20
are stored column-by-column basis. a[3][0] 21

a[0][1] 3

a[1][1] 25
Col 1
a[2][1] 4

a[3][1] 18

a[0][2] 1

a[1][2] 13
Col 2
a[2][2] 7

a[3][2] 14
C.V.Raman College of Engineering
Address Calculation of Matrix Elements in
Memory
• Row-major Order Representation:
A[r][c] is a matrix
where r=Row size(ranges from 0 to r-
1)
c=Column size(ranges from 0
to c-1)
B=base address of the array A
Address(A[i][j])=B+(i*c + j) *w
where w=size of each element

C.V.Raman College of Engineering


Address Calculation of Matrix Elements in
Memory
Ex:
A 2-D array A[15][10] is stored in the memory with each element
requiring 4 bytes of storage. If the base address of the array A is
1000,determine the location of A[12][9] when the array is stored in
row-major order.
Ans:
Base Address=1000,w=4
Address(A[12][9])=1000+(12*10+9)*4
=1000+(129*4)
=1516

C.V.Raman College of Engineering


Address Calculation of Matrix Elements in
Memory
• Column-major Order Representation:
A[r][c] is a matrix
where r=Row size(ranges from 0 to R-
1)
c=Column size(ranges from 0 to
C-1)
B=base address of the array A
Address(A[i][j])=B+(j*r + i) *w
where w=size of each element
Address Calculation of Matrix Elements in
Memory
Ex:
A 2-D array A[15][10] is stored in the memory with each element
requiring 4 bytes of storage. If the base address of the array A is
1000,determine the location of A[12][9] when the array is stored in
column-major order.
Ans:
Base Address=1000,w=4
Address(A[12][9])=1000+(9*15+12)*4
=1000+(147*4)
=1588
PROGRAMS
Program to input elements for a matrix and display the matrix elements.
#include<stdio.h>
void main( ){
int arr[2][2],i,j;
printf(“Enter the elements of the matrix:”);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf(“%d”,&a[i][j]); //reading the matrix elements
printf(“The matrix elements are:\n”);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf(“%d”,a[i]); // displaying the matrix elements
printf(“\n”); }
Output: Enter the elements of the matrix: 1 2 3 4
The matrix elements are: 12
34
ASSIGNMENTS
1.Program to take a matrix and calculate the sum of all the matrix elements.

2.Program for the addition of two matrices of order m*n and display the resultant
matrix.

3.Program for the multiplication of two matrices of order m*n and n*p and display
the resultant matrix.

4.Program to find out the transpose of a given matrix of order m*n.

5.Program to check whether a matrix is symmetric or not.

C.V.Raman College of Engineering


Multi-Dimensional Array

 C allows arrays of three or more dimensions.


The exact limit is determined by the compiler.
 The general form of a multi-dimensional array
type array_name[s1][s2][s3]…[sm];
• Where si is the size of ith dimension.
• For exmaple
int MyArray[2][3][4];

C.V.Raman College of Engineering


Multi-Dimensional Array

Int MyArray[2][3][4];

0,0 0,1 0, 2 0,3


1,0 1,1 1,2 1,3
0,0 0,1 0,2 0,3
1,0 1,1 1,2 1,3
2,0 2,1 2,2 2,3

C.V.Raman College of Engineering


TIPS & TRICKS
• Array name gives the address of the 0th element of the array.
• The array name is a constant pointer which can not be
incremented or decremented.
• int a[10],the value 10 called as the dimension of the array.
• [] tells the compiler that we are dealing with an array.
• Array index starts with 0 and ends with size-1.
• Array is also known as a subscripted variable.
• During declaration if the array is initialized, then the
leftmost dimension is optional.
• Visiting the array elements possible through subscript and
address.

C.V.Raman College of Engineering


TIPS & TRICKS
• Two dimensional array is nothing but a collection of a
number of one dimensional arrays.
• ‘\0’ is necessary in the declaration of string array.
• Array name in sizeof() operator returns the size of the array.
• Array elements can not be variables.
• Array elements can not be the address of auto and register
storage class.

C.V.Raman College of Engineering


SPARSE MATRIX

col0 col1 col2 col3 col4 col5


row0
 15 0 0 22 0  15
row1  0 11 3 0 0 0 
 
row2  0 0 0  6 0 0
 
 0 0 0 0 0 0
row3

row4  91 0 0 0 0 0
 
5*3 row5  0 0 28 0 0 0
6*6
15/15
8/36

C.V.Raman College of Engineering


SPARSE MATRIX

• Sparse matrix is a matrix that has many


elements with zero value.
• Sparse matrix can be represented using arrays
and linked lists.

C.V.Raman College of Engineering


TYPES OF SPARSE MATRICES

Lower Triangular Matrix


All the elements with a non-zero value appears
below the diagonal
In a lower triangular matrix, Ai,j=0, where i<j
1

5 3

11 7 9

2 12 10 1

C.V.Raman College of Engineering


TYPES OF SPARSE MATRICES

Upper Triangular Matrix


All the elements with a non-zero value appears
above the diagonal
In a upper triangular matrix, Ai,j=0 , where i>j
1 12 11 2

3 5 10

9 7

C.V.Raman College of Engineering


TYPES OF SPARSE MATRICES
TridiagonalMatrix
Elements with a non-zero value appears only the
diagonal or immediately above or below the
diagonal
In a upper triangular matrix, Ai,j=0 , where |i-j|>1
1 12

11 3 5

10 9 7

2 1

C.V.Raman College of Engineering


SPARSE MATRIX
• For the sparse matrix of  15 0 0 22 0  15
 0 11 3 0 0 0 
order 6*6 can be efficiently  
 0 0 0 6 0 0
stored in memory in  
3-tuples(triplet) form to  0 0 0 0 0 0
 91 0 0 0 0 0
store only non-zero  
 0 0 28 0 0 0
values to utilize the memory
space.
• The 3-tuple representation has the form
<i, j, non-zero value>

C.V.Raman College of Engineering


SPARSE MATRIX(3-tuple Form)
[0] [1] [2]
row col value

a[0] 6 6 8
[1] 0 0 15
[2] 0 3 22
[3] 0 5 -15
[4] 1 1 11
[5] 2 5 28
[6] 2 3 -6
[7] 4 0 91
[8] 5 2 28

C.V.Raman College of Engineering


Questions
1. The memory address of the first element of an array is called

a. Floor Address
b. Foundation Address
c. First Address
d. Base Address

(d) Base Address

C.V.Raman College of Engineering


Questions
2. Which of the following data structures are indexed structures?

a. Linear Arrays
b. Linked Lists
c. Both
d. None of these

a. Linear Arrays

C.V.Raman College of Engineering


Questions
4. Which of the following data structure can't store the non- homogeneous
data elements?
a. Arrays
b. Records
c. Pointers
d. None

a. Arrays

C.V.Raman College of Engineering


Questions
5. Which of the following statement is false?

a. Arrays are dense lists and static data structure


b. Data elements in linked list need not be stored in
adjacent space in memory
c. Pointers store the next data element of a list
d. Linked lists are collection of the nodes that contain
information part and next pointer

c. Pointers store the next data element of a list

C.V.Raman College of Engineering


Questions
6. Which of the following data structure is linear type?

a. Strings
b. Lists
c. Queues
d. All of above

d. All of the above

C.V.Raman College of Engineering


Questions
7.Arrays are best data structures

a) For relatively permanent collections of data


b) For the size of the structure and the data in the structure are
constantly changing
c) For both of above situation
d) For none of above situation

a) For relatively permanent collections of data

C.V.Raman College of Engineering


Questions
8. Which of the following data structure is not linear data structure?

a) Arrays
b) Linked lists
c) Both of the above
d) None of the above

d)None of the above

C.V.Raman College of Engineering


Questions
9. A list which displays the relationship of adjacency between elements is
said to be

a) Linear
b) Non linear
c) Linked list
d) Trees

a)Linear

C.V.Raman College of Engineering


Questions
10.Array is an example of -------- data structure

a)Linear data structure

b)Non linear data structure

a. Linear Data structure

C.V.Raman College of Engineering

You might also like