(19). 9.1 TWO DIMENSIONAL ARRAYS | 2‐D ARRAYS | - anishsingh90/Data_Structure_And_Algorithm_In_Cpp_github.io GitHub Wiki
//2-D ARRAY PROGRAM. #include using namespace std;
int main(){ int n; cout << "Enter the size of row: "; cin >> n; int m; cout << "Enter the size of column: "; cin >> m;
int arr[n][m];
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin >> arr[i][j];
}
}
cout << "\n\n";
cout << "Matrix is:\n";
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cout << arr[i][j] << " ";
}
cout << "\n";
}
return 0;
}
/* OUTPUT: Enter the size of row: 3 Enter the size of column: 3 7 6 4 8 7 5 8 6 3
Matrix is: 7 6 4 8 7 5 8 6 3 */
//SEARCHING ARRAY #include using namespace std;
int main(){ int n; cout << "Enter the size of row: "; cin >> n; int m; cout << "Enter the size of column: "; cin >> m;
int arr[n][m];
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin >> arr[i][j];
}
}
cout << "\n\n";
cout << "Matrix is:\n";
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cout << arr[i][j] << " ";
}
cout << "\n";
}
int x;
cout << "Enter the element : ";
cin >> x;
bool flag = false;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(arr[i][j]==x){
cout << i <<" " << j << " ";
flag = true;
}
}
}
if(flag){
cout << "Element is found\n";
}
else{
cout << "Element is not found\n";
}
return 0;
}
/* OUTPUT: Enter the size of row: 2 Enter the size of column: 2 7 4 8 6
Matrix is: 7 4 8 6 Enter the element : 4 0 1 Element is found */
//SPIRAL ORDER MATRIX TRAVERSAL #include using namespace std;
int main(){ int n; cout << "Enter the number of row: "; cin >> n; int m; cout << "Enter the number of column: "; cin >> m;
int a[n][m];
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin >> a[i][j];
}
}
//SPIRAL ORDER PRINT
int row_start = 0, row_end = n-1, column_start = 0, column_end = m-1;
while(row_start <= row_end && column_start <= column_end)
{
//for row_start
for(int col = column_start; col <= column_end; col++){
cout << a[row_start] [col] << " ";
}
row_start++;
//for column end
for(int row = row_start; row <= row_end; row++){
cout << a[row][column_end] << " ";
}
column_end--;
//for row_end
for(int col = column_end; col >= column_start; col--){
cout << a[row_end][col]<<" ";
}
row_end--;
//for column_start
for(int row = row_end; row >= row_start; row--){
cout << a[row][column_start] << " ";
}
column_start++;
}
return 0;
}
/* OUTPUT: Enter the number of row: 5 Enter the number of column: 5 7 5 9 7 4 7 4 9 2 8 8 5 9 1 2 6 9 4 1 9 7 4 9 3 8 7 5 9 7 4 8 2 9 8 3 9 4 7 6 8 7 4 9 2 1 1 4 9 5 9 */