SlideShare a Scribd company logo
www.cppforschool.com
Two Dimensional Array
It is a collection of data elements of same data type arranged in rows and
columns (that is, in two dimensions).
Declaration of Two-Dimensional Array
Type arrayName[numberOfRows][numberOfColumn];
For example,
int Sales[3][5];
Initialization of Two-Dimensional Array
An two-dimensional array can be initialized along with declaration. For two-
dimensional array initialization, elements of each row are enclosed within curly
braces and separated by commas. All rows are enclosed within curly braces.
int A[4][3] = {{22, 23, 10},
{15, 25, 13},
{20, 74, 67},
{11, 18, 14}};
Referring to Array Elements
To access the elements of a two-dimensional array, we need a pair of indices:
one forthe row position and one for the column position. The format is as simple
as:
name[rowIndex][columnIndex].
Examples:
cout << A[1][2]; //print an array element
A[1][2] = 13; // assign value to an array element
cin >> A[1][2]; //input element
Using Loop to input an Two-Dimensional Array from user
int mat[3][5], row, col ;
for (row = 0; row < 3; row++)
for (col = 0; col < 5; col++)
cin >> mat[row][col];
Arrays as Parameters
Two-dimensional arrays can be passed as parameters to a function, and they
are passed by reference. When declaring a two-dimensional array as a formal
parameter, we can omit the size of the first dimension, but not the second; that
is, we must specify the number of columns. For example:
void print(int A[][3], int N, int M)
In order to pass to this function an array declared as:
int arr[4][3];
we need to write a call like this:
print(arr);
Here is a complete example:
#include <iostream>
using namespace std;
void print(int A[][3], int N, int M)
{
for (R = 0; R < N; R++)
for (C = 0; C < M; C++)
cout << A[R][C];
}
int main ()
{
int arr[4][3] ={{12, 29, 11},
{25, 25, 13},
{24, 64, 67},
{11, 18, 14}};
print(arr,4,3);
return 0;
}
Function to read the array A
void Read(int A[][20], int N, int M)
{
for(int R = 0; R < N; R++)
for(int C = 0; C < M; C++)
{
cout << "(R<<','<<")?";
cin >> A[R][C];
}
}
Function to display content of a two dimensional array A
void Display(int A[][20], int N, int M)
{
for(int R = 0;R < N; R++)
{
for(int C = 0; C < M; C++)
cout << setw(10) << A[R][C];
cout << endl;
}
}
Function to find the sum of two dimensional arrays A and B
void Addition(int A[][20], int B[][20], int N, int M)
{
for(int R = 0; R < N; R++)
for(int C = 0;C < M; C++)
C[R][C] = A[R][C] + B[R][C];
}
Function to multiply two dimensional arrays A and B of
order NxL and LxM
void Multiply(int A[][20], int B[][20], int C[][20],int N, int L,
int M)
{
for(int R = 0; R < N; R++)
for(int C = 0; C < M; C++)
{
C[R][C] = 0;
for(int T = 0; T < L; T++)
C[R][C] += A[R][T] * B[T][C];
}
}
Function to find & display sum of rows & sum of cols. of array A
void SumRowCol(int A[][20], int N, int M)
{
for(int R = 0; R < N; R++)
{
int SumR = 0;
for(int C = 0; C < M; C++)
SumR += A[R][C];
cout << "Row("<<R<<")=" << SumR << endl;
}
for(int R = 0;R < N; R++)
{
int SumR = 0;
for(int C = 0; C < M; C++)
SumR += A[R][C];
cout << "Row("<<R<<")=" << SumR << endl;
}
}
Function to find sum of diagonal elements of a square matrix A
void Diagonal(int A[][20], int N, int &Rdiag, int &LDiag)
{
for(int I = 0, Rdiag = 0; I < N; I++)
Rdiag += A[I][I];
for(int I = 0, Ldiag = 0; I < N; I++)
Ldiag += A[N-I-1][I];
}
Function to find out transpose of a two dimensional array A
void Transpose(int A[][20], int B[][20], int N, int M)
{
for(int R = 0; R < N; R++)
for(int C = 0;C < M; C++)
B[R][C] = A[C][R];
}

More Related Content

PDF
C++ ARRAY WITH EXAMPLES
PPTX
Pf presntation
PPTX
Lecture 1 mte 407
PPTX
Lecture 1 mte 407
PDF
Scala collection methods flatMap and flatten are more powerful than monadic f...
PPTX
Warehouse layout design
PPT
Module 2 topic 2 notes
PPT
Arrays
C++ ARRAY WITH EXAMPLES
Pf presntation
Lecture 1 mte 407
Lecture 1 mte 407
Scala collection methods flatMap and flatten are more powerful than monadic f...
Warehouse layout design
Module 2 topic 2 notes
Arrays

What's hot (19)

PDF
R Data Visualization: Learn To Add Text Annotations To Plots
PDF
Functor Laws
PPTX
Asymptotic Notation
PDF
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
PDF
Lecture 1 python arithmetic (ewurc)
PPT
Thesis PPT
PPT
ML: A Strongly Typed Functional Language
PDF
Compiling fµn language
PPT
Tools for research plotting
PDF
Data Visualization With R: Learn To Modify Color Of Plots
PDF
How You Can Read ISIN_
PPTX
Booth’s algorithm.(a014& a015)
DOCX
Experement no 6
PPT
4.5 sec and csc worked 3rd
PPT
4.5 tan and cot.ppt worked
PPT
Presentation.mehr
DOC
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
PDF
Applications of stack
R Data Visualization: Learn To Add Text Annotations To Plots
Functor Laws
Asymptotic Notation
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Lecture 1 python arithmetic (ewurc)
Thesis PPT
ML: A Strongly Typed Functional Language
Compiling fµn language
Tools for research plotting
Data Visualization With R: Learn To Modify Color Of Plots
How You Can Read ISIN_
Booth’s algorithm.(a014& a015)
Experement no 6
4.5 sec and csc worked 3rd
4.5 tan and cot.ppt worked
Presentation.mehr
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Applications of stack
Ad

Viewers also liked (20)

PPTX
Texas STaR Chart
PDF
Why Free Enterprise is Regulated Enterprise
PDF
Chapter 11 Function
PDF
An Examination of Health Care Quality--with a focus on physician rendered care
PDF
Chapter 3 - Variable Memory Concept
PDF
Chapter 9 - Loops in C++
PDF
Cbse question paper class_xii_paper_2000
PDF
Chapter 7 - Input Output Statements in C++
PDF
Cbse question-paper-computer-science-2009
PDF
Chapter28 data-file-handling
PPT
Chapter 11
PPT
Ch5 array nota
PPT
Two dimensional array
PPTX
2 d array
PPT
Inheritance polymorphism-in-java
PPT
C++ questions and answers
PDF
CBSE Question Paper Computer Science with C++ 2011
PDF
Chpater29 operation-on-file
PPTX
Arrays in Java
PDF
Revision notes for exam 2011 computer science with C++
Texas STaR Chart
Why Free Enterprise is Regulated Enterprise
Chapter 11 Function
An Examination of Health Care Quality--with a focus on physician rendered care
Chapter 3 - Variable Memory Concept
Chapter 9 - Loops in C++
Cbse question paper class_xii_paper_2000
Chapter 7 - Input Output Statements in C++
Cbse question-paper-computer-science-2009
Chapter28 data-file-handling
Chapter 11
Ch5 array nota
Two dimensional array
2 d array
Inheritance polymorphism-in-java
C++ questions and answers
CBSE Question Paper Computer Science with C++ 2011
Chpater29 operation-on-file
Arrays in Java
Revision notes for exam 2011 computer science with C++
Ad

Similar to Chapter13 two-dimensional-array (20)

PPTX
2- Dimensional Arrays
PDF
Arrays and library functions
PPTX
Two dimensional arrays
PPT
Fp201 unit4
PDF
Chapter12 array-single-dimension
PPT
2DArrays.ppt
PPTX
Structured data type
PDF
C++ Nested loops, matrix and fuctions.pdf
PPTX
ARRAYS.pptx
PPT
Lecture#5-Arrays-oral patholohu hfFoP.ppt
PPTX
Arrays matrix 2020 ab
PPTX
Data structure array
PPTX
Array and string in C++_093547 analysis.pptx
PPTX
C-Programming Arrays.pptx
PPTX
C-Programming Arrays.pptx
PPTX
Yash Bhargava Array In programming in C
PDF
05_Arrays C plus Programming language22.pdf
PPTX
Arrays
PPT
ReviewArrays.ppt
PDF
A02
2- Dimensional Arrays
Arrays and library functions
Two dimensional arrays
Fp201 unit4
Chapter12 array-single-dimension
2DArrays.ppt
Structured data type
C++ Nested loops, matrix and fuctions.pdf
ARRAYS.pptx
Lecture#5-Arrays-oral patholohu hfFoP.ppt
Arrays matrix 2020 ab
Data structure array
Array and string in C++_093547 analysis.pptx
C-Programming Arrays.pptx
C-Programming Arrays.pptx
Yash Bhargava Array In programming in C
05_Arrays C plus Programming language22.pdf
Arrays
ReviewArrays.ppt
A02

More from Deepak Singh (19)

PDF
Computer networks - CBSE New Syllabus (083) Class - XII
PDF
Chapter27 polymorphism-virtual-function-abstract-class
PDF
Chapter26 inheritance-ii
PDF
Chapter25 inheritance-i
PDF
Chapter24 operator-overloading
PDF
Chapter23 friend-function-friend-class
PDF
Chapter22 static-class-member-example
PDF
Chapter21 separate-header-and-implementation-files
PDF
Chapter20 class-example-program
PDF
Chapter19 constructor-and-destructor
PDF
Chapter18 class-and-objects
PDF
Chapter17 oop
PDF
Chapter16 pointer
PDF
Chapter15 structure
PDF
Chapter 10 Library Function
PDF
Chapter 8 - Conditional Statement
PDF
Chapter 5 - Operators in C++
PDF
Chapter 2 - Structure of C++ Program
PDF
Computer science-2010-cbse-question-paper
Computer networks - CBSE New Syllabus (083) Class - XII
Chapter27 polymorphism-virtual-function-abstract-class
Chapter26 inheritance-ii
Chapter25 inheritance-i
Chapter24 operator-overloading
Chapter23 friend-function-friend-class
Chapter22 static-class-member-example
Chapter21 separate-header-and-implementation-files
Chapter20 class-example-program
Chapter19 constructor-and-destructor
Chapter18 class-and-objects
Chapter17 oop
Chapter16 pointer
Chapter15 structure
Chapter 10 Library Function
Chapter 8 - Conditional Statement
Chapter 5 - Operators in C++
Chapter 2 - Structure of C++ Program
Computer science-2010-cbse-question-paper

Chapter13 two-dimensional-array

  • 1. www.cppforschool.com Two Dimensional Array It is a collection of data elements of same data type arranged in rows and columns (that is, in two dimensions). Declaration of Two-Dimensional Array Type arrayName[numberOfRows][numberOfColumn]; For example, int Sales[3][5]; Initialization of Two-Dimensional Array An two-dimensional array can be initialized along with declaration. For two- dimensional array initialization, elements of each row are enclosed within curly braces and separated by commas. All rows are enclosed within curly braces. int A[4][3] = {{22, 23, 10}, {15, 25, 13}, {20, 74, 67}, {11, 18, 14}};
  • 2. Referring to Array Elements To access the elements of a two-dimensional array, we need a pair of indices: one forthe row position and one for the column position. The format is as simple as: name[rowIndex][columnIndex]. Examples: cout << A[1][2]; //print an array element A[1][2] = 13; // assign value to an array element cin >> A[1][2]; //input element Using Loop to input an Two-Dimensional Array from user int mat[3][5], row, col ; for (row = 0; row < 3; row++) for (col = 0; col < 5; col++) cin >> mat[row][col]; Arrays as Parameters Two-dimensional arrays can be passed as parameters to a function, and they are passed by reference. When declaring a two-dimensional array as a formal parameter, we can omit the size of the first dimension, but not the second; that is, we must specify the number of columns. For example: void print(int A[][3], int N, int M) In order to pass to this function an array declared as: int arr[4][3]; we need to write a call like this: print(arr);
  • 3. Here is a complete example: #include <iostream> using namespace std; void print(int A[][3], int N, int M) { for (R = 0; R < N; R++) for (C = 0; C < M; C++) cout << A[R][C]; } int main () { int arr[4][3] ={{12, 29, 11}, {25, 25, 13}, {24, 64, 67}, {11, 18, 14}}; print(arr,4,3); return 0; } Function to read the array A void Read(int A[][20], int N, int M) { for(int R = 0; R < N; R++) for(int C = 0; C < M; C++) { cout << "(R<<','<<")?"; cin >> A[R][C]; } }
  • 4. Function to display content of a two dimensional array A void Display(int A[][20], int N, int M) { for(int R = 0;R < N; R++) { for(int C = 0; C < M; C++) cout << setw(10) << A[R][C]; cout << endl; } } Function to find the sum of two dimensional arrays A and B void Addition(int A[][20], int B[][20], int N, int M) { for(int R = 0; R < N; R++) for(int C = 0;C < M; C++) C[R][C] = A[R][C] + B[R][C]; } Function to multiply two dimensional arrays A and B of order NxL and LxM void Multiply(int A[][20], int B[][20], int C[][20],int N, int L, int M) { for(int R = 0; R < N; R++) for(int C = 0; C < M; C++) { C[R][C] = 0; for(int T = 0; T < L; T++) C[R][C] += A[R][T] * B[T][C]; } }
  • 5. Function to find & display sum of rows & sum of cols. of array A void SumRowCol(int A[][20], int N, int M) { for(int R = 0; R < N; R++) { int SumR = 0; for(int C = 0; C < M; C++) SumR += A[R][C]; cout << "Row("<<R<<")=" << SumR << endl; } for(int R = 0;R < N; R++) { int SumR = 0; for(int C = 0; C < M; C++) SumR += A[R][C]; cout << "Row("<<R<<")=" << SumR << endl; } } Function to find sum of diagonal elements of a square matrix A void Diagonal(int A[][20], int N, int &Rdiag, int &LDiag) { for(int I = 0, Rdiag = 0; I < N; I++) Rdiag += A[I][I]; for(int I = 0, Ldiag = 0; I < N; I++) Ldiag += A[N-I-1][I]; } Function to find out transpose of a two dimensional array A void Transpose(int A[][20], int B[][20], int N, int M) { for(int R = 0; R < N; R++) for(int C = 0;C < M; C++) B[R][C] = A[C][R]; }