IP
ARRAYS
Er. GAGANDEEP KAUR
ASSISTANT PROFESSOR CSE
CEC- CGC University, Mohali
ARRAY
If you want to store 100 integers, you’d have to declare 100 variables: int a1, a2, a3, ... a100; This is impractical,
hard to
Manage and inefficient.
An array groups multiple values under a single name: int arr[100]; Now you can access each value using an
index: arr[0], arr[1], arr[2], ..., arr[99];
Advantages of Arrays
1. Efficient storage: Stores multiple values in continuous memory locations.
2. Easy access: Access any element using an index.
3. Code simplicity: Instead of writing multiple variables, you use loops:
for (int i = 0; i < 100; i++) {
arr[i] = i + 1; }
4. Useful for algorithms: Sorting, searching, and data manipulation become possible.
5. Foundation for advanced concepts: Arrays are used in strings, matrices, data structures (like stacks, queues).
2 ARRAYS 20XX
ARRAY
• An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used
to store the collection of primitive data types such as int, char, float, etc., and also derived and user-defined data
types such as pointers, structures, etc. Arrays are static in nature, i.e., they are allocated memory at the compile
time.
3 ARRAYS 20XX
ARRAY DECLARATION
• In C, we have to declare the array like any other variable before using it. We can declare an array by specifying its
name, the type of its elements, and the size of its dimensions. When we declare an array in C, the compiler
allocates the memory block of the specified size to the array name.
• Syntax:
data_type array_name [size];
or
data_type array_name [size1] [size2]...[sizeN];
where N is the number of dimensions.
• For Example:- Int a[5];
4 ARRAYS 20XX
ARRAY INITIALIZATION
• Initialization in C is the process to assign some initial value to the variable. When the array is declared or allocated
memory, the elements of the array contain some garbage value. So, we need to initialize the array to some
meaningful value. There are multiple ways in which we can initialize an array in C.
Array Initialization with Declaration & size.
Array Initialization with Declaration without Size.
Array Initialization after Declaration (Using Loops).
5 ARRAYS 20XX
ARRAY INITIALIZATION WITH DECLARATION
& SIZE
In this method, we initialize the array along with its declaration. We use an initializer list to initialize multiple
elements of the array. An initializer list is the list of values enclosed within braces { } separated by a comma.
Syntax:- data_type array_name [size] = {value1, value2, ... valueN};
Example:- int a[3] = {2,4,6};
6 ARRAYS 20XX
ARRAY INITIALIZATION WITH DECLARATION
BUT WITHOUT SIZE
If we initialize an array using an initializer list, we can skip declaring the size of the array as the compiler can
automatically deduce the size of the array in these cases. The size of the array in these cases is equal to the number of
elements present in the initializer list as the compiler can automatically deduce the size of the array.
Syntax:- data_type array_name[] = {1,2,3,4,5};
Example:- int a[] = {2,4,6};
7 ARRAYS 20XX
ARRAY INITIALIZATION USING LOOPS
We initialize the array after the declaration by assigning the initial value to each element individually. We can use for
loop, while loop, or do-while loop to assign the value to each element of the array.
Syntax:- for (int i = 0; i < N; i++) {
array_name[i] = valuei;
}
Example:- for (int i = 0; i < 5; i++) {
a[i] = valuei;
}
8 ARRAYS 20XX
// C Program to demonstrate array initialization
#include <stdio.h>
int main()
{
// array initialization using initializer list
int arr[5] = { 10, 20, 30, 40, 50 };
// array initialization using initializer list without
specifying size
int arr1[] = { 1, 2, 3, 4, 5 };
// array initialization using for loop
float arr2[5];
for (int i = 0; i < 5; i++) {
arr2[i] = (float)i * 2.1;
}
return 0;
}
9 ARRAY 20XX
ACCESS ARRAY ELEMENTS
We can access any element of an array in C using the array subscript operator [ ] and the index value i of the element.
Syntax:- array_name [index];
For example:- a[0];
One thing to note is that the indexing in the array always starts with 0, i.e., the first element is at index 0 and the last
element is at N – 1 where N is the number of elements in the array.
10 ARRAYS 20XX
// C Program to illustrate element access using array subscript
#include <stdio.h>
int main()
{
int a[] = { 15, 25, 35, 45, 55 }; // array declaration and initialization
printf("Element at arr[2]: %d\n", a[2]); // accessing element at index 2 i.e 3rd element
printf("Element at arr[4]: %d\n", a[4]); // accessing element at index 4 i.e last element
printf("Element at arr[0]: %d", a[0]); // accessing element at index 0 i.e first element
return 0;
}
OUTPUT
Element at arr[2]: 35
Element at arr[4]: 55
Element at arr[0]: 15
11 ARRAY 20XX
12 Presentation title 20XX
TYPES OF ARRAY
• 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
1. One Dimensional Array in C:- The One-dimensional arrays, also known as 1-D arrays in C are those arrays that
have only one dimension.
• Syntax:- array_name [size];
• Example:- a[8];
13 ARRAYS 20XX
MULTIDIMENSIONAL ARRAY- 2D ARRAY
• 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.
A. 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:- datatype array_name[size1] [size2];
int a[2][3];
Here, size1: Size of the first dimension(rows).
size2: Size of the second dimension(columns).
14 ARRAYS 20XX
// C Program to illustrate 2d array
#include <stdio.h>
int main()
{
int arr[2][3] = { 10, 20, 30, 40, 50, 60 }; // declaring and initializing 2d array
printf("2D Array:\n");
// printing 2d array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT
2D Array:
10 20 30
40 50 60
15 ARRAY 20XX
16 Presentation title 20XX
17 20XX
18 20XX
19 20XX
20 20XX
21 20XX
22 20XX
23 20XX
24 20XX
25 20XX