0% found this document useful (0 votes)
66 views20 pages

5 - Arrays

The document discusses different types of arrays in programming languages. It defines an array as a fixed size collection of elements of the same data type. The key points covered are: - One-dimensional arrays store data in a single list and can be accessed using indices. Two-dimensional and multi-dimensional arrays extend this to multiple lists organized in rows and columns. - Arrays are declared with a type, name, and size and can be initialized with initial values. The size determines how much memory is allocated. - Dynamic arrays are created at runtime using memory allocation functions, allowing flexible sizing compared to static arrays which are fixed at compile time.

Uploaded by

Neeru Redhu
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)
66 views20 pages

5 - Arrays

The document discusses different types of arrays in programming languages. It defines an array as a fixed size collection of elements of the same data type. The key points covered are: - One-dimensional arrays store data in a single list and can be accessed using indices. Two-dimensional and multi-dimensional arrays extend this to multiple lists organized in rows and columns. - Arrays are declared with a type, name, and size and can be initialized with initial values. The size determines how much memory is allocated. - Dynamic arrays are created at runtime using memory allocation functions, allowing flexible sizing compared to static arrays which are fixed at compile time.

Uploaded by

Neeru Redhu
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/ 20

ARRAYS

Programming languages for Bioinformatics


BIF507

By Neeru Redhu
CCS HAU
Derived data type

An array is a fixed size sequenced collection of


elements of the same data type.

e.g. roll no. of students, salaries of employees


Arrays can hold multiple values.
int count Enough memory for 1 int

12345

float price Enough memory for 1 float

56.981

char letter Enough memory for 1 char

A
Arrays
Data structures
Related data items of same type
Remain same size once created
Fixed-length entries
ARRAY - MEMORY LAYOUT
The definition:
int tests[5];
allocates the following memory:

first second third fourth fifth


element element element element element
Array Number of Size of each Size of the
Declaration elements element array
char letter[25] 25 1 bytes 25 bytes
short rings[100] 100 2 bytes 200 bytes

int miles[84] 84 4 bytes 336 bytes

float temp[12] 12 4 bytes 48 bytes


ARRAY TERMINOLOGY
In the definition int tests[5];

int is the data type of the array elements

tests is the name of the array

5, in [5], is the size declarator. It shows the number


of elements in the array.

The size of an array is (number of elements) * (size of


each element)
ARRAY TERMINOLOGY

The size of an array is:


the total number of bytes allocated for it
(number of elements) * (number of bytes for each element)

Examples:
int tests[5] is an array of 20 bytes, assuming 4 bytes for
an int
long double measures[10]is an array of 80 bytes,
assuming 8 bytes for a long double
SIZE DECLARATORS
Named constants are commonly used as size
declarators.

const int SIZE = 5;


int tests[SIZE];
This eases program maintenance when the size of
the array needs to be changed.
One-Dimensional Arrays
Declaration of One-Dimensional Arrays
Initialization of One Dimensional Arrays

Two- Dimensional Arrays


Initialization of Two- Dimensional Arrays

Multi- Dimensional Arrays


Dynamic Arrays
ARRAYS
1-dimensional array.

Variable First Sec Third Fourth Fifth Sixth


value instance instance instance instance instance instance
x 5.15 6.2 0.65 3.025 15.5 162.0
x [0] x [0] 5.15

x [1] x [1] 6.2

x [2] x [2] 0.65

x [3] x [3] 3.025

x [4] x [4] 15.5

x [5] x [5] 162.0


a = x[1] + 2;

x[2] = x[1] + x[6];

x [6] = y[1] + z[9];

x[3] = x[1] * 6;
Declaration of One-Dimensional Arrays
DataType variable-name [size];
float height[50];

Initialization of One Dimensional Arrays


DataType variable-name [size]= {list of variables};
float height[50] = {0.0, 15.7, 33.2};
float height [ ] = {0.0, 15.7, 33.2};
ARRAYS
An array is a collection of data elements that are of
the same type (e.g., a collection of integers,
collection of characters, collection of doubles).

Variable First Sec Third Fourth Fifth Sixth


value instance instance instance instance instance instance
x 5.15 6.2 0.65 3.025 15.5 162.0
Y 5.5364 15 154. 6356 .48 2.2
Z 3 6 49 2465 13 458
W 1154 35 458 3 61 48
Declaration of Two-Dimensional Arrays
Type variable-name [row_size][column_size];
float height[50][10];

Initialization of TwoDimensional Arrays


Type variable-name [size]= {list of variables};
float height[50] = {{0.0, 15.7, 33.2}, {1.2, 56.5, 23.5}};
Variable First Sec Third Fourth Fifth Sixth
value instance instance instance instance instance instance
x 5.15 6.2 0.65 3.025 15.5 162.0
Y 5.5364 15 154. 6356 .48 2.2
1 454 48 245 78 415
Z 3 6 49 2465 13 458
3 124 6 45 478428 1 25 6.4785 63.5
W 1154 35 458 3 61 48
15 23 .3565
6 41.5
478 64 1 .1354
6.4 454.
63.
First time
4485
5 4 .14 25 4.5 48.61 4 .17 16 44.
Second time
14 4 25 48. 84 16
Third time
Declaration of multi-Dimensional Arrays
Type variable-name [s1][s2][s3]....[sm];
float height[50][10][6];

Initialization of multi Dimensional Arrays


Type variable-name [size]= {list of variables};
DYNAMIC ARRAYS
Memory allocation
Compile time (static memory allocation)
Run time (dynamic memory allocation)

Arrays created at runtime are known as dynamic


arrays
Created using pointer variables and memory management
functions like malloc, calloc and realloc (from <stdlib.h>)
Used in creation of linked lists, stacks and queues.
END

You might also like