By
Dr. Kanchan V. Shende
Module 3 : Arrays in C
Contents:
Introduction to 1-Dimensional (1-D) Array
– Definition
– Declaration, initialization Accessing and displaying 1- D array elements
Introduction to 2-Dimensional (2-D) Array
– Definition
– Declaration, initialization Accessing and displaying 2- D array elements
Multidimensional Arrays
02.11.23 Module 3 2
Array
Array in C is one of the most used data structures in C programming.
It is a simple and fast way of storing multiple values under a single name.
What is Array in C?
• 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.
02.11.23 Module 3 3
C 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 of Array Declaration
data_type array_name [size];
or
data_type arrray_name [size1] [size1]........[sizeN]
02.11.23 Module 3 4
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.
02.11.23 Module 3 5
1. Array Initialization with Declaration
◦ In this method, initialize the array along with its declaration.
◦ 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.
data_type array_name [size] = {value1, value2, ........., valueN}
02.11.23 Module 3 6
2. Array Initialization with Declaration 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.
data_type array_name[] = {1,2,3,4,5};
02.11.23 Module 3 7
3. Array Initialization after Declaration (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.
for (int i = 0; i < N; i++)
{
array_name[i] = valuei;
}
02.11.23 Module 3 8
Conti....
Example of Array Initialization in C
// C Program to demonstrate array initialization
#include <stdio.h>
int main()
// array initialization using initialier 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;
02.11.23 Module 3 9
One-Dimensional Array
• Arrays are a fundamental concept in programming, and they come in different
dimensions.
• One-dimensional arrays, also known as single arrays, are arrays with only one
dimension or a single row
Syntax of One-Dimensional Array in C
dataType arrayName[arraySize];
dataType specifies the data type of the array. It can be any valid data type in C
programming language, such as int, float, char, double, etc.
arrayName is the name of the array, which is used to refer to the array in the program.
arraySize specifies the number of elements in the array. It must be a positive integer
value.
–
02.11.23 Module 3 10
Rules for Declaring One Dimensional Array in C
• Before using and accessing, we must declare the array variable.
• In an array, indexing starts from 0 and ends at size-1. For example, if we have arr[10] of
size 10, then the indexing of elements ranges from 0 to 9.
• We must include data type and variable name while declaring one-dimensional arrays in C.
• We can initialize them explicitly when the declaration specifies array size within square
brackets is not necessary.
• Each element of the array is stored at a contiguous memory location with a unique index
number for accessing.
02.11.23 Module 3 11
Example of One-Dimensional Array in C
Output:
Example 1:
numbers[0] = 10
numbers[1] = 20
#include <stdio.h> numbers[2] = 30
numbers[3] = 40
numbers[4] = 50
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
for(int i=0; i<5; i++)
Explanation:
•In the above example, declared a one-
{ dimensional array of integers named
numbers. The array contains five
elements, and each element is initialized
printf("numbers[%d] = %d\n", i, numbers[i]); with a value.
} • used a for loop to iterate over the
elements of the array and print their
values using the printf function.
return 0;
02.11.23 Module 3 12
Accessing Elements of One-Dimensional Array in C
• In a one-dimensional array, each element is identified by its index or
position in the array.
• The index of the first element in the array is 0, and the index of the last
element is arraySize - 1.
• To access an element of a one-dimensional array in C programming
language, use the following syntax:
arrayName[index]
• arrayName is the name of the array.
• index is the index of the element we want to access.
02.11.23 Module 3 13
Example of Accessing Elements of One-Dimensional Array
in C
#include <stdio.h>
int main()
int numbers[5] = {10, 20, 30, 40, 50};
printf("The first element of the array is: %d\n", numbers[0]);
printf("The third element of the array is: %d\n", numbers[2]);
return 0;
}
Output:
The first element of the array is: 10
The third element of the array is: 30
02.11.23 Module 3 14
Modifying Elements of One-Dimensional Arrays
• We can modify the value of individual elements of a one-dimensional array using their index. To
modify an element, we simply assign a new value to it using the assignment operator =.
Example of Modifying Elements of One-Dimensional Array in C
#include <stdio.h>
int main()
int numbers[5] = {10, 20, 30, 40, 50};
printf("The third element of the array is %d\n", numbers[2]);
numbers[2] = 35;
printf("The third element of the array is now %d\n", numbers[2]); Output:
The third element of the array is 30
return 0; The third element of the array is now
35
}
02.11.23 Module 3 15
Initializing One Dimensional Array in C
• In C programming language, we can initialize a one-dimensional array
while declaring it or later in the program. We can initialize a one-
dimensional array while declaring it by using the following syntax:
dataType arrayName[arraySize] = {element1, element2, ..., elementN};
"dataType' specifies the data type of the array.
"arrayName' is the name of the array.
"arraySize' specifies the number of elements in the array.
"{element1, element2, ..., elementN}' specifies the values of the elements in
the array. The number of elements must be equal to "arraySize'.
02.11.23 Module 3 16
Example of Initializing One Dimensional Array in C
Example 1:
#include <stdio.h>
Output:
int main() { numbers[0] = 10
numbers[1] = 20
int numbers[5] = {10, 20, 30, 40, 50}; numbers[2] = 30
numbers[3] = 40
numbers[4] = 50
for(int i=0; i<5; i++) {
printf("numbers[%d] = %d\n", i, numbers[i]);
return 0;
02.11.23 Module 3 17
• We can also initialize a one-dimensional array later in the program by
assigning values to its elements using the following syntax:
arrayName[index] = value;
arrayName is the name of the array.
index is the index of the element we want to assign a value to.
value is the value we want to assign to the element.
02.11.23 Module 3 18
Example 2:
#include <stdio.h>
int main() {
int numbers[5]; Output:
numbers[0] = 10
numbers[0] = 10; numbers[1] = 20
numbers[2] = 30
numbers[1] = 20; numbers[3] = 40
numbers[4] = 50
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
for(int i=0; i<5; i++) {
printf("numbers[%d] = %d\n", i, numbers[i]);
return 0;
}
02.11.23 Module 3 19
Advantages of One-Dimensional Array
• Organization of data: Arrays help in organizing data into a fixed-size,
ordered container. This simplifies data storage and retrieval while making
code more readable and comprehensible.
• Managing large data sets: Arrays can store a large number of elements
in a single variable, facilitating the processing and computation of large
datasets without declaring multiple variables.
• Memory efficiency: Arrays allow programmers to allocate blocks of
memory efficiently. The elements in the array are stored in adjoining
memory locations, making it easy to access and process data in real-time.
• Time Complexity: Using arrays can help optimize algorithms and reduce
time complexity due to their precise indexing and simplified data access
02.11.23 Module 3 20
Two Dimensional Array in C
• The Two Dimensional (2D) array is organized as matrices which can be
represented as the collection of rows and columns.
• It provides ease of holding the bulk of data at once which can be passed to
any number of functions wherever required.
Declaration of two dimensional Array in C
The syntax to declare the 2D array is given below.
data_type array_name[rows][columns];
eg. int twodimen[4][3];
02.11.23 Module 3 21
• The simplest form of multidimensional array is the two-dimensional array.
• A two-dimensional array is, in essence, a list of one-dimensional arrays. To
declare a two-dimensional integer array of size [x][y],
you would write something as follows −
type arrayName [ x ][ y ];
• A two-dimensional array can be considered as a table which will have x
number of rows and y number of columns. A two-dimensional array a, which
contains three rows and four columns can be shown as follows −
02.11.23 Module 3 22
Two-dimensional array example in C
#include<stdio.h>
Output:
int main(){
int i=0,j=0; arr[0][0] = 1
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5}, arr[0][1] = 2
{4,5,6}}; arr[0][2] = 3
arr[1][0] = 2
//traversing 2D array
arr[1][1] = 3
for(i=0;i<4;i++){ arr[1][2] = 4
for(j=0;j<3;j++){ arr[2][0] = 3
arr[2][1] = 4
printf("arr[%d] [%d] = %d \n",i,j,arr[i]
arr[2][2] = 5
[j]);
arr[3][0] = 4
}//end of j arr[3][1] = 5
}//end of i arr[3][2] = 6
return 0;
}
02.11.23 Module 3 23
2D array example: Storing elements in a matrix and
printing it.
#include <stdio.h>
printf("\n printing the elements ....\
void main () n");
{ for(i=0;i<3;i++)
int arr[3][3],i,j; {
for (i=0;i<3;i++)
printf("\n");
{
for (j=0;j<3;j++)
for (j=0;j<3;j++)
{
{
printf("%d\t",arr[i][j]);
printf("Enter a[%d]
[%d]: ",i,j); }
scanf("%d",&arr[i][j]); }
} }
}
02.11.23 Module 3 24
Enter a[0][0]: 56 Enter a[2][0]: 45
Enter a[2][1]: 56
Enter a[0][1]: 10 Enter a[2][2]: 78
printing the elements ....
Enter a[0][2]: 30
56 10 30
Enter a[1][0]: 34 34 21 34
45 56 78
Enter a[1][1]: 21
Enter a[1][2]: 34
02.11.23 Module 3 25
Initialization of 2D Array in C
• n the 1D array, we don't need to specify the size of the array if the
declaration and initialization are being done simultaneously.
• However, this will not work with 2D arrays. We will have to define at
least the second dimension of the array.
• The two-dimensional array can be declared and defined in the following
way.
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
02.11.23 Module 3 26
Example:
#include <stdio.h>
Output:
int main () {
/* an array with 5 rows and 2 columns*/
a[0][0]: 0
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j; a[0][1]: 0
a[1][0]: 1
/* output each array element's value */
a[1][1]: 2
for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 2; j++ ) { a[2][0]: 2
printf("a[%d][%d] = %d\n", i,j, a[i][j] ); a[2][1]: 4
} a[3][0]: 3
}
a[3][1]: 6
return 0; a[4][0]: 4
} a[4][1]: 8
02.11.23 Module 3 27
How 2D Arrays are Stored in the Memory?
•The elements of the 2-D array have to be stored contiguously in memory. As the
computers have linear memory addresses, the 2-D arrays must be linearized so as to
enable their storage. There are two ways to achieve linearization of array elements:
Row-major- The linearization technique stores firstly the first row of the array,
then the second row of the array, then the third row, and so on. (i.e. elements are
stored row-wise. Rows are listed on the basis of columns)
Column-major– This linearization technique stores first the first column, then the
second column, then the third column, and so on i.e. (elements are stored column-
wise. Columns are listed on the basis of rows)
•The computer does not keep track of the addresses of all the elements of the array
but does keep track of the Base Address (starting address of the very first element)
and calculates the addresses of the elements when required.
02.11.23 Module 3 28
Three-Dimensional Array in C
• A Three Dimensional Array or 3D array in C is a collection of two-
dimensional arrays. It can be visualized as multiple 2D arrays stacked on top
of each other.
02.11.23 Module 3 29
Conti...
• We can declare a 3D array with x 2D arrays each having y rows and z columns using the syntax
shown below.
• Three dimensional array is form of a Multidimensional array.
• 3D array is a multi-dimensional array used to store 3-dimensional information.
• 3D array is essentially an array of arrays of arrays : it's an array or collection od 2D array. and a
2D array is an array of 1D array.
• It can be of any type like integer, character, float, etc. depending on the Initialization.
data_type array_name[x][y][z];
data_type: Type of data to be stored in each element.
array_name: name of the array
x: Number of 2D arrays.
y: Number of rows in each 2D array.
z: Number of columns in each 2D array.
02.11.23 Module 3 30
data_type array_name[x][y][z];
• We have to write the 3D array by using three square brackets.
• x is used to define the number of rows in array,
• y is used to define the number of columns of array,
• z is used to define the number of blocks of array.
02.11.23 Module 3 31
Initialization of three-dimensional array
First method :
int arr[2][3][4] = {2, 1, 0, 2, 3, -1, 5, 10, 13, 7, 20, 12, 3, -4, 6, -2, 8, -1, 5, -5, -2,
11, 14, 0};
Second (Better) method :
int arr[2][3][4] = {{ {2, 1, 0, 2}, {3, -1, 5, 10}, {13, 7, 20, 12} },{ {3, -4, 6, -2},
{8, -1, 5, -5}, {-2, 11, 14, 0} }};
02.11.23 Module 3 32