SlideShare a Scribd company logo
1 of 14
Download to read offline
Two Dimensional Array
Definition
Two dimensional array is an array that has two dimensions, such as row and column. Total number
of elements in a two dimensional array can be calculated by multiplication of the numbers of rows
and the number of columns. If there are m rows and n columns then the total number of elements
is mxn, and mxn is called the size of the array. Of course, the data elements of the array will be
same type.
In mathematics, the two dimensional array can be expressed as follows:
Aij or A[I,j] for 1<=i<=m and 1<=j<=n (where m and n are the number of rows and columns
respectively)
A[1…….m, 1………n]
m rows n columns
Fig 1: Symbolic representation of two dimensional array
1 2 3 4 5 6 7
1 0 10 12 13 19 20 18
2 23 5 63 72 79 80 65
3 - - - - - - -
4 - - - - - - -
5 - - - - 75 - -
6 20 31 32 33 39 40 33
Fig 2: Graphical representation of two dimensional array
Store and retrieve values in and from a 2-D array
for(i=0; i<2; i++) {
for(j=0;j<3;j++)
scanf("%d", &B[i][j]);
}
for(i=0; i<2; i++) {
for(j=0;j<3;j++)
printf("%d ", B[i][j]);
}
Array B
Size=6x7
Cell B[5][5]
C program to store and retrieve values in and from a 2-D array
#include<stdio.h>
int main(){
int disp[10][10];
int m,n;
int i, j;
printf("Enter the number of row:n");
scanf("%d",&m);
printf("Enter the number of column:n");
scanf("%d",&n);
for(i=0; i<m; i++) {
for(j=0;j<n;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
printf("Two Dimensional array elements:n");
for(i=0; i<m; i++) {
for(j=0;j<n;j++) {
printf("%dt", disp[i][j]);
if(j==n-1){
printf("n");
}
}
}
return 0;
}
Sample Output
Enter the number of row:
3
Enter the number of column:
4
Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[0][3]:4
Enter value for disp[1][0]:5
Enter value for disp[1][1]:6
Enter value for disp[1][2]:7
Enter value for disp[1][3]:8
Enter value for disp[2][0]:9
Enter value for disp[2][1]:10
Enter value for disp[2][2]:11
Enter value for disp[2][3]:12
Two Dimensional array elements:
1 2 3 4
5 6 7 8
9 10 11 12
Algorithm to find out the summation of boundary elements
1. Input: a two-dimensional array
A[1….m,1….n]
Sum=0;
2. Find each boundary element
for(i = 1; i <= m; i++)
for(j = 1; j <= n; j++)
if(i = 1 || j = 1 || i = m || j = n), sum=sum+A[i.j],
3. Output: Print sum as the result of summation of boundary elements
C program to find out the summation of boundary elements
#include<stdio.h>
int main (){
int i,j, arr[100][100],sum = 0,row,col;
printf("Enter the number of row: ");
scanf("%d",&row);
printf("Enter the number of column: ");
scanf("%d",&col);
printf("Enter row and column n");
for(i = 1; i <= row; i++){
for(j = 1; j <= col; j++){
scanf("%d",&arr[i][j]);
}
}
for(i = 1; i <= row; i++){
for(j = 1; j <= col; j++){
if(i == 1 || j == 1 || i == row || j == col){
sum+=arr[i][j];
}
}
}
printf("Sum of boundary is: %d ",sum);
return 0;
}
Sample Output
Enter the number of row: 3
Enter the number of column: 3
Enter row and column
1 2 3
4 5 6
7 8 9
Sum of boundary is: 40
Algorithm to find out the summation of diagonal elements
1. Input: a two-dimensional array
B[1….n,1….n]
Sum=0;
2. Find each diagonal element and add them with sum
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
if(i = j || i+j = n+1), sum=sum+B[i.j],
3. Output: Print sum as the result of summation of diagonal elements
C program to find out the summation of diagonal elements
#include<stdio.h>
void main()
{
int B[10][10];
int i,j,n,sum=0;
printf("Enter the number of rows and columns for 1st
matrixn");
scanf("%d%d",&n,&n);
printf("Enter the elements of the matrixn");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&B[i][j]);
}
}
printf("The matrixn");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%dt",B[i][j]);
}
printf("n");
}
//To add diagonal elements
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if((i==j) || (i+j==n+1) )
{
sum=sum+B[i][j];
}
}
}
printf("The sum of diagonal elements of a square matrix =
%dn",sum);
return 0;
}
Sample Output
Enter the number of rows and columns for 1st matrix
3 3
Enter the elements of the matrix
1 2 3
4 5 6
7 8 9
The matrix
1 2 3
4 5 6
7 8 9
The sum of diagonal elements of a square matrix = 25
Algorithm for matrix addition
Step 1: Start
Step 2: Declare matrix A[r][c];
and matrix B[r][c];
and matrix C[r][c]; r= no. of rows, c= no. of columns
Step 3: Read r, c, A[][] and B[][]
Step 4: Declare variable i=0, j=0
Step 5: Repeat until i < r
5.1: Repeat until j < c
C[i][j]=A[i][j] + B[i][j]
Set j=j+1
5.2: Set i=i+1
Step 6: C is the required matrix after addition
Step 7: Stop
C program for matrix addition
//A matrix can only be added to (or subtracted from) another
matrix if the two matrices have the same dimensions .
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrixn");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrixn");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrixn");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);
printf("Sum of entered matrices:-n");
for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%dt", sum[c][d]);
}
printf("n");
}
return 0;
}
Sample Output
Enter the number of rows and columns of matrix
2
2
Enter the elements of first matrix
1 2
3 4
Enter the elements of second matrix
5 6
2 1
Sum of entered matrices:-
6 8
5 5
Algorithm for matrix subtraction
Step 1: Start
Step 2: Declare matrix A[r][c] // Matrix 1;
and matrix B[r][c] // Matrix 2;
and matrix C[r][c]; r= no. of rows, c= no. of columns
Step 3: Read r, c, A[][] and B[][]
Step 4: Declare variable i=0, j=0
Step 5: Repeat until i < r
5.1: Repeat until j < c
C[i][j]=A[i][j] - B[i][j]
Set j=j+1
5.2: Set i=i+1
Step 6: C is the required matrix after subtraction
Step 7: Stop
C Program to perform matrix subtraction
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10],
difference[10][10];
printf("Enter the number of rows and columns of matrixn");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrixn");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrixn");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &second[c][d]);
printf("Difference of entered matrices:-n");
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++) {
difference[c][d] = first[c][d] - second[c][d];
printf("%dt",difference[c][d]);
}
printf("n");
}
return 0;
}
Sample Output
Enter the number of rows and columns of matrix
2
2
Enter the elements of first matrix
4 3
2 1
Enter the elements of second matrix
1 2
1 1
Difference of entered matrices:-
3 1
1 0
Algorithm for matrix multiplication
Step 1: Start
Step 2: Declare matrix A[m][n]
and matrix B[p][q]
and matrix C[m][q]
Step 3: Read m, n, p, q.
Step 4: Now check if the matrix can be multiplied or not, if n is not equal to q matrix can't be
multiplied and an error message is generated.
Step 5: Read A[][] and B[][]
Step 4: Declare variable i=0, k=0 , j=0 and sum=0
Step 5: Repeat Step until i < m
5.1: Repeat Step until j < q
5.1.1: Repeat Step until k < p
Set sum= sum + A[i][k] * B[k][j]
Set multiply[i][j] = sum;
Set sum = 0 and k=k+1
5.1.2: Set j=j+1
5.2: Set i=i+1
Step 6: C is the required matrix.
Step 7: Stop
C Program to perform matrix multiplication
//To multiply two matrices, the number of columns of the first
matrix should be equal to the number of rows of the second
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first
matrixn");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrixn");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second
matrixn");
scanf("%d%d", &p, &q);
if ( n != p )
printf("Matrices with entered orders can't be multiplied
with each other.n");
else
{
printf("Enter the elements of second matrixn");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%dt", multiply[c][d]);
printf("n");
}
}
return 0;
}
Sample Output
Enter the number of rows and columns of first matrix
3 3
Enter the elements of first matrix
1 2 0
0 1 1
2 0 1
Enter the number of rows and columns of second matrix
3 3
Enter the elements of second matrix
1 1 2
2 1 1
1 2 1
Product of entered matrices:-
5 3 4
3 3 2
3 4 5

More Related Content

What's hot

Cg my own programs
Cg my own programsCg my own programs
Cg my own programsAmit Kapoor
 
Digital differential analyzer
Digital differential analyzerDigital differential analyzer
Digital differential analyzerSajid Hasan
 
SE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of PuneSE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of PuneBhavesh Shah
 
Wap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmWap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmKapil Pandit
 
COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALCOMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALVivek Kumar Sinha
 
Mat lab lecture part 1
Mat lab lecture part 1Mat lab lecture part 1
Mat lab lecture part 1OmGulshan
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicalsManoj Chauhan
 
Bresenham's line algo.
Bresenham's line algo.Bresenham's line algo.
Bresenham's line algo.Mohd Arif
 
Rsa Signature: Behind The Scenes
Rsa Signature: Behind The Scenes Rsa Signature: Behind The Scenes
Rsa Signature: Behind The Scenes acijjournal
 
Cse 121 presentation on matrix [autosaved]
Cse 121 presentation on matrix [autosaved]Cse 121 presentation on matrix [autosaved]
Cse 121 presentation on matrix [autosaved]Kanis Fatema Shanta
 
Computer graphics programs in c++
Computer graphics programs in c++Computer graphics programs in c++
Computer graphics programs in c++Ankit Kumar
 
Matlab assignment
Matlab assignmentMatlab assignment
Matlab assignmentRutvik
 
Solution of matlab chapter 3
Solution of matlab chapter 3Solution of matlab chapter 3
Solution of matlab chapter 3AhsanIrshad8
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointersSushil Mishra
 
Magic Methods (Python meetup)
Magic Methods (Python meetup)Magic Methods (Python meetup)
Magic Methods (Python meetup)Ines Jelovac
 

What's hot (19)

Cg my own programs
Cg my own programsCg my own programs
Cg my own programs
 
Digital differential analyzer
Digital differential analyzerDigital differential analyzer
Digital differential analyzer
 
SE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of PuneSE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of Pune
 
C programs
C programsC programs
C programs
 
Wap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmWap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithm
 
COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALCOMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUAL
 
Mat lab lecture part 1
Mat lab lecture part 1Mat lab lecture part 1
Mat lab lecture part 1
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicals
 
Arrays
ArraysArrays
Arrays
 
Bresenham's line algo.
Bresenham's line algo.Bresenham's line algo.
Bresenham's line algo.
 
Rsa Signature: Behind The Scenes
Rsa Signature: Behind The Scenes Rsa Signature: Behind The Scenes
Rsa Signature: Behind The Scenes
 
Dvst
DvstDvst
Dvst
 
Cse 121 presentation on matrix [autosaved]
Cse 121 presentation on matrix [autosaved]Cse 121 presentation on matrix [autosaved]
Cse 121 presentation on matrix [autosaved]
 
Computer graphics programs in c++
Computer graphics programs in c++Computer graphics programs in c++
Computer graphics programs in c++
 
Matlab assignment
Matlab assignmentMatlab assignment
Matlab assignment
 
Solution of matlab chapter 3
Solution of matlab chapter 3Solution of matlab chapter 3
Solution of matlab chapter 3
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
 
Thesis PPT
Thesis PPTThesis PPT
Thesis PPT
 
Magic Methods (Python meetup)
Magic Methods (Python meetup)Magic Methods (Python meetup)
Magic Methods (Python meetup)
 

Similar to Two Dimensional Array Definition, Operations & Examples in C

C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020vrgokila
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdfrushabhshah600
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkarsandeep kumbhkar
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
 
C programming codes for the class assignment
C programming codes for the class assignmentC programming codes for the class assignment
C programming codes for the class assignmentZenith SVG
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings imtiazalijoono
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxhappycocoman
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using CBilal Mirza
 

Similar to Two Dimensional Array Definition, Operations & Examples in C (20)

C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
SaraPIC
SaraPICSaraPIC
SaraPIC
 
array.ppt
array.pptarray.ppt
array.ppt
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
C-programs
C-programsC-programs
C-programs
 
C programming codes for the class assignment
C programming codes for the class assignmentC programming codes for the class assignment
C programming codes for the class assignment
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
1D Array
1D Array1D Array
1D Array
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
unit-2-dsa.pptx
unit-2-dsa.pptxunit-2-dsa.pptx
unit-2-dsa.pptx
 
Pnno
PnnoPnno
Pnno
 
C programs
C programsC programs
C programs
 

More from A. S. M. Shafi (20)

2D Transformation in Computer Graphics
2D Transformation in Computer Graphics2D Transformation in Computer Graphics
2D Transformation in Computer Graphics
 
3D Transformation in Computer Graphics
3D Transformation in Computer Graphics3D Transformation in Computer Graphics
3D Transformation in Computer Graphics
 
Projection
ProjectionProjection
Projection
 
2D Transformation
2D Transformation2D Transformation
2D Transformation
 
Fragmentation
FragmentationFragmentation
Fragmentation
 
File organization
File organizationFile organization
File organization
 
Bankers algorithm
Bankers algorithmBankers algorithm
Bankers algorithm
 
RR and priority scheduling
RR and priority schedulingRR and priority scheduling
RR and priority scheduling
 
Fcfs and sjf
Fcfs and sjfFcfs and sjf
Fcfs and sjf
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Stack push pop
Stack push popStack push pop
Stack push pop
 
Queue
QueueQueue
Queue
 
Searching
SearchingSearching
Searching
 
Sorting
SortingSorting
Sorting
 
Linked list
Linked listLinked list
Linked list
 
Sum of subset problem
Sum of subset problemSum of subset problem
Sum of subset problem
 
Quick sort
Quick sortQuick sort
Quick sort
 
N queens problem
N queens problemN queens problem
N queens problem
 
MST
MSTMST
MST
 
Merge sort
Merge sortMerge sort
Merge sort
 

Recently uploaded

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 

Recently uploaded (20)

Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 

Two Dimensional Array Definition, Operations & Examples in C

  • 1. Two Dimensional Array Definition Two dimensional array is an array that has two dimensions, such as row and column. Total number of elements in a two dimensional array can be calculated by multiplication of the numbers of rows and the number of columns. If there are m rows and n columns then the total number of elements is mxn, and mxn is called the size of the array. Of course, the data elements of the array will be same type. In mathematics, the two dimensional array can be expressed as follows: Aij or A[I,j] for 1<=i<=m and 1<=j<=n (where m and n are the number of rows and columns respectively) A[1…….m, 1………n] m rows n columns Fig 1: Symbolic representation of two dimensional array 1 2 3 4 5 6 7 1 0 10 12 13 19 20 18 2 23 5 63 72 79 80 65 3 - - - - - - - 4 - - - - - - - 5 - - - - 75 - - 6 20 31 32 33 39 40 33 Fig 2: Graphical representation of two dimensional array Store and retrieve values in and from a 2-D array for(i=0; i<2; i++) { for(j=0;j<3;j++) scanf("%d", &B[i][j]); } for(i=0; i<2; i++) { for(j=0;j<3;j++) printf("%d ", B[i][j]); } Array B Size=6x7 Cell B[5][5]
  • 2. C program to store and retrieve values in and from a 2-D array #include<stdio.h> int main(){ int disp[10][10]; int m,n; int i, j; printf("Enter the number of row:n"); scanf("%d",&m); printf("Enter the number of column:n"); scanf("%d",&n); for(i=0; i<m; i++) { for(j=0;j<n;j++) { printf("Enter value for disp[%d][%d]:", i, j); scanf("%d", &disp[i][j]); } } printf("Two Dimensional array elements:n"); for(i=0; i<m; i++) { for(j=0;j<n;j++) { printf("%dt", disp[i][j]); if(j==n-1){ printf("n"); } } } return 0; }
  • 3. Sample Output Enter the number of row: 3 Enter the number of column: 4 Enter value for disp[0][0]:1 Enter value for disp[0][1]:2 Enter value for disp[0][2]:3 Enter value for disp[0][3]:4 Enter value for disp[1][0]:5 Enter value for disp[1][1]:6 Enter value for disp[1][2]:7 Enter value for disp[1][3]:8 Enter value for disp[2][0]:9 Enter value for disp[2][1]:10 Enter value for disp[2][2]:11 Enter value for disp[2][3]:12 Two Dimensional array elements: 1 2 3 4 5 6 7 8 9 10 11 12 Algorithm to find out the summation of boundary elements 1. Input: a two-dimensional array A[1….m,1….n] Sum=0; 2. Find each boundary element for(i = 1; i <= m; i++) for(j = 1; j <= n; j++)
  • 4. if(i = 1 || j = 1 || i = m || j = n), sum=sum+A[i.j], 3. Output: Print sum as the result of summation of boundary elements C program to find out the summation of boundary elements #include<stdio.h> int main (){ int i,j, arr[100][100],sum = 0,row,col; printf("Enter the number of row: "); scanf("%d",&row); printf("Enter the number of column: "); scanf("%d",&col); printf("Enter row and column n"); for(i = 1; i <= row; i++){ for(j = 1; j <= col; j++){ scanf("%d",&arr[i][j]); } } for(i = 1; i <= row; i++){ for(j = 1; j <= col; j++){ if(i == 1 || j == 1 || i == row || j == col){ sum+=arr[i][j]; } } } printf("Sum of boundary is: %d ",sum); return 0; }
  • 5. Sample Output Enter the number of row: 3 Enter the number of column: 3 Enter row and column 1 2 3 4 5 6 7 8 9 Sum of boundary is: 40 Algorithm to find out the summation of diagonal elements 1. Input: a two-dimensional array B[1….n,1….n] Sum=0; 2. Find each diagonal element and add them with sum for(i = 1; i <= n; i++) for(j = 1; j <= n; j++) if(i = j || i+j = n+1), sum=sum+B[i.j], 3. Output: Print sum as the result of summation of diagonal elements C program to find out the summation of diagonal elements #include<stdio.h> void main() { int B[10][10]; int i,j,n,sum=0; printf("Enter the number of rows and columns for 1st matrixn"); scanf("%d%d",&n,&n); printf("Enter the elements of the matrixn"); for(i=1;i<=n;i++) {
  • 6. for(j=1;j<=n;j++) { scanf("%d",&B[i][j]); } } printf("The matrixn"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("%dt",B[i][j]); } printf("n"); } //To add diagonal elements for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if((i==j) || (i+j==n+1) ) { sum=sum+B[i][j]; } } } printf("The sum of diagonal elements of a square matrix = %dn",sum); return 0; }
  • 7. Sample Output Enter the number of rows and columns for 1st matrix 3 3 Enter the elements of the matrix 1 2 3 4 5 6 7 8 9 The matrix 1 2 3 4 5 6 7 8 9 The sum of diagonal elements of a square matrix = 25 Algorithm for matrix addition Step 1: Start Step 2: Declare matrix A[r][c]; and matrix B[r][c]; and matrix C[r][c]; r= no. of rows, c= no. of columns Step 3: Read r, c, A[][] and B[][] Step 4: Declare variable i=0, j=0 Step 5: Repeat until i < r 5.1: Repeat until j < c C[i][j]=A[i][j] + B[i][j] Set j=j+1 5.2: Set i=i+1 Step 6: C is the required matrix after addition Step 7: Stop
  • 8. C program for matrix addition //A matrix can only be added to (or subtracted from) another matrix if the two matrices have the same dimensions . #include <stdio.h> int main() { int m, n, c, d, first[10][10], second[10][10], sum[10][10]; printf("Enter the number of rows and columns of matrixn"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrixn"); for (c = 0; c < m; c++) for (d = 0; d < n; d++) scanf("%d", &first[c][d]); printf("Enter the elements of second matrixn"); for (c = 0; c < m; c++) for (d = 0 ; d < n; d++) scanf("%d", &second[c][d]); printf("Sum of entered matrices:-n"); for (c = 0; c < m; c++) { for (d = 0 ; d < n; d++) { sum[c][d] = first[c][d] + second[c][d]; printf("%dt", sum[c][d]); } printf("n"); } return 0; }
  • 9. Sample Output Enter the number of rows and columns of matrix 2 2 Enter the elements of first matrix 1 2 3 4 Enter the elements of second matrix 5 6 2 1 Sum of entered matrices:- 6 8 5 5 Algorithm for matrix subtraction Step 1: Start Step 2: Declare matrix A[r][c] // Matrix 1; and matrix B[r][c] // Matrix 2; and matrix C[r][c]; r= no. of rows, c= no. of columns Step 3: Read r, c, A[][] and B[][] Step 4: Declare variable i=0, j=0 Step 5: Repeat until i < r 5.1: Repeat until j < c C[i][j]=A[i][j] - B[i][j] Set j=j+1 5.2: Set i=i+1 Step 6: C is the required matrix after subtraction Step 7: Stop
  • 10. C Program to perform matrix subtraction #include <stdio.h> int main() { int m, n, c, d, first[10][10], second[10][10], difference[10][10]; printf("Enter the number of rows and columns of matrixn"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrixn"); for (c = 0; c < m; c++) for (d = 0 ; d < n; d++) scanf("%d", &first[c][d]); printf("Enter the elements of second matrixn"); for (c = 0; c < m; c++) for (d = 0; d < n; d++) scanf("%d", &second[c][d]); printf("Difference of entered matrices:-n"); for (c = 0; c < m; c++) { for (d = 0; d < n; d++) { difference[c][d] = first[c][d] - second[c][d]; printf("%dt",difference[c][d]); } printf("n"); } return 0; }
  • 11. Sample Output Enter the number of rows and columns of matrix 2 2 Enter the elements of first matrix 4 3 2 1 Enter the elements of second matrix 1 2 1 1 Difference of entered matrices:- 3 1 1 0 Algorithm for matrix multiplication Step 1: Start Step 2: Declare matrix A[m][n] and matrix B[p][q] and matrix C[m][q] Step 3: Read m, n, p, q. Step 4: Now check if the matrix can be multiplied or not, if n is not equal to q matrix can't be multiplied and an error message is generated. Step 5: Read A[][] and B[][] Step 4: Declare variable i=0, k=0 , j=0 and sum=0 Step 5: Repeat Step until i < m 5.1: Repeat Step until j < q 5.1.1: Repeat Step until k < p Set sum= sum + A[i][k] * B[k][j] Set multiply[i][j] = sum; Set sum = 0 and k=k+1
  • 12. 5.1.2: Set j=j+1 5.2: Set i=i+1 Step 6: C is the required matrix. Step 7: Stop C Program to perform matrix multiplication //To multiply two matrices, the number of columns of the first matrix should be equal to the number of rows of the second #include <stdio.h> int main() { int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], multiply[10][10]; printf("Enter the number of rows and columns of first matrixn"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrixn"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &first[c][d]); printf("Enter the number of rows and columns of second matrixn"); scanf("%d%d", &p, &q); if ( n != p ) printf("Matrices with entered orders can't be multiplied with each other.n"); else { printf("Enter the elements of second matrixn"); for ( c = 0 ; c < p ; c++ )
  • 13. for ( d = 0 ; d < q ; d++ ) scanf("%d", &second[c][d]); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) { for ( k = 0 ; k < p ; k++ ) { sum = sum + first[c][k]*second[k][d]; } multiply[c][d] = sum; sum = 0; } } printf("Product of entered matrices:-n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) printf("%dt", multiply[c][d]); printf("n"); } } return 0; }
  • 14. Sample Output Enter the number of rows and columns of first matrix 3 3 Enter the elements of first matrix 1 2 0 0 1 1 2 0 1 Enter the number of rows and columns of second matrix 3 3 Enter the elements of second matrix 1 1 2 2 1 1 1 2 1 Product of entered matrices:- 5 3 4 3 3 2 3 4 5