The pattern to print if n=5 is
1 2 3 4 5
11 12 13 14 15
21 22 23 24 25
16 17 18 19 20
6 7 8 9 10
if anyone can tell me why and where is the overflow/segmentation fault.
#include <bits/stdc++.h>
vector<string> printPattern(int n)
{
int matrix[n][n];
int i=0;
int j=n-1;
int k=1;
while(i<=j)
{
int x=0;
while (x <n&& i<=j)
{
matrix[i][x]=k;
k++;
x++;
}
i++;
int y=0;
while(y<n && i<=j)
{
matrix[j][y]=k;
k++;
y++;
}
j--;
}
for (int i = 0; i <= n-1; i++)
{
for (int j = 0; j <=n-1; j++)
{
cout<< matrix[i][j];
if (j < n - 1)
{
cout << " ";
}
}
cout<<endl;
}
}
Its a 2D array approach to a pattern problem in codingninja's codestudio interview problems.
Aucun commentaire:
Enregistrer un commentaire