Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 11

Name: Karthick Seshadri

RC Code: 1173
(Thiagarajar College Of
Engineering,Madurai)

Arrays
Slides for the
alternate_assignment_part3_I
ndian_language

What is an Array?
A data structure to hold a collection of
homogeneous items.
An array has a name and is
characterized by a size or capacity.
Arrays are basically structures that can
hold multiple values under the same
name for ease of access and processing.
An array can be constructed out of any
data type like int, char, float, double and
so on.
3

Memory Layout
The elements of an array are
contiguously stored in memory.
The address of the first element in an
array is called the Base Address.
Typically Arrays are indexed from 0.
10

20

30

40

50

4
4

Array creation
<data type> <Name of the array>
[size]
For e.g: int arr[20];
Creates an array large enough to hold 20
integers.

Typically static in nature


5

Array Indexing
arr[2]
Refers to the third element at the index
2.
[] -> subscript operator.

Address of the 3rd element = Base


Address + index of the 3rd element *
size of an integer
6

Reading Arrays in Programs


int arr[10];
int i=0;
for(i=0;i<10;i++) {
scanf(%d,&arr[i]);
}

Using Arrays to find out the


sum of a set of integers
int arr[] = {10, 20, 30, 40, 50};
int i=0, sum=0;
for(i=0;i<4;i++) {
sum += arr[i];
}
printf(%d, sum);

2D - Arrays
Can be used to store a table of items
Row/Colum
n Index

10

20

30

40

50

60

70

80

90

Indexed using two subscripts arr[i][j]

2D-Arrays - Memory Layout


Row major
10

20

30

40

50

60

70

80

90

50

80

30

60

90

Column major
10

40

70

20

10

Arrays - Applications
Implementing other data structures
Stacks, queues, heaps, trees and so on.

String Manipulation
Manipulating a set of records
Binary Search
11

You might also like