9.1 Two Dimensional Arrays | 2D Arrays | C++ Placement Course

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Due to some technical glitch, we were not able to upload any video from the last 2 days.

ApnaCollegeOfficial
Автор

In spiral order traversal, those who are getting 29 twice in the output, actually you'll have to add a check before the inner last two for loops to prevent unnecessary iterations.
Code:

```

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main()
{
int n, m;
cin >> n >> m;
int arr[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> arr[i][j];
}
}

// spiral order traversal
int row_start = 0;
int col_start = 0;
int row_end = n - 1;
int col_end = m - 1;
int cnt = n * m; // To count total elements left for printing

while (row_start <= row_end && col_start <= col_end)
{
if (cnt != 0)
{
// printing the start row from left to right
for (int col = col_start; col <= col_end; col++)
{
cout << arr[row_start][col] << " ";
cnt--;
}
row_start++;

// printing the end col from start to end
for (int row = row_start; row <= row_end; row++)
{
cout << arr[row][col_end] << " ";
cnt--;
}
col_end--;

// printing the end row from right to left
// Add a check before the inner loops to prevent unnecessary iterations
if (row_start <= row_end)
{
for (int col = col_end; col >= col_start; col--)
{
cout << arr[row_end][col] << " ";
cnt--;
}
}
row_end--;

// printing the start col from end to start
// Add a check before the inner loops to prevent unnecessary iterations
if (col_start <= col_end)
{
for (int row = row_end; row >= row_start; row--)
{
cout << arr[row][col_start] << " ";
cnt--;
}
}
col_start++;
}
}

return 0;
}
```

vidhanpatni
Автор

Corrected last code(spiral traversal)

#include<iostream>
using namespace std;
int main()
{
int n, m;
cin>>n>>m;
int a[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
}
}
int row_start=0, col_start=0, row_end=n-1, col_end=m-1;
while(row_start<=row_end && col_start<=col_end)
{
for(int
{
cout<<a[row_start][col]<<" ";
}
row_start++;

for(int
{
cout<<a[row][col_end]<<" ";
}
col_end--;
if(row_start<=row_end)
{

for(int
{
cout<<a[row_end][col]<<" ";
}
}
row_end--;

if(col_start<=col_end)
{

for(int
{
cout<<a[row][col_start]<<" ";
}
}
col_start++;
}
return 0;
}

anchalmehandiratta
Автор

yes i also got the repeat of 29 using the original code so a simple solution to this is use a count that is row*column and — it for every element printed and if count==0 stop loop break out and prevent any of the inner loops from running ! did the job !

parthapratimghose
Автор

Thank you Apna College Team for making me understand the code so well. I am glad I found your channel. You guys are doing a great job. More power to you.

aishwaryamishra
Автор

This is the first lecture from this channel that I didn't understand😢😢 otherwise all the videos are very helpful for me but this is...😢😢..😢😢

Ankit
Автор

There is a bug in Spiral matrix traverse code. You have to also use an if statement " if(row_start<=row_end && column_start<=column_end) " for each FOR loop in the while loop, otherwise it will only work for square matrices.

ProductionReadyApp
Автор

I am loving these videos seriously! Finally I am starting to spend the lockdown productively!

devanshsingh
Автор

Karat karat abhayas te janmat hoye sujan...rasri avat jat te sil par pade Nishan..🔥✌

pranjalbajpai
Автор

Thank you apna college team, following this amazing course has been a great experience, however some videos contains advanced level questions but I think they are required to boost up your level and thinking ability.
Once again thanking you all for this amazing free content🙏

Special_moment_
Автор

Why both the last videos are not available Aman Bhaiya? Any clue??

adityabhansinghrathore
Автор

2D array declaration -\ 0:15
Taking and Printing Output -\ 2:03
Searching a Matrix -\ 5:15
Spiral Order Matrix Traversal -\ 7:45

shreyaschavhan
Автор

2D array declare a[n][m] se krte hai aur n rows ke liye hota hai aur m columns ke liye
input lene ke liye for(i=0 se n-1) ke andar for(j=o se m-1) me cin krenge similar for cout aur simple search ke liye bhi wahi karenge
Now printing in spiral order me ham 4 parameters lete hai
row_start=0
row_end= n-1
col_start=0 aur
col_end= m-1

while(Rs<=Re && Cs<=Ce){
isme 4 for loops laga ke aachese print kiya jeyega
}

developerNik
Автор

@Apna College

in the last code, the final output must also contain '29' after '30' . i code the same program in my lappy and it showed 29 after 30 cuz the "third" for loop inside while loop will execute one more time as the condition would hold true in the THIRD for loop. SEE BELOW

for( int row=2; row>=2; row - -)
{
cout<<a[2][2]<<" ";
}


this code will display 29 as the last number after 30.

prajjawalkushwaha
Автор

We tremendously appreciate aman bhaiya..You are doing a huge help to us thank you thank you very much..Bhaya we are eagerly waiting for web dev courses..

ashokdalai
Автор

Bhaiya,
At the end the video, you told us to practice Different types of programming and learn Algorithms.

It'll be great if you recommend some sources to practice from.

soumilbanik
Автор

Toh is video series k 2 D array tak pahuchane k baad views bahut kam dikh rahe hai... Agar aap yaha tak pahuche toh well done..👍

vishesh_pandey
Автор

Bhaiya, Please turn on the monetization. It's a humble request.

ShubhamSharma-qvol
Автор

Extra must know points.

You can not directly initialise 2D array as we do in simple array. So we can use memset to initialise it quickly.

You can not pass 2d array address to function and use it.

Ex.
void fun(int & matrix[][] )
{
}

C++ don’t allow this. So if you want to give reference of 2D array than use different way to initialise it. By making pointer of pointer.

int **matrix = new int *[ 10 ] ;

for (int i = 0; i<10; i++){
matrix[ i ] = new int[ 10 ];
}

Now you can reference it.

void fun ( int **mat )
{
}

It does not only benefit into reference it, also useful to initialise 2D matrix with initial value. 👌

Another thing to notice is that we don’t need to make iterative code of spirally printing 2D matrix. Just use simple recursion and save lot of time and brain cells 😂

rutvikrana
Автор

Corrected code :-

#include <iostream>
using namespace std;

int main(){
int n, m;
cin>>n;
cin>>m;
int arr[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>arr[i][j];
}
}

//Spiral Order Traversal Code
int row_Start=0;
int col_Start=0;
int row_End=n-1;
int col_End=m-1;
int count=m*n;

while(row_Start<=row_End && col_Start<=col_End){
//1st
for(int
count--;
if(count<0){
break;
}
cout<<arr[row_Start][col]<<" ";
}
row_Start++;

//2nd
for(int
count--;
if(count<0){
break;
}
cout<<arr[row][col_End]<<" ";
}
col_End--;

//3rd
for(int
count--;
if(count<0){
break;
}
cout<<arr[row_End][col]<<" ";
}row_End--;

//4th
for(int
count--;
if(count<0){
break;
}
cout<<arr[row][col_Start]<<" ";
}
col_Start++;
}
}

jaysoni