ARRAY
Md Shafiuzzaman
Lecturer
Dept. of CSE, NBIU
WHAT IS ARRAY?
• An array is defined as the collection of similar type of data items stored at
contiguous memory locations.
• Arrays are the derived data type in C programming language which can store
the primitive type of data such as int, char, double, float, etc.
• It also has the capability to store the collection of derived data types, such as
pointers, structure, etc.
• The array is the simplest data structure where each data element can be
randomly accessed by using its index number.
WHAT IS ARRAY?
• Explanation: The index is starting from 0, which stores value. we can
also store a fixed number of values in an array. Array index is to be
increased by 1 in sequence whenever its not reach the array size.
PROPERTIES OF ARRAY
1. The array contains the following properties.
2. Each element of an array is of same data type and carries the same size, i.e., int = 4
bytes.
3. Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
4. Elements of the array can be randomly accessed since we can calculate the
address of each element of the array with the given base address and the size of
the data element.
TYPES OF ARRAY
Types of Array in C
There are two types of arrays based on the number of dimensions it has. They
are as follows:
1. One Dimensional Arrays (1D Array)
2. Multidimensional Arrays
1D ARRAY
1D ARRAY
• #include <stdio.h>
• int main()
•{
int arr[5] = { 15, 25, 35, 45, 55 };
printf("Element at arr[2]: %d\n", arr[2]);
• printf("Element at arr[4]: %d\n", arr[4]);
• printf("Element at arr[0]: %d", arr[0]);
• return 0;
•}
1D ARRAY
#include <stdio.h>
int main()
{
int arr[5];
for (int i = 0; i < 5; i++)
{
scanf(“%d”,&arr[i]);
}
printf("Elements of Array: ");
for (int i = 0; i < 5; i++) {
printf("%d \n", arr[i]);
}
return 0;
}
MULTIDIMENSIONAL ARRAY IN C
Multi-dimensional Arrays in C are those arrays that have more than one dimension.
Some of the popular multidimensional arrays are 2D arrays and 3D arrays. We can
declare arrays with more dimensions than 3d arrays but they are avoided as they get
very complex and occupy a large amount of space.
Two-Dimensional Array in C
A Two-Dimensional array or 2D array in C is an array that has exactly two
dimensions. They can be visualized in the form of rows and columns organized in a
two-dimensional plane.
Syntax of 2D Array in C
Here,
size1: Size of the first dimension.
array_name[size1] [size2]; size2: Size of the second dimension.
2D ARRAY IN C
#include <stdio.h>
int main()
{
int arr[2][3] = { 10, 20, 30, 40, 50, 60 };
printf("2D Array:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
ADVANTAGE & DISADVANTAGES
Advantages:
1. Code Optimization: Less code to the access the data.
2. Ease of traversing: By using the for loop, we can retrieve the elements of an array
easily.
3. Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4. Random Access: We can access any element randomly using the array.
Disadvantage:
Fixed Size: Whatever size, we define at the time of declaration of the array, we can't
exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will
learn later.
DECLARATION OF ARRAY
Declaration of C Array
We can declare an array in the c language in the following way.
data_type array_name[array_size];
Example: int a[10] , here a is a integer type array with size 10.
Declaration with Initialization
We can initialize the c array at the time of declaration. Let's see the code.
int marks[5]={20,30,40,50,60};