Lecture 6:
Function
Engr. Md Arif Hasan Chowdhury
Assistant Professor
Department of Computer Science and
Engineering
Southern University Bangladesh
Concept of Array
Array
Array is a collection – Array is a container that can hold a collection of
data.
Array is finite – The collection of data in array is always finite, which is
determined prior to its use.
Array is sequential – Array stores collection of data sequentially in
memory.
Array contains homogeneous data – The collection of data in array
must share a same data type.
A class has 20 students and you want to
arrange the marks of all the 20 students
Example: if you want to store 6 integers,
you can create an array for it.
int data[6];
Properties of Array
Each element of an array is of same data type
and the same size, i.e., int = 4 bytes.
Elements of the array are stored at
contiguous memory locations
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.
Advantage of C Array
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 of C Array
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.
Types of Array
Arrays in C are classified into three
types:
•One-dimensional arrays
•Two-dimensional arrays
•Multi-dimensional arrays
How to declare an array?
Syntax of Array Declaration:
data_type array_name [size];
or
data_type array_name [size1] [size2]...
[sizeN];
/* Example of array declaration */
char a[5]; /* char type value array */
float ar[9]; /* float type value array */
int arr[10]; /* int type value array */
// C Program to illustrate the array declaration
#include <stdio.h>
int main()
{
// declaring array of integers
int arr_int[5];
// declaring array of characters
char arr_char[5];
return 0;
Array Initialization
• Static array initialization
• Dynamic array initialization
Syntax:
data_type array_name [size] = {value1, value2, ... valueN};
Example
int marks[5]={2,4,8,12,16};
Syntax:
array_name[index] = some_value;
Example:
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
Example
// 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;
}
Example
#include<stdio.h> #include<stdio.h>
int main(){ int main(){
int i=0; int i=0;
int marks[5];// int marks[5]={20,30,40,50,60};
declaration of array //
marks[0]=80;// declaration and initialization of arr
initialization of array ay
marks[1]=60; //traversal of array
marks[2]=70; for(i=0;i<5;i++)
marks[3]=85; {
marks[4]=75; printf("%d \n",marks[i]);
//traversal of array }
for(i=0;i<5;i++){ return 0;
printf("%d \n",marks[i]); }
}//end of for loop
return 0;
}
Output
80 60 70 85 75 Output
20 30 40 50 60
Access Array Elements
array_name [index];
Example:
// C Program to illustrate element access using array
// subscript
#include <stdio.h>
int main()
{
// array declaration and initialization
int arr[5] = { 15, 25, 35, 45, 55 };
// accessing element at index 2 i.e 3rd element
printf("Element at arr[2]: %d\n", arr[2]);
// accessing element at index 4 i.e last element
printf("Element at arr[4]: %d\n", arr[4]);
// accessing element at index 0 i.e first element
printf("Element at arr[0]: %d", arr[0]);
return 0;
} Output:
Element at arr[2]: 35
Element at arr[4]: 55
Element at arr[0]: 15
Array Traversal
Array Traversal using for Loop
for (int i = 0; i < N; i++)
{
array_name[i];
}
Input and Output Array Elements
Read Array Elements:
scanf("%d", &mark[array index]);
Print Array Elements:
printf("%d", mark[array index]);
Example
// C Program to demonstrate the use of array
#include <stdio.h>
int main()
{
// array declaration and initialization
int arr[5] = { 10, 20, 30, 40, 50 };
// modifying element at index 2
arr[2] = 100;
// traversing array using for loop
printf("Elements in Array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Elements in Array: 10 20 100 40 50
Read and Print elements of an array
#include <stdio.h>
void main()
{
int arr[10];
int i;
printf("\n\nRead and Print elements of an array:\
n");
printf("-----------------------------------------\
n"); printf("Input 10 elements in the array :\n");
for(i=0; i<10; i++)
{
printf("element - %d : ",i);
scanf("%d", &arr[i]);
}
printf("\nElements in array are: ");
for(i=0; i<10; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
Access elements out of its bound!
Suppose you declared an array of 10 elements. Let's say,
int testArray[10];
You can access the array elements from testArray[0] to testArray[9].
Now let's say if you try to access testArray[12]. The element is not available. This may cause
unexpected output (undefined behavior). Sometimes you might get an error and some other
time your program may run correctly.
Hence, you should never access elements of an array outside of its bound.
Multidimensional Arrays in C
Syntax:
data_type array_name[size1][size2]....
[sizeN];
Where
•data_type: Type of data to be stored in the array.
•array_name: Name of the array.
•size1, size2,…, sizeN: Size of each dimension.
Multidimensional Arrays:
Size of Multidimensional Arrays:
Total number of elements that can be stored in a multidimensional array
can be calculated by multiplying the size of all the dimensions.
For example:
•The array int x[10][20] can store total (10*20) = 200 elements.
•Similarly array int x[5][10][20] can store total (5*10*20) = 1000
elements.
For example:
•Size of array int x[10][20] = 10 * 20 * 4 = 800 bytes. (where int = 4
bytes)
•Similarly, size of int x[5][10][20] = 5 * 10 * 20 * 4 = 4000 bytes.
(where int = 4 bytes)
Multidimensional array are:
1.Two Dimensional Array
2.Three Dimensional Array
2D array
2D array is the simplest form of the multidimensional
array
Declaration of Two-Dimensional Array in C
Syntax:
data_type array_name[size][size];
where,
•data_type: Type of data to be stored in each element.
•array_name: name of the array
•x: Number of rows.
•y: Number of column
Initialization of Two-Dimensional Arrays
in C
The various ways in which a 2D array can be initialized are as
follows:
1.Using Initializer List
2.Using Loops
• Initialization of 2D array using Initializer List
First Method:
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 ,
10 , 11}
Second Method (better):
int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
• Initialization of 2D array using Loops
Example:
int x[3][4];
for(int i = 0; i < 3; i++)
{ for(int j = 0; j < 4; j++)
{
x[i][j] = i + j;
}
}
Accessing Elements of Two-Dimensional
Arrays in C
Elements in 2D arrays are accessed using row indexes and column
indexes. Each element in a 2D array can be referred to by:
Syntax:
array_name[i][j]
where,
•i: The row index.
•j: The column index.
Example:
int x[2][1];
// C Program to print the elements of a
// Two-Dimensional array
#include <stdio.h>
int main(void)
{
// an array with 3 rows and 2 columns.
int x[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
// output each array element's value
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
printf("Element at x[%i][%i]: ", i, j);
printf("%d\n", x[i][j]);
}
} OutputElement at x[0][0]: 0 Element at x[0][1]: 1 Element at x[1][0]: 2
Element at x[1][1]: 3 Element at x[2][0]: 4 Element at x[2][1]: 5
return (0);
}
// This code is contributed by sarajadhav12052009
Three-Dimensional Array in
C
Declaration of Three-Dimensional Array in C
We can declare a 3D array with x 2D arrays each
having y rows and z columns using the syntax
shown below.
Syntax:
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.
Example:
int array[3][3][3];
Initialization of Three-Dimensional Array in C
A 3D array in C can be initialized by using:
Initializer List
Loops
Initialization of 3D Array using Initializer List
Method 1:
int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23};
Method 2(Better):
int x[2][3][4] = { { {0,1,2,3}, {4,5,6,7},
{8,9,10,11} }, { {12,13,14,15},
{16,17,18,19}, {20,21,22,23} } };
Initialization of 3D Array using Loops
Using more nested loop for accessing one more
dimension.
int x[2][3][4];
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
for (int k=0; k<4; k++)
{
x[i][j][k] = (some_value);
}
}
}
Accessing elements in Three-Dimensional Array in C
Syntax:
array_name[x][y][z]
where,
•x: Index of 2D array.
•y: Index of that 2D array row.
•z: Index of that 2D array column
Example
/ C program to print elements of Three-Dimensional Array
#include <stdio.h>
int main(void)
Output
{
Element at x[0][0][0] =
// initializing the 3-dimensional array
0 Element at x[0][0][1]
int x[2][3][2] = { { { 0, 1 }, { 2, 3 }, { 4, 5 } },
= 1 Element at x[0][1]
{ { 6, 7 }, { 8, 9 }, { 10, 11 } }
[0] = 2 Element at x[0]
};
[1][1] = 3 Element at
x[0][2][0] = 4 Element
// output each element's value at x[0][2][1] = 5
for (int i = 0; i < 2; ++i) { Element at x[1][0][0] =
for (int j = 0; j < 3; ++j) { 6 Element at x[1][0][1]
for (int k = 0; k < 2; ++k) { = 7 Element at x[1][1]
printf("Element at x[%i][%i][%i] = %d\n", [0] = 8 Element at x[1]
i, [1][1] = 9 Element at
j, k, x[i][j][k]); x[1][2][0] = 10 Element
} at x[1][2][1] = 11
}
}
return (0);
}