0% found this document useful (0 votes)
27 views

2D Arrays in C++: Dr. Mohammad Ghoniem

This document discusses 2D arrays in C++. It explains that 2D arrays can be used to represent table data and matrices, with each array cell referenced using two indices - a row index and a column index. It provides the syntax for declaring and accessing 2D arrays, including using two for loops to traverse the arrays. Examples are given of programs to create identity matrices and upper/lower triangle matrices from 2D arrays. Functions are also described that can analyze 2D arrays of student grades by computing average grades for each student and module or displaying grades and averages in a table.

Uploaded by

Michael Nasief
Copyright
© Attribution Non-Commercial (BY-NC)
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)
27 views

2D Arrays in C++: Dr. Mohammad Ghoniem

This document discusses 2D arrays in C++. It explains that 2D arrays can be used to represent table data and matrices, with each array cell referenced using two indices - a row index and a column index. It provides the syntax for declaring and accessing 2D arrays, including using two for loops to traverse the arrays. Examples are given of programs to create identity matrices and upper/lower triangle matrices from 2D arrays. Functions are also described that can analyze 2D arrays of student grades by computing average grades for each student and module or displaying grades and averages in a table.

Uploaded by

Michael Nasief
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 6

2D Arrays in C++

Dr. Mohammad Ghoniem

Motivation

Represent table data / matrices Array cells are referenced using two indices

First index for rows Second index for columns

Syntax

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];

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