0% found this document useful (0 votes)
7 views27 pages

Array Data Structure

An array is a fixed-size, sequenced collection of elements of the same data type, allowing for indexed access and storage of homogenous values. Arrays can be one-dimensional or multidimensional, and operations such as traversal, insertion, deletion, searching, and updating can be performed on them. Memory address calculations for arrays can be done using specific formulas depending on whether the array is one-dimensional or multidimensional.
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)
7 views27 pages

Array Data Structure

An array is a fixed-size, sequenced collection of elements of the same data type, allowing for indexed access and storage of homogenous values. Arrays can be one-dimensional or multidimensional, and operations such as traversal, insertion, deletion, searching, and updating can be performed on them. Memory address calculations for arrays can be done using specific formulas depending on whether the array is one-dimensional or multidimensional.
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

Array

What is array?
array is a fixed-size, sequenced collection of
elements of the same data type.
array is a sequence of data items that
are of the same type, that are indexible,
and that are stored contiguously.
Arrays are data type that is used to
represent a large number of homogenous
values.
array is a sequenced collection, we can
refer to the elements in the array as the
first element, the second element, and so
forth until we get to the last element.
Arrays are data type that is used to
represent a large number of homogenous
values.
use loops to read and write the elements
in an array.

use loops to add, subtract, multiply, and


divide the elements.
use loops for more complex processing
such as calculating averages.
The General Form – One-dimensional array

storage class data-type arrayname[expression];


Example:
int ccmit[5];
char bscs[30];

float bsit[20];
double ITCS[12];
Array with Initialization

Consider the following array definition:


int x[4] = {1, 2, 3};
float y[5] = {1.0, 1.25, 1.5};
The results on an element by element basis are:
x[0] = 1 y[0] = 1.0
x[1] = 2 y[1] = 1.25
x[2] = 3 y[2] = 1.5
x[3] = 0 y[3] = 0
y[4] = 0
char college[6] = “CCMIT”;

college[0] = ‘C’;
college[1] = ‘C’;
college[2] = ‘M’;
college[3] = ‘I’;
college[4] = ‘T’;
college[5] = ‘\0’;
Default values for an Array
Data Type Default Value
bool false
char 0
int 0
float 0.0
double 0.0f
void
wchar_t 0
Accessing Elements in Arrays

for(i=0; i<10; i++)


scores[i]…..;
Inputting Values

int scores[10];
int i; …
for(i=0; i<10; i++)
scanf(“%d”, &scores[i]);
Inputting Values

for(i=0 i<10; i++)


scores[i] = i * 2;
Sample program 1:
This is a program that sorts the values of the array num.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num[3] = {5, 3, 7};
int h, i, temp;

for(h=0; h<3; ++h)


for(i=0; i<h; ++i)
{
if(num[i] > num[I + 1])
{
temp = num[i];
num[i] = num[I + 1];
num[i+ 1] = temp;
}
}
for(i=0; i<3; i++)
printf(“%d\n”, num[i]);
getch();
}

sample run:

Output:
3
5
7
Memory Address Calculation in an Array
It is easier to calculate the position of each element by simply adding an offset to a base
value (the base value here is the memory location of the first element of the array denoted by
the name of the array).

array name is X

X base address is 200


Address Calculation in Single (One) Dimension Array
Example:
Given the base address of an array CS[1300.....1900]
as 1020 and size of each element is 2 bytes in the
memory. Find the address of CS[1700].

Solution: The given values are: B = 1020, LB = 1300, W = 2,


X = 1700
Address of CS [ 1700 ] = B + W * ( X – LB )
= 1020 + 2 * (1700 – 1300) = 1020 + 2 * 400
= 1020 + 800
= 1820 [Ans]

Address of A[X] = B + W * (X – LB) Solution: The given values are: B = 1100, LB = 0,


Where: W = 4, X = 2
B = base address Address of A [ 2 ] = B + W * ( X – LB )
W = storage size of one element stored in the array (in bytes) = 1100 + 4 * (2 – 0) = 1100 + 4 * 2
X = subscript of element whose address is to be found = 1100 + 8
LB = Lower limit/Lower bound of subscript, if not specified = 1108 [Ans]
assume 0 (zero)
Multidimensional
Arrays
• Multidimensional arrays are defined in much the same manner as
one-dimensional
• A two-dimensional array requires two pairs of square brackets.
float IT[5][3];
int CS[3][3];
int ccmit[3][3] ={1,2,3,4,5,6,7,8,9};

ccmit[0][0] = 1 ccmit[0][1] = 2 ccmit[0][2] = 3


ccmit[1][0] = 4 ccmit[1][1] = 5 ccmit[1][2] = 6
ccmit[2][0] = 7 ccmit[2][1] = 8 ccmit[2][2] = 9
column
ccmit [3] [3]
0 1 2
row 0 1 2 3
1 4 5 6
2 7 8 9
example: declarations
int x[3][4]={{2,4,6,8},{10,12,1,3},{5,7,9,11}};
int x[3][4]={{2,4,6,},{10,12,1,3},{5,7}};
error
int x[3][4]={{2,4,6,8},{10,12,1,3,14},{5,7,9,11}};
int x[3][4]={{2,4,6,8},{10,12,1,3},{8,1,3,4},{5,7,9,11}};
int x[3][4]={2,4,6,8,10,12,1,3,5,7,9,11};
int a;
int b;
for (a=0;a<3;a++)
for(b=0;b<4;b++)
printf(“%d\t”,x[a][b]);
referencing:
int x[3][4]={{2,4,6,8},{10,12,1,3},{5,7,9,11}};
printf(“%d”, x[1][2]);
scanf(“%f”, &x[0]0]);
x[2][0] = x[3][2] * x[1][3];
If(x[1][4]>=7)
While( 9 == x[a][b])
Address Calculation in Two Dimension Array
Row Major System: The address of a location in Row
Major System is calculated using the following formula:
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
Column Major System: The address of a location in
Column Major System is calculated using the following
formula:
Address of A [ I ][ J ] = B + W * [( I – Lr ) + M * ( J – Lc )]
Where:
B = Base address
I = Row subscript of element whose address is to be found
J = Column subscript of element whose address is to be found
W = Storage Size of one element stored in the array (in byte)
Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
Lc = Lower limit of column/start column index of matrix, if not given assume 0
M = Number of rows of the given matrix
N = Number of columns of the given matrix
A[Lr- – – – – Ur, Lc- – – – – Uc]
Number of rows (M) will be calculated as = (Ur – Lr) + 1
Number of columns (N) will be calculated as = (Uc – Lc) + 1
Example:
An array X [-15..........10, 15...............40] requires one byteWhere:
of storage.
If beginning location is 1500, B = Base address
I = Row subscript of element whose address is to be found
Determine the location of x[15][20]. J = Column subscript of element whose address is to be found
Solution: W = Storage Size of one element stored in the array (in byte)
Number of rows say M = (Ur – Lr) + 1 = [10 – (- 15)] +1 = 26 Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
Number of columns say N = (Uc – Lc) + 1 = [40 – 15)] +1 = 26 Lc = Lower limit of column/start column index of matrix, if not given assume 0
M = Number of rows of the given matrix
Column Major Wise Calculation of above equation N = Number of columns of the given matrix
The given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, M = 26
Address of A [ I ][ J ] = B + W * [ ( I – Lr ) + M * ( J – Lc ) ]
= 1500 + 1 * [(15 – (-15)) + 26 * (20 – 15)] = 1500 + 1 * [30 + 26 * 5]
= 1500 + 1 * [160]
= 1660 [Ans]
Row Major Wise Calculation of above equation
The given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, N = 26
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
= 1500 + 1* [26 * (15 – (-15))) + (20 – 15)] = 1500 + 1 * [26 * 30 + 5]
= 1500 + 1 * [780 + 5]
= 1500 + 785
= 2285 [Ans]
Basic Array Operations
[Link]. This operation traverses through the elements of an array
meaning we print all the array elements one by one as we visit them.
[Link]. Insert operation is to insert one or more data elements into an
array. Based on the requirement, a new element can be added at the
beginning, end, or any given index of the array.
3. Deletion. Deletion refers to removing an existing element from the array
and re- organizing all elements of an array.
4. Search. With this operation, you can search for an item in an array based
on a given value (or through an index).
5. Update. Update operation refers to updating an existing element from the
array at a given index. This operation is quite similar to the insert method,
except that it will replace the existing value at the given index.

You might also like