Arrays
Defining an array
Processing an array
Multi dimensional arrays
strings
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
• Static Initialization:
• In this method, the array elements
are explicitly specified in the source
code.
• Example (in C):
• int numbers[] = {1, 2, 3, 4, 5};
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept of EEE, PSG CT
• Dynamic Initialization: The array
elements are initialized using
runtime values or calculated
expressions
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept of EEE, PSG CT
• int arr[10]; // Declare an array of size
10 to store integer values
• int i;
• printf("Input 10 elements in the
array :\n");
• for(i=0; i<10; i++)
•{
• printf("element - %d : ",i);
• scanf("%d", &arr[i]); }
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept of EEE, PSG CT
• Zero Initialization: All elements of
the array are initialized to zero.
• int zeros[5] = {0}; // Initializes all
elements to 0
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
• Default Initialization: The elements of the
array are initialized to default values based on
their data type.
• This usually means zero for numeric types and
null for reference types
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
• Write a program in C to read n
number of values in an array and
display them in reverse order.
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
• Start
• Declare n, i as integer variables
• Declare nums as an integer array of size n
• Read n from the user
• Read values into the array nums from the user
in a loop from i=0 to n-1
• Print "Array in reverse order: "
• For i=n-1 to 0 Step -1
• Print nums [i]
• End For
• End
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
#include <stdio.h>
int main()
{
int i, n, a[100];
printf("\n\nRead n number of values in an array and
display it in reverse order:\n");
printf("-------------------------------------------\n");
printf("Input the number of elements to store in the
array :");
scanf("%d", &n);
printf("Input %d number of elements in the array :\n",
n);
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
for (i = 0; i < n; i++)
{
printf("element - %d : ", i);
scanf("%d", &a[i]);
}
printf("\nThe values stored in the array are : \n");
for (i = 0; i < n; i++)
{
printf("% 5d", a[i]);
}
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
printf("\n\nThe values stored in the array in
reverse are :\n");
for (i = n - 1; i >= 0; i--)
{
printf("% 5d", a[i]);
}
printf("\n\n");
return 0;
}
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
Read n number of values in an array and display it in reverse order: -
-----------------------------------------------------------------------
Input the number of elements to store in the array :3
Input 3 number of elements in the array :
element - 0 : 2
element - 1 : 5
element - 2 : 7
The values store into the array are :
257
The values store into the array in reverse are :
752
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
• Multidimensional Array
Initialization: Arrays with multiple
dimensions can also be initialized
using nested initialization methods.
• int matrix[][] =
• { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
• To get the size of the array in bytes, we
multiply the size of a single element with the
total number of elements in the array.
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)
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
Two-Dimensional Array in C
• A two-dimensional array or 2D array in C is the
simplest form of the multidimensional array.
• We can visualize a two-dimensional array as an
array of one-dimensional arrays arranged one
over another forming a table with ‘x’ rows and ‘y’
columns
• where the row number ranges from 0 to (x-1) and
the column number ranges from 0 to (y-1).
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
Declaration of Two-Dimensional Array in C
• The basic form of declaring a 2D array with x rows
and y columns in C is shown below.
• Syntax:
• data_type array_name[x][y];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 columns.
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
Initialization of Two-Dimensional Arrays in C
• The various ways in which a 2D array can be
initialized are as follows:
• Using Initializer List
• Using Loops
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
1. Initialization of 2D array using Initializer List
• We can initialize a 2D array in C by using an initializer
list as shown in the example below.
• First Method:
• int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11} The
above array has 3 rows and 4 columns.
• The elements in the braces from left to right are stored
in the table also from left to right.
• The elements will be filled in the array in order: the
first 4 elements from the left will be filled in the first
row, the next 4 elements in the second row, and so on.
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
2. Initialization of 2D array using Loops
• int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
• This type of initialization makes use of nested
braces.
• Each set of inner braces represents one row. In
the above example, there is a total of three
rows so there are three sets of inner braces.
• The advantage of this method is that it is
easier to understand.
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
Example:
• int x[3][4];
• for(int i = 0; i < 3; i++)
• {
• for(int j = 0; j < 4; j++)
• {
• x[i][j] = i + j;
• }
• }
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
include <stdio.h>
nt 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]);
}
}
return (0);
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
include <stdio.h>
oid main(void)
int i,j;
int a[3][2] = { {1,2,3}, {4,5,6}, {7,8,9} };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
printf(“%5d", a[i][j]);
printf(“\n”);
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
include <stdio.h>
oid main(void)
int i,j;
int mat[3][3];
printf(“Enter elements of matrix \n”)
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf(“%d”, &mat[i][j]);
}
printf(“Transpose of matrix \n”)
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf(“\t%d”, mat[j][i]);
}
printf(“\n”);
}
} 06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
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.
• 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.
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
• 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];
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
Initialization of 3D Array using Initializer
List
• 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};
• 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} }
};
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
Initialization of 3D Array using Loops
• 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);
• }
• }
• }
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
#include <stdio.h>
int main(void)
{
// initializing the 3-dimensional array
int x[2][3][2] = { { { 0, 1 }, { 2, 3 }, { 4, 5 } },
{ { 6, 7 }, { 8, 9 }, { 10, 11 } }
};
// output each element's value
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 2; ++k) {
printf("Element at x[%i][%i][%i] = %d\n",
i,
j, k, x[i][j][k]);
}
}
}
return (0);
} 06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
• #include <stdio.h> • for (j = 1; j <= i; +
• int main() { +j) {
• int i, j, rows; • printf("%d ", j);
• printf("Enter the • }
number of rows: "); • printf("\n");
• scanf("%d", &rows); • }
• for (i = 1; i <= rows; • return 0;
++i) { • }
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
• #include <stdio.h> • }
• int main() { • while (k != 2 * i - 1)
• int i, space, rows, k = 0; • {
• printf("Enter the number of • printf("* ");
rows: "); • ++k;
• scanf("%d", &rows); • }
• for (i = 1; i <= rows; ++i, k = 0) • printf("\n");
• { • }
• for (space = 1; space <= rows - • return 0;
i; ++space) • }
• {
• printf(" ");
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT
06/22/2025 Dr. P. Sivakumar and Dr. A. Angamuthu Dept
of EEE, PSG CT