0% found this document useful (0 votes)
39 views24 pages

2 Dimensional Arrays

The document provides an overview of two-dimensional arrays in the C programming language, explaining their structure, declaration, initialization, and operations such as addition, subtraction, and multiplication. It also discusses how to pass 2D arrays to functions and emphasizes the need to specify the number of columns due to the way C stores these arrays in memory. Examples are included to illustrate concepts like matrix transposition and function usage with 2D arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views24 pages

2 Dimensional Arrays

The document provides an overview of two-dimensional arrays in the C programming language, explaining their structure, declaration, initialization, and operations such as addition, subtraction, and multiplication. It also discusses how to pass 2D arrays to functions and emphasizes the need to specify the number of columns due to the way C stores these arrays in memory. Examples are included to illustrate concepts like matrix transposition and function usage with 2D arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

2 DIMENSIONAL

ARRAYS
TA K AW IR A TA D IWA N A S H E T H 2 4 0 5 5 8 N
TA N D I T I N O T E N D A H 2 4 0 4 3 3 R
TA N G W E N A E D WA R D H 2 4 0 11 9 G
TO Z V I R E VA J O S E P H H 2 4 0 4 5 8 J
W IL L A R D M A R T I N
ZHOU SUSAN H240278X
• The C language provides a capability
that enables the user to design a set of
similar data types, called array.

• An array is a collection of similar


elements.
• These similar elements could be all ints,
or all floats, or all chars, etc.

06/01/2025
• It is also possible for arrays to have two
or more dimensions.
• The two dimensional array is also called a
matrix.
• Dimensional refers to the array’s size ,
which is how big the array is.

06/01/2025
• Usually, the array of characters is called a
‘string’,
whereas an array of ints or floats is called
simply an array.
• All elements of any given array must be of
the same type. i.e. we cannot have an array
of 10 numbers, of which 5 are ints and 5 are
floats.
06/01/2025
Declaration of Two-dimensional
Arrays
Format:
data_type array_name[row_size1][column_size1];
The first subscript represents the row size and the second subscript
represents the column size.
The value in the ith row and jth column is referred to by name[i][j].
The total number of elements in a two dimensional array is
calculated by multiplying the number of rows by the number of
columns.
In two-dimensional arrays, the values are stored row by row.
06/01/2025
Example

int x[3][2];

06/01/2025
Initialisation of Arrays

Syntax:
data_type array_name[row_size]
[col_size]={variables};

A two-dimensional array can be initialised as given


below.
int a[3][2] = {20, 25, -3, 8, -5, 7};
06/01/2025
• Here, the initialised values are assigned to the array
elements according to the order of the storage in the
memory.

• But if the initialisers are enclosed within the inner


braces as given below
int a[3][2] = {{20,25}, {-3, 8}, {-5,7}};
• The initialised values within the inner braces are
assigned to each row.
• To enforce the assignment of each row, it is essential to
use the inner braces.

06/01/2025
• If the number of values initialised for an array is less than the size
mentioned, the missing elements are assigned zero. In the
initialisation
int a[3][4] = { {1,2},{4,8,15} };

• the elements a[0][0] and a[0][1] of the zeroth row and a[1][0], a[1]
[1] and a[1][2] of the first row are initialised with the values 1,2,4,8
and 15 respectively.

• All the other elements are initialised to zero.

If it is given as
int a[3][4] = {1,2,3,8,15};
the values are assigned from the left end to a[0][0], a[0][1], a[0][2],
a[0][3], and a[[1][0] according to the order of the storage
representation. 06/01/2025
OPERATIONS ON TWO DIMENSIONAL
ARRAYS

06/01/2025
Transpose of a matrix
the tranpose of an m×n matrix Ais given
as a n×m matrix where
Bi ,j =Ai , j.

06/01/2025
Sum
Two matrices that are compatible with
each other can be added,storing the result
in the third matrix
The elements can be added by writing:
Ci , j =Ai , j + Bi , j

06/01/2025
Difference
Two matrices with the same number of
rows and columns can be subtracted
The elements of the two matrices can
be subtracted by writing:
C i , j = A i , j - Bi , j
06/01/2025
Product
Two matrices can be multiplied with each
other if the number of columns in the first
matrix is equal to the number of rows in the
second matrix
The elements can be multiplied by writing:
Ci , j =∑ Ai , kBk ,j for k=1 to n

06/01/2025
Example
Transpose of a 3×3 matrix

#include <stdio.h>

int main()

int i, j, mat[3][3], transposed_mat[3][3];

printf(" Enter the elements of the matrix \n");

06/01/2025
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &mat[i][j]);
}
}
printf(" The elements of the matrix are \n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("\t %d", mat[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)

06/01/2025
transposed_mat[i][j] = mat[j][i];
}
printf("\n The elements of the transposed matrix are ");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("\t %d", transposed_mat[i][j]); return 0;
}

Output

Enter the elements of the matrix


1 2 3 4 5 6 7 8 9 The elements of the matrix are
1 2 3
4 5 6
7 8 9

The elements of the transposed matrix are


1 4 7
2 5 8
3 6 9

06/01/2025
In C, you can pass a 2D array to a function in
two ways:
1)by passing the entire array as an argument or
2)by passing the array as a dynamic pointer to
the function.

The first method involves specifying the size of


the second dimension while leaving the first
dimension unspecified.
The second method uses a dynamic pointer and
06/01/2025
USING A DYNAMIC POINTER

When you pass a 2D array to a function, you


need to specify the number of columns as a
parameter because C stores 2D arrays as
contiguous rows in memory, and the function
needs this information to correctly access the
elements.

06/01/2025
#include <stdio.h>

// Function to print the elements of a 2D integer array


void print2DArray(int arr[][3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}

int main() {
int myArray[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int rows = sizeof(myArray) / sizeof(myArray[0]);
int cols = sizeof(myArray[0]) / sizeof(myArray[0][0]);

printf("Original 2D array:\n");
print2DArray(myArray, rows, cols); // Passing the 2D array to the function

return 0;
}
06/01/2025
In this example:

The print2DArray function takes three parameters: a 2D integer array


arr, the number of rows (rows), and the number of columns (cols) in
the array.

Inside the main function, a 2D integer array myArray is defined, and


the number of rows and columns is calculated using the sizeof
operator.

The print2DArray function is called to print the elements of the 2D


array.
06/01/2025
Output:

Original 2D array:
123
456
789

06/01/2025
• When you pass a 2D array to a function, you need to specify the
number of columns as a parameter because C stores 2D arrays
as contiguous rows in memory, and the function needs this
information to correctly access the elements.

• Just like with 1D arrays, modifications made to a 2D array inside


a function are reflected in the original array because you are
working with pointers to the actual array elements.

• C does not allow you to return entire 2D arrays from functions


directly. If you need to create or modify a 2D array within a
function and return it, you can do so by dynamically allocating
memory for the 2D array and returning a pointer to the
allocated memory.
06/01/2025
THE END
THANK YOU

You might also like