C++ 2D Array |
C++ 2D Array
2D are in form of matrx. Before moving on 2D array, you should know about 1D Array (Simple Array)
More info
2D array have row and column. In static 2D array, we set row and column of array. 2D array can be in same row and column or different. For example,
- Matrix 4x4
- Matrix 2x4
- Matrix 4x2
- Matrix 1x2
Matrix 4x4
This type of array has 4 row and 4 column. Total element store in this array are 16 (Total elements of 2D array are count row multiply by column). This is square matrix means both row and column are same.
Matrix 2x4
This type of array has 2 row and 4 column. Total element store in this array are 8 (Total elements row * column = 2 * 4 => 8). This is rectangle type matrix means rows are different than column.
Matrix 4x2
This type of array has 4 row and 2 column. Total element store in this array are 8 (Total elements row * column = 4 * 2 => 8). This is also rectangle type matrix means both row and column are not same.
Matrix 1x2
This type of array has 1 row and 2 column. Total element store in this array are 2 (Total elements row * column = 1 * 2 => 2). This array will look like 1D array but programmatically it is 2D array
C++ 2D Array Syntax
dataType nameOfArray[row][column];
dataTypes are (int,float,double,char etc). nameOfArray: any name you can write according to rules. row and column should be an integer. lets look following examples, how to declere and initlize following type of 2D array with int data type.
-
Matrix 4 X 4
int square_matrix[4][4];
or
int square_matrix[4][4] = {{1,2,3,4},{4,5,6,7},{7,8,9,10},{10,11,12,13}}; -
Matrix 2 X 4
int rectangle_matrix[2][4];
or
int rectangle_matrix[2][4] = {{1,2,3,4},{4,5,6,7}}; -
Matrix 4 X 2
int rectangle_matrix[4][2];
or
int rectangle_matrix[4][2] = {{1,2},{3,4},{5,6},{7,8}}; -
Matrix 1 X 2
int matrix[1][2];
or
int matrix[1][2] = {{1,2}};
C++ 2D Array Methods
Basic method, insert, getValue, getIndex, display, getMin, getMax, diagonalCompare etc
Method and purpose.
-
insert()
This method used to take value from user.
-
getValue()
This method used to value from specific index. it has two parameters one of them is row index while other in column index.
-
getIndex()
This method used to get index of specific value.
-
display()
This method used to print 2D array value.
-
getMin()
This method return minimum value from array.
-
getMax()
This method return maximum value from array
-
diagonalCompare()
This method used to check whether left or right diagonal of matrix are same or not. It takes a parameter which decide side of matrix. if parameter value is 0 then left side diagonal check and if parameter value is 1 then right side diagonal will check.
Outcome
void insert()
{
for (int row = 0; row < ROW; row++)
{
for (int column = 0; column < COLUMN; column++)
{
cout << "Enter value at row " << row + 1 << " column " << column + 1 << " : ";
cin >> matrix[row][column];
}
}
}
int getValue(int rowIndex, int columnIndex)
{
if (rowIndex < ROW && columnIndex < COLUMN)
return matrix[rowIndex][columnIndex];
else
return NULL;
}
struct Matrix {
int rowIndex = 0;
int columnIndex = 0;
void show()
{
cout << rowIndex << "X" << columnIndex << endl;
}
};
Matrix getIndex(int value)
{
Matrix index;
for (int row = 0; row < ROW; row++)
{
for (int column = 0; column < COLUMN; column++)
{
if (matrix[row][column] == value)
{
index.rowIndex = row;
index.columnIndex = column;
}
}
}
return index;
}
void display()
{
for (int row = 0; row < ROW; row++)
{
for (int column = 0; column < COLUMN; column++)
{
cout << matrix[row][column] << " ";
}
cout << endl;
}
}
int getMin()
{
int value = matrix[0][0];
for (int row = 0; row < ROW; row++)
{
for (int column = 0; column < COLUMN; column++)
{
if (value >= matrix[row][column])
value = matrix[row][column];
}
}
return value;
}
int getMax()
{
int value = matrix[0][0];
for (int row = 0; row < ROW; row++)
{
for (int column = 0; column < COLUMN; column++)
{
if (value <= matrix[row][column])
value = matrix[row][column];
}
}
return value;
}
bool diagonalCompare(int side)
{
bool check = false;
int counter = 0;
if (ROW == COLUMN)
{
if (side == 0)
{
int value = matrix[0][0];
for (int index = 0; index < ROW; index++)
{
if (value == matrix[index][index])
counter++;
}
}
if(side==1)
{
int value = matrix[0][COLUMN-1];
counter = 0;
for(int row = 0; row < ROW; row++)
{
if (value == matrix[row][(COLUMN-1)-row])
counter++;
}
}
if (counter == ROW)
check = true;
}
return check;
}
void main() {
cout <<"Get value index[3][2]: " << getValue(3, 2) << endl;
Matrix index = getIndex(12);
display();
cout << "Get min : " << getMin() << endl;
cout << "Get max : " << getMax() << endl;
if (diagonalCompare(1))
cout << "Diagnols are same" << endl;
else
cout << "Dignols are not same" << endl;
cout << "Find index of 12 value ";
index.show();
}
C++ 2D Array |
0 Comments
Thanks