Module 17
Module 17
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.
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;
return 0;
}