2D Arrays in C++: Dr. Mohammad Ghoniem
2D Arrays in C++: Dr. Mohammad Ghoniem
Motivation
Represent table data / matrices Array cells are referenced using two indices
Syntax
Declaration
Access
name[i][j] accesses the value at the intersection of row i and column j Index numbers start at zero
Traversal
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];
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