SlideShare uma empresa Scribd logo
1 de 14
Baixar para ler 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

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (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)
 

Semelhante a 2D array

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
 

Semelhante a 2D array (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
 

Mais de 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
 

Último

COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 

Último (20)

COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 

2D array

  • 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