0% found this document useful (0 votes)
12 views11 pages

Module 17

The document outlines the objectives for a discussion on C++ programs utilizing multidimensional arrays, specifically rectangular arrays. It provides examples of how to declare and initialize these arrays, as well as how to access their elements. Additionally, it includes a sample C++ program that allows users to input row and column numbers to access specific values from a predefined array of grades.
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)
12 views11 pages

Module 17

The document outlines the objectives for a discussion on C++ programs utilizing multidimensional arrays, specifically rectangular arrays. It provides examples of how to declare and initialize these arrays, as well as how to access their elements. Additionally, it includes a sample C++ program that allows users to input row and column numbers to access specific values from a predefined array of grades.
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/ 11

Objectives:

At the end of the discussion, the students should be able to;


1. Trace C++ programs with Multidimensional Arrays.
an array of an array, also known as rectangular arrays.
Type name[row][column]
 Type –data type
 Name – identifier
 Row – number of rows in an array
 Column – number of columns in an array

Example on how to declare:


int a[2][3]; (row*column) or also known as matrix
2 means an array has 2 rows
3 columns
Example:
int a[2][3];
a[0][0] = 5;
a[0][1] = 19;
a[0][2] = 3; Row 0 5 19 3

a[1][0] = 22; Row 1 22 -8 10

a[1][1] = -8; 0 1 2
a[1][2] = 10;

# of # of
Row column
Example:
int a[3][3]; [row][column]
a[1][2] = 5;
a[0][0] = 19;
Row [0] 19 3
a[0][2] = 3;
Row [1] 22 -8 5
a[1][0] = 22; Row [2] 10 10 10
a[1][1] = -8; Col [0] Col [1] Col[2]
a[2][1] = 10;
a[2][2] = 10;
a[2][0] = 10;
int test[2][3] = { {2, 4, 5}, {9, 0, 19}}; or
 int a[ ][3] = { { 5, 19, 3 }, { 22, -8, 10 } };
 has 2 rows and 3 columns, which is why we have two rows of elements
with 3 elements each.

Col 0 Col 1 Col 2

Row 0 2 4 5
Row 1 9 0 19
Example:
int value [3][4] = { {3,8,6,4}, {81,78,90,7}, {12, 21, 62, 19} }
value [2][2] returns 62
value [0][3] =4
value [2][1] = 21
value [1][2]=90 Row [0] 3 8 6 4
value [2][3]=19 Row [1] 81 78 90 7
value [1][1]=78 Row [2] 12 21 62 19
Col [0] Col [1] Col [2] Col [3]
value [2][2]=62
value [1][3]=7
cout << "Enter the elements of the array:" << endl;
INITIALIZING 3D ARRAY for (int i = 0; i < rows; i++)

#include <iostream> {
for (int j = 0; j < cols; j++)
using namespace std; {
cout << "Element [" << i << "][" << j << "]: ";
cin >> arr[i][j];
int main() }
{ }

int rows, cols; cout << "The 2D array is:" << endl;
cout << "Enter the number of rows: "; for (int i = 0; i < rows; i++)
{
cin >> rows;
for (int j = 0; j < cols; j++)
cout << "Enter the number of columns: "; {
cout << arr[i][j] << “\t";
cin >> cols;
}
cout << endl;
}
int arr[rows][cols];
return 0;
}
1. Create a C++ program that allows the user to choose what
element to access from the following grades;

85 54 96 78 95
94 80 77 64 24
84 75 66 60 55
#include <iostream>
using namespace std;

int main()
{
int row, col, array[3][5]={{85,54,96,78,95},{94,80,77,64,24},{84,75,66,60,55}};
cout << "Enter the row number: ";
cin >> row;
cout << "Enter the column number: ";
cin >> col;

cout<<"ACCESS VALUE: "<<array[row][col];

return 0;
}

You might also like