0% found this document useful (0 votes)
29 views21 pages

Module-4-2D and Multidimensional Arrays

The document covers the concept of 2D and multidimensional arrays, including their declaration, initialization, and operations such as transpose, sum, and product. It provides examples of C programs for reading, displaying, and performing operations on 2D arrays, as well as applications of arrays in sorting and searching. Additionally, it discusses sorting techniques like insertion sort with a detailed example.

Uploaded by

Kangana W. M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views21 pages

Module-4-2D and Multidimensional Arrays

The document covers the concept of 2D and multidimensional arrays, including their declaration, initialization, and operations such as transpose, sum, and product. It provides examples of C programs for reading, displaying, and performing operations on 2D arrays, as well as applications of arrays in sorting and searching. Additionally, it discusses sorting techniques like insertion sort with a detailed example.

Uploaded by

Kangana W. M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Module-4: 2D and Multidimensional array

Two dimensional arrays, operations on two-dimensional arrays, two


dimensional arrays to functions, multidimensional arrays, applications of
arrays, case study with sorting techniques

2-Dimensional Array

Arrays with 2 dimensions are called 2 –Dimensional array or 2-D array.

Declaration of 2-D array:

data_type array_name[row_size][column_size];

data_type can be any primitive data type.

array_name is a variable name

row_size is the maximum number of rows in the array.

column_size is the maximum number of column in the array.

Example: int a[2][3];

This can be read as

Initialization of 2-D array:

1. Initialize with total number of elements:

int a[2][3]={1,2,3,4,5,6}

2. Initialize with sets

int a[2][3]={{1,2,3},{4,5,6}}

3. Partial initialization

int a[2][3]={{1,1},{2}}

Nisha S K, Dept. of ECE, SVIT Page 1


Module-4: 2D and Multidimensional array

4. Initialize without size

int a[ ][3]={{1,2,3},{4,5,6}}

Initialization of 2-D array:

for(i=0;i<row;i++)

for(j=0;j<column;j++)

scanf(“%d”,&a[i][j]);

WAP to read and display elements from 2-D array.

#include<stdio.h>

void main()

int a[20][20],m,n,i,j;

printf("Enter the number of rows and columns: ");

scanf("%d,%d",&m,&n);

printf("Enter the elements of the array:");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

scanf("%d",&a[i][j]);

Nisha S K, Dept. of ECE, SVIT Page 2


Module-4: 2D and Multidimensional array

printf("The array elements are:\n");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

printf("%d\t",a[i][j]);

printf("\n");

Output:

Enter the number of rows and columns: 3,3

Enter the elements of the array:1 2 3 4 5 6 7 8 9

The array elements are:

1 2 3

4 5 6

7 8 9

Operations on 2-Dimensional Array

1. Transpose

2. Sum

3. Difference

4. Product

Nisha S K, Dept. of ECE, SVIT Page 3


Module-4: 2D and Multidimensional array

WAP to transpose 3 X 3 matrix.

#include<stdio.h>

void main()

int a[20][20],m,n,i,j,b[20][20];

printf("Enter the number of rows and columns: ");

scanf("%d,%d",&m,&n);

printf("Enter the elements of the array:");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

scanf("%d",&a[i][j]);

printf("The array elements are:\n");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

printf("%d\t",a[i][j]);

printf("\n");

Nisha S K, Dept. of ECE, SVIT Page 4


Module-4: 2D and Multidimensional array

for(i=0;i<m;i++)

for(j=0;j<n;j++)

b[i][j]=a[j][i];

printf("The elemnts of transposed matrix are:\n");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

printf("%d\t",b[i][j]);

printf("\n");

Output:

Enter the number of rows and columns: 3,3

Enter the elements of the array:1 2 3 4 5 6 7 8 9

The array elements are:

1 2 3

4 5 6

7 8 9

Nisha S K, Dept. of ECE, SVIT Page 5


Module-4: 2D and Multidimensional array

The elements of transposed matrix are:

1 4 7

2 5 8

3 6 9

WAP to input 2 m x n matrices and then calculate the sum of their


corresponding elements and store it in third m x n matrix.

#include<stdio.h>

int main()

int a[20][20],b[20][20],c[20][20],m,n,p,q,r,t,i,j;

printf("Enter the number of rows and columns in first matrix: ");

scanf("%d,%d",&m,&n);

printf("Enter the number of rows and columns in second matrix: ");

scanf("%d,%d",&p,&q);

if(m!=p||n!=q)

printf("Number of rows and columns of both the matrix should be


equal");

return 0;

r=m;

t=n;

printf("Enter the elements of the array 1:");

for(i=0;i<m;i++)

Nisha S K, Dept. of ECE, SVIT Page 6


Module-4: 2D and Multidimensional array

for(j=0;j<n;j++)

scanf("%d",&a[i][j]);

printf("Enter the elements of the array 2:");

for(i=0;i<p;i++)

for(j=0;j<q;j++)

scanf("%d",&b[i][j]);

for(i=0;i<r;i++)

for(j=0;j<t;j++)

c[i][j]=a[i][j]+b[i][j];

printf("The elements of the resultant matrix are:\n");

for(i=0;i<r;i++)

for(j=0;j<t;j++)

Nisha S K, Dept. of ECE, SVIT Page 7


Module-4: 2D and Multidimensional array

printf("%d\t",c[i][j]);

printf("\n");

Output:

Enter the number of rows and columns in first matrix: 2,2

Enter the number of rows and columns in second matrix: 2,2

Enter the elements of the array 1:2 2 2 2

Enter the elements of the array 2:2 2 2 2

The elements of the resultant matrix are:

4 4

4 4

WAP to input 2 m x n matrices and then calculate the product of their


corresponding elements and store it in third m x n matrix.

#include<stdio.h>

int main()

int a[20][20],b[20][20],c[20][20],m,n,p,q,k,i,j;

printf("Enter the number of rows and columns in first matrix: ");

scanf("%d,%d",&m,&n);

printf("Enter the number of rows and columns in second matrix: ");

scanf("%d,%d",&p,&q);

if(n!=p)

Nisha S K, Dept. of ECE, SVIT Page 8


Module-4: 2D and Multidimensional array

printf("Matrix multiplication is not possible");

return 0;

printf("Enter the elements of the array 1:");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

scanf("%d",&a[i][j]);

printf("Enter the elements of the array 2:");

for(i=0;i<p;i++)

for(j=0;j<q;j++)

scanf("%d",&b[i][j]);

for(i=0;i<m;i++)

for(j=0;j<q;j++)

Nisha S K, Dept. of ECE, SVIT Page 9


Module-4: 2D and Multidimensional array

c[i][j]=0;

for(k=0;k<n;k++)

c[i][j]=a[i][k]*b[k][j]+c[i][j];

printf("The elements of the resultant matrix are:\n");

for(i=0;i<m;i++)

for(j=0;j<q;j++)

printf("%d\t",c[i][j]);

printf("\n");

Output:

Enter the number of rows and columns in first matrix: 2,2

Enter the number of rows and columns in second matrix: 2,2

Enter the elements of the array 1:2 2 2 2

Enter the elements of the array 2:2 2 2 2

The elements of the resultant matrix are:

8 8

8 8

Nisha S K, Dept. of ECE, SVIT Page 10


Module-4: 2D and Multidimensional array

Using arrays with functions

• Putting individual elements of the array

• Passing the whole array

Passing individual elements of the array

#include<stdio.h>

void square(int x);

void main()

int n,a[10],i;

printf("Enter the number of elements: ");

scanf("%d",&n);

printf("Enter the elements: ");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("The square of given elements are: ");

for(i=0;i<n;i++)

square(a[i]);

void square(int x)

printf("%d\t",x*x);

return;

Output:

Nisha S K, Dept. of ECE, SVIT Page 11


Module-4: 2D and Multidimensional array

Enter the number of elements: 5

Enter the elements: 1 2 3 4 5

The square of given elements are: 1 4 9 16 25

Passing whole array

#include<stdio.h>

void avg(int a[]);

void main()

int b[6]={1,2,3,4,5,6};

avg(b);

void avg(int a[])

int i,Average,sum=0;

for(i=0;i<6;i++)

sum=sum+a[i];

Average=sum/6;

printf("Average=%d",Average);

Output:

Average=3

Nisha S K, Dept. of ECE, SVIT Page 12


Module-4: 2D and Multidimensional array

Multi-Dimensional array

• A Multi-Dimensional array is an array of arrays.

• Like we have 1 index in 1-D array, 2 index in 2-D array, we have n index
in n-dimensional array.

WAP to read and display 2 x 2 x 2 array.

#include<stdio.h>

void main()

int a[2][2][2],i,j,k;

printf("Enter the elements of the matrix: ");

for(i=0;i<2;i++)

for(j=0;j<2;j++)

for(k=0;k<2;k++)

scanf("%d",&a[i][j][k]);

printf("The matrix is: \n");

for(i=0;i<2;i++)

for(j=0;j<2;j++)

Nisha S K, Dept. of ECE, SVIT Page 13


Module-4: 2D and Multidimensional array

for(k=0;k<2;k++)

printf("a[%d][%d][%d]=%d\t",i,j,k,a[i][j][k]);

printf("\n");

Output:

Enter the elements of the matrix: 1 2 3 4 5 6 7 8 9

The matrix is:

a[0][0][0]=1 a[0][0][1]=2

a[0][1][0]=3 a[0][1][1]=4

a[1][0][0]=5 a[1][0][1]=6

a[1][1][0]=7 a[1][1][1]=8

Applications of array

• Storing and accessing data: Arrays are used to store and retrieve data
in a specific order. For example, an array can be used to store the scores
of a group of students, or the temperatures recorded by a weather
station.

• Sorting: Arrays can be used to sort data in ascending or descending


order. Sorting algorithms such as bubble sort, merge sort, and quick sort
rely heavily on arrays.

• Searching: Arrays can be searched for specific elements using


algorithms such as linear search and binary search.

Nisha S K, Dept. of ECE, SVIT Page 14


Module-4: 2D and Multidimensional array

• Matrices: Arrays are used to represent matrices in mathematical


computations such as matrix multiplication, linear algebra, and image
processing.

• Stacks and queues: Arrays are used as the underlying data structure
for implementing stacks and queues, which are commonly used in
algorithms and data structures.

• Graphs: Arrays can be used to represent graphs in computer science.


Each element in the array represents a node in the graph, and the
relationships between the nodes are represented by the values stored in
the array.

Case study with sorting techniques:

• Bubble sort

• Insertion sort

• Selection sort

Insertion sort

• It is a very simple sorting algorithm, in which one element at a time.

• The main idea behind insertion sort is that it inserts each item into its
proper place in the final list.

• It works as follows:

1. The array of the values to be sorted is divided into 2 sets.

2. The sorting algorithm will proceed until there are elements in the
unsorted array.

3. Suppose there are n elements in the array. Initially the element with
index 0 is in the sorted set, rest is in the unsorted set.

4. The first element of the unsorted partition has array index 1. During
each iteration, the first element in the unsorted set is picked and
inserted in correct position.

Nisha S K, Dept. of ECE, SVIT Page 15


Module-4: 2D and Multidimensional array

Insertion Sort Example:

Consider an array of integers given below. Sort the values in the array using
insertion sort.

39 9 45 63 18 81 108 54 72 36

39 9 45 63 18 81 108 54 72 36

A[0] is the only element in sorted list.

Pass 1:

9 39 45 63 18 81 108 54 72 36

Pass 2:

9 39 45 63 18 81 108 54 72 36

Pass 3:

9 39 45 63 18 81 108 54 72 36

Pass 4:

9 18 39 45 63 81 108 54 72 36

Pass 5:

9 18 39 45 63 81 108 54 72 36

Pass 6:

9 18 39 45 63 81 108 54 72 36

Pass 7:

9 18 39 45 54 63 81 108 72 36
Pass 8:

Nisha S K, Dept. of ECE, SVIT Page 16


Module-4: 2D and Multidimensional array

9 18 39 45 54 63 72 81 108 36

Pass 9:

9 18 36 39 45 54 63 72 81 108

WAP to sort an array using insertion sort algorithm.

#include<stdio.h>

void ins_sort(int a[],int n);

void main()

int a[10],i,n;

printf("Enter the number of elements in the array: ");

scanf("%d",&n);

printf("Enter the array elements: ");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

ins_sort(a,n);

printf("The sorted array is: \n");

for(i=0;i<n;i++)

printf("%d\t",a[i]);

Nisha S K, Dept. of ECE, SVIT Page 17


Module-4: 2D and Multidimensional array

void ins_sort(int a[],int n)

int i,j,temp;

for(i=0;i<n;i++)

temp=a[i];

j=i-1;

while((temp<a[j])&&(j>=0))

a[j+1]=a[j];

j--;

a[j+1]=temp;

Selection sort

• It is generally the preferred choice for sorting files with very large
objects.

• Although it performs worse than insertion sort, it is noted for its


simplicity, and also has performance advantages over complicated
algorithms.

Technique:

Consider array with N elements. The selection sort makes N-1 passes to sort
the entire array and works as follows. First find the smallest value in the array
and place it in the first position. Then find the second smallest value in the

Nisha S K, Dept. of ECE, SVIT Page 18


Module-4: 2D and Multidimensional array

array and place it in the second position. Repeat this procedure until the entire
array is sorted.

Selection sort example:

39 9 81 45 90 27 72 18

Pass POS A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]
1 1 9 39 81 45 90 27 72 18
2 7 9 18 81 45 90 27 72 39
3 5 9 18 27 45 90 81 72 39
4 7 9 18 27 39 90 81 72 45
5 7 9 18 27 39 45 81 72 90
6 6 9 18 27 39 45 72 81 90
7 6 9 18 27 39 45 72 81 90

WAP to sort an array using selection sort algorithm.

#include<stdio.h>

void sel_sort(int a[],int n);

int small(int a[],int k,int n);

void main()

int a[10],i,n;

printf("Enter the number of elements in the array: ");

scanf("%d",&n);

printf("Enter the array elements: ");

for(i=0;i<n;i++)

Nisha S K, Dept. of ECE, SVIT Page 19


Module-4: 2D and Multidimensional array

scanf("%d",&a[i]);

sel_sort(a,n);

printf("The sorted array is: \n");

for(i=0;i<n;i++)

printf("%d\t",a[i]);

int small(int a[],int k,int n)

int pos=k,s=a[k],i;

for(i=k+1;i<n;i++)

if(a[i]<s)

s=a[i];

pos=i;

return pos;

void sel_sort(int a[],int n)

Nisha S K, Dept. of ECE, SVIT Page 20


Module-4: 2D and Multidimensional array

int k,pos,temp;

for(k=0;k<n;k++)

pos=small(a,k,n);

temp=a[k];

a[k]=a[pos];

a[pos]=temp;

*****End*****

Nisha S K, Dept. of ECE, SVIT Page 21

You might also like