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

Data Structures-Arrays

Uploaded by

Jesica D'cruz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Data Structures-Arrays

Uploaded by

Jesica D'cruz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Array

Array:
Definition:
An array is defined as a collection of elements of similar data type. All the
elements of an array are stored in consecutive memory locations i.e. one
after the other or in a sequence in main memory. All the elements shares
common array name and they are accessed using index numbers.
Eg: Consider an array of Marks of 5 student as shown below

80 65 38 76 55

Marks [0] Marks [1] Marks [2] Marks [3] Marks [4]
Here, Marks is name of the array and [0], [1], [2], [3], [4] are the index
numbers.
Properties of Array:

1. Array elements are stored in contiguous memory.


2. Array name represents its base address. The base address is the
address of the first element of the array.
3. Array’s index starts with 0 and ends with N-1. Here, N stands for the
number of elements. For Example, there is an integer array of 5
elements, then it’s indexing will be 0 to 4.
4. Array elements are accessed by using an integer index.

Types of Array:
1. Single Dimensional Array( 1-D)
2. Multi-Dimensional Array (2-D OR 3-D)

1. Single Dimensional Array( 1-D):


Arrays with one set of square bracket ‘[]’ are called single
dimensional array .A single-dimensional array or 1-D array is a linear
list consisting of related and similar data items.
Declaration of Single Dimensional Array:

Syntax:
Data_type array_Name [array size] ;

Example:
int a[6];
Here, the compiler will reserve 6 locations (4*6=24 bytes) for the array a.
In each of the location we can store an integer value. The memory allocated
by the compiler is pictorially represented as:
80 65 87 76 55 65

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


Initialization of Single-dimensional Array:
Assigning the required data to a variable before processing is called
initialization.
Different ways of initializing arrays:
Array
Initialization

Initialization all Partial Array Initialization String


specified memory Initialization without size initialization
location
1. Initialization all specified memory location:
Array can be initialized at the time of declaration when their
initial values are known in advance.
Example:
int m[5]={12,32,43,54,65};
During Compilation, 5 contiguous memory locations are reserved
by the compiler for the array ‘m’ and all the locations are
initialized as shown below:

12 32 43 54 65

m[0] m[1] m[2] m[3] m[4]


2. Partial array initialization:
If the number of values to be initialized is less than the size of the
array, then the elements are initialized in the order from 0th
location. The remaining location will be initialized to zero
automatically.
Example:
int m[5]={14,23};

Here Compiler will allocate 5 memory locations and initializes


first two locations with 14 and 23.The next set of memory
locations will automatically initialize to zero by the compiler.

14 23 0 0 0

m[0] m[1] m[2] m[3] m[4]

3. Initialization without size:


While creating an array size is not mandatory but if you are
creating an array without specifying the array size then
initialization is mandatory.
Example:
int p[ ]={32,54,53,78};

Here,We have not mentioned the array size, so array size will be
the toal number of elements specified.so the array size will be set
to 4 automatically.

32 54 53 78

P[0] p[1] p[2] p[3]

4. Array initializing of a string:


In array string ends with a NULL character (‘\0’) and this NULL
character also takes one byte in the memory, so in case of array of
string size will be total character + 1 (for NULL).
Example:
char c[7]={‘s’,’a’,’h’,’y’,’o’,’g’,’\0’};
OR
char c[ ]= ”Sahyog”;
Reading & Writing Single Dimensional Array In C Language:
1. WAP in C to display elements of an array without using any loop.

OR

2. WAP in C to display elements of an array using any loop.


3. WAP to take input from the user in an array .

4. Program to Insert an element in the array in the specified location.


5. Program to delete an element from the array at specified position.
Address calculation in one-dimensional Array:
Since array elements are stored in contiguous memory locations, the
computer needs to not to know the address of every element but the
address of only first element. The address of first element is called base
address of array. Address of any other element is calculated using the
formula:-
Address (arr [k]) =base (arr) + w * k
● Where k is the index of array whose address we want to calculate
● w is the number of bytes per storage location of for one element of
array.
● And arr is name of the array which will give address of first element.
Example:-
Suppose that array “arr” is declared as integer with size 10 and its first
element is stored at address 1000. Calculate the address of 3rd element of
array when the index of the array starts from 0.
3rd element will be stored at 2nd index
Here, base address=1000, k=3, w=4.
Thus, Address(arr[2])=1000 + 4*2=1008
Address of 3rd element is i.e. 2nd index=1008
2. Multi-Dimensional Array (2-D OR 3-D):
An array with 2 set of square brackets ‘[] []’ are called 2-dimensional
array. Array with 2 or more dimensions are called multi-dimensional
array.
Declaration of Multi-Dimensional Array:
Syntax: Data_type array_name[size1][size2];
Example: int a[2][3];
Columns

0 1 2

0 a[0][0] a[0][1] a[0] [2]


Rows a[1][0] a[1][1] a[1][2]
1
(2*3)
Here, array ‘a’ is 2-dimensional array with 2 rows and 3 columns. This
declaration informs the compiler to reserve 6 locations (2*3=6)
contiguously one after the other. Total memory reserved is 6*4 (int takes 4
byte)=24 bytes.
Initialization of 2-D Array:
Assigning required value to a variable before processing is called
initialization.
Syntax:
data_type variable_name[size1][size2]={{val1,val2},{val3,val4}};
i. Initialization all specified memory location:
Example:
int first[3][4]={ {11,32,53,74},
{55,47,26,32},
{43,54,23,11}};
The declaration indicates that array ‘first’ has 3 rows and 4
columns. The pictorial representation of this 2-D array is:
0 1 2 3
0
11 32 53 74
1
2 55 47 26 32
43 54 23 11

ii. Partial array Initialization:


If the number of values to be initialized is less than the size of the
array, then the elements are initialized from left to right one after
the other and the remaining locations will be initialized to zero
automatically.
Example:
int first[3][4]={ {11,32 },
{55,47 },
{43,54}};
The declaration indicates that array ‘first’ has 3 rows and 4
columns. The pictorial representation is of this partial array is
shown below:
0 1 2 3
0 11 32 0 0
1 55 47 0 0
2
43 54 0 0
iii. Initialization without size:
Example:
int first[][3]={ {11,32,32 },{3,4,6}};

In 2-d array size of row is not mandatory but size of column is


mandatory.

Reading & Writing Multi Dimensional Array:


1. WAP to display element of 2-D array without using loop:

2. Program for Multiplication table.


Manipulation in two-dimensional arrays:
Let us see how to manipulate two-dimensional arrays by taking various
examples such as:
● Add two matrices and store the result in third matrix.
● Subtract a matrix from other matrix and store the result in third
matrix.
● Sum of elements of a given matrix
● Average of all the elements of a given matrix
● Largest element in a given matrix
● Transpose of a matrix
● Trace of a matrix

1. Biggest element in a given matrix( Try smallest element program)


2. Sum and avg of arrays

3. Transpose of matrix:
Transpose of a matrix is obtained by changing rows to columns and
columns to rows.
Program to get transpose matrix:
4. Trace of matrix:
The trace of a matrix is the sum of the elements of diagonal in a given
matrix. For example, consider the matrix shown b6666666elow:
Size of matrix is 3 × 3
10 15 20
25 30 35
40 45 50
Trace=10+30+50=90
If we add the diagonal elements 10, 30 and 50, we get 90.So, the trace of
given matrix is 90.
Note: Since diagonal elements exist only for a square matrix, the size of the
matrix should be n x n i.e. size of row and column should be equal. For a
rectangular matrix trace cannot be computed.
SPARSE MATRICES:
Sparse Array:
● A sparse array is an array in which majority of the array elements
are zero.
● If half of the array element or more than half element is zero then it
would be a sparse array.
Dense Array:
● A Dense array is an array in which majority of the array elements
are non-zero.
1 0 3
0 0 4
0 6 0
1 2 0
4 11 5
0 15 0
0 2 0
3 0 5
0 0 1

It’s a sparse array It’s a Dense sparse array It’s a sparse array

You might also like