0% found this document useful (0 votes)
40 views12 pages

2D-Arrays

2D arrays allow data to be organized into rows and columns for representing tables, matrices, and other two-dimensional data structures. They are declared with two indices, one for rows and one for columns, and elements are accessed using both indices. Traversing a 2D array requires two nested for loops, the outer iterating over rows and the inner over columns. Functions can operate on 2D arrays to perform operations like reading, printing, adding, multiplying, transposing, and computing diagonal sums of matrices. Examples demonstrate creating identity, reverse identity, upper/lower triangle, and student grade matrices.

Uploaded by

asakr8481
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)
40 views12 pages

2D-Arrays

2D arrays allow data to be organized into rows and columns for representing tables, matrices, and other two-dimensional data structures. They are declared with two indices, one for rows and one for columns, and elements are accessed using both indices. Traversing a 2D array requires two nested for loops, the outer iterating over rows and the inner over columns. Functions can operate on 2D arrays to perform operations like reading, printing, adding, multiplying, transposing, and computing diagonal sums of matrices. Examples demonstrate creating identity, reverse identity, upper/lower triangle, and student grade matrices.

Uploaded by

asakr8481
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/ 12

2D- Arrays

Programming II
Dr. Zeinab Abd El Haliem
Motivation
• Represent table data / matrices
• Array cells are referenced using two indices
• First index for rows
• Second index for columns
Syntax
• It is a collection of data elements of same data type arranged in rows
and columns (that is, in two dimensions).
• Declaration
• Type name[# of rows][# of columns]
• For example: float grades[3][4] declares a 2D array having 3x4 cells (3
rows and 4 columns)
• Access
• name[i][j] accesses the value at the intersection of row i and column j
• Index numbers start at zero
Traversal
• Traversing a 2D array in order to read or store a value
requires 2 for loops
• One loop iterates over the rows
• The other loop iterates over the columns
• Example : filling a 10x5 array from the keyboard
for(int row = 0; row < 10; row++)
for(int col = 0; col < 5; col++)
cin >> grades[row][col];
2D Arrays & Function
• Function to Read A matrix
void Read(int A[][20], int N, int M)
{
for(int R=0;R<N;R++)
{
for(int C=0;C<M;C++)
{
cin>>A[R][C];
}
}
}
2D Arrays & Function (Cont.)
• Function to Print A matrix
void Print(int A[][20], int N, int M)
{
for(int R=0;R<N;R++)
{
for(int C=0;C<M;C++)
{
cout << A[R][C];
}
}
}
2D Arrays & Function (Cont.)
• Function to Add Two Matrices A & B
void ADD(int A[][20], int B[][20], int Z[][20], int N, int M)
{
for(int R=0;R<N;R++)
{
for(int C=0;C<M;C++)
{
Z[R][C]=A[R][C]+ B [R][C];
}
}
}
2D Arrays & Function (Cont.)
• Function To Multiply Two Matrices.

void Multiply(int A[][20], int B[][20], int Z[][20],int N, int L, int M)


{
for(int R=0;R<N;R++)
{
for(int C=0;C<M;C++)
{
Z[R][C]=0;
for(int T=0;T<L;T++)
{
Z[R][C]+=A[R][T]*B[T][C];
}
}
}
}
Transpose a Matrix
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];
}
}
}
Summation of Diagonal elements
of a Square Matrix
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];
}
}
Examples
• Write a C++ program that creates the identity matrix of size
N. (A square matrix with 1 on the diagonal and zero
elsewhere.)
• Write a C++ program that creates the reverse identity matrix
of size N. (A square matrix with 1 on the reverse diagonal
and zero elsewhere.)
• Write a program that creates an upper-triangle binary matrix.
• Write a program that creates an lower-triangle binary matrix.
Examples
• Write a function that takes a 2D array of student x module
grades and computes the average grades of each student.
• Write a function that takes a 2D array of student x module
grades and computes the average grades for each module.
• Write a function that takes a 2D array of student x module
grades and displays the grades along with average values per
student and per module in a tabulated format

You might also like