2D Array basic input and output
#include<bits/stdc++.h>
#include<cstring>
#include<iostream>
using namespace std;
int main(){
//2D array
int row, col;
cout<<"Enter no of row: ";
cin>>row;
cout<<"Enter no of column: ";
cin>>col;
int arr[row][col];
//Input 2D array
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
cin>>arr[i][j];
}
}
//Output 2D array
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
cout<<arr[i][j];
}
cout<<endl;
}
return 0 ;
}
2D Array Searching a Matrix
#include<bits/stdc++.h>
#include<cstring>
#include<iostream>
using namespace std;
int main(){
//2D array
int row, col;
cout<<"Enter no of row: ";
cin>>row;
cout<<"Enter no of column: ";
cin>>col;
int arr[row][col];
//Input 2D array
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
cin>>arr[i][j];
}
}
//Output 2D array
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
cout<<arr[i][j];
}
cout<<endl;
}
//Search in Matrix
int x;
cout<<"Search any no in a matrix: ";
cin>>x;
bool flag=false;
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
if(arr[i][j]==x){
cout<<i<<" "<<j<<" "<<endl;
flag=true;
}
}
}
if(flag){
cout<<"Element is found: "<<endl;
}
else{
cout<<"Element not found: "<<endl;
}
return 0 ;
}
2D Array Spiral Order Matrix Traversal in C++
#include<bits/stdc++.h>
#include<cstring>
#include<iostream>
using namespace std;
int main(){
//2D array
int row, col;
cout<<"Enter no of row: ";
cin>>row;
cout<<"Enter no of column: ";
cin>>col;
int arr[row][col];
//Input 2D array
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
cin>>arr[i][j];
}
}
//Spiral order print
int rowStart=0, rowEnd=row-1, colStart=0, colEnd=col-1;
while(rowStart <= rowEnd && colStart <= colEnd){
//for Row Start
for(int col = colStart; col <=colEnd; col++){
cout<<arr[rowStart][col]<<" ";
}
rowStart++;
//for column end
for(int row = rowStart; row<=rowEnd; row++){
cout<<arr[row][colEnd]<<" ";
colEnd--;
//for row end
for(int col= colEnd; col>=colStart; col--){
cout<<arr[rowEnd][col]<<" ";
rowEnd--;
//for column Start
for(int row = rowEnd; row >= rowStart; row--){
cout<<arr[row][colStart]<<" ";
}
colStart++;
}
}
}
return 0 ;
}
2D Array Wave Print (Traversal) in C++
#include<iostream>
using namespace std;
int main(){
int n, m;
cout<<"Enter Row: ";
cin>>m;
cout<<"Enter column: ";
cin>>n;
int arr[n][m];
if((m>=1 && m<=10)&&((n>=1 && n<=10)))
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
for(int j=0;j<n;j=j+1)
{
if(j%2==0)
{
for(int i=0;i<m;i++)
{
cout<<arr[i][j]<<","<<endl;
}
}
else
{
for(int i=m-1;i>=0;i--)
{
cout<<arr[i][j]<<",";
}
}
}
}
cout<<"END";
}
2D Array Rotate image NxN array 90 degree in C++
#include<iostream>
using namespace std;
void rotate(int ar[][5],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i<j)
{
swap(ar[i][j],ar[j][i]);
}
}
}
}
int main()
{
int ar[5][5];
int n;
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>ar[i][j];
}
}
rotate(ar,n);
for(int i=n-1;i>=0;i--)
{
for(int j=0;j<n;j++)
{
cout<<ar[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
2D Array Staircase Search in C++
#include<bits/stdc++.h>
using namespace std;
void stairCaseSearch(vector<vector<int>> arr, int n, int el) {
int i=0, j=n-1;
while(i<n && j>=0) {
if(el==arr[i][j]) {
cout<<"Found at row = "<<i+1<<" and column = "<<j+1;
return ;
} else if(el<arr[i][j])
j--;
else if(el>arr[i][j])
i++;
}
cout<<"Not found";
return;
}
int main() {
int n, el;
cout<<"Enter no of row: ";
cin>>n;
cout<<"Enter no of column: ";
cin>>el;
vector<vector<int>> arr(n, vector<int> (n));
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
cin>>arr[i][j];
}
}
stairCaseSearch(arr,n,el);
return 0;
}
2D Array Read Character/Strings From User in C++
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main(){
char ch[1000][1000];
//Read a list of string and we will store them in a 2d character
int n;
cout<<"Enter no of line: ";
cin>>n;
cin.get();
for(int i=0; i<n; i++){
cout<<"Enter character or Words: ";
cin.getline(ch[i], 1000);
}
for(int j=0; j<n; j++){
cout<<ch[j]<<endl;
}
return 0 ;
}