- Whenever we want to work with large number of data values, we need to use that much number of different variables.
 
- As the number of variables are increasing, complexity of the program also increases and programmers get confused with the variable names.
 
To understand the concept of arrays, consider the following example declaration.
int a, b, c;
Here, the compiler allocates 2 bytes of memory with name ‘a’, another 2 bytes of memory with name ‘b’ and more 2 bytes with name ‘c’.
These three memory locations are may be in sequence or may not be in sequence.
Array:
There may be situations in which we need to work with large number of similar data values.
To make this work more easy,
C/C++ programming language provides a concept called “Array”.
Python: list
Python: list
An array is a variable which can store multiple values of same data type at a time.
Definition
An array is a collection of elements of the same data type stored in contiguous memory locations.
Array indices start from 0.
Types of Arrays
One-Dimensional (1D) – int a[5];
Two-Dimensional (2D) – int a[3][3];
Syntax
data_type array_name[size];
Example:
int num[5];
Initialization
Or
Accessing Elements
Array with Loops
for (int i = 0; i < 5; i++)
   printf("%d ", num[i]);
Memory Representation:
Elements are stored sequentially in memory.array[i] address = base_address + (i * sizeof(data_type))
Array with Loops
for (int i = 0; i < 5; i++)
   printf("%d ", num[i]);
Memory Representation:
Elements are stored sequentially in memory.array[i] address = base_address + (i * sizeof(data_type))2D Array Example

0 Comments