## Adobe Interview Question

Software Engineer in Tests- 0of 0 votes
Given an n*n matrix. Print the main diagonal elements starting from bottom-left to top-right.

**Country:**India

**Interview Type:**In-Person

```
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, j, row, col, a[10][10];
printf("\nEnter the number of rows and columns\n");
scanf("%d %d", &row, &col);
printf("\nEnter the elements row-wise\n");
for(i = 0; i < row; i++)
for(j = 0; j < col; j++)
scanf("%d", &a[i][j]);
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
printf("%d\t", a[i][j]);
printf("\n");
}
printf("\nFollowing are the elements from left-bottom to right-top\n");
for(i = row - 1, j = 0; i >= 0; i--, j++)
{
printf("%d\t", a[i][j]);
}
system("pause");
return 0;
}
```

I am very sure the question is not as simple as it seems. The interviewer wanted me to avoid loops.

```
#include<iostream>
using namespace std;
int printDiagonal(int a[100][100], int n, int m)
{
if(m!=0)
printDiagonal(a, n, m-1);
cout<<a[n-m][m]<<"\t";
return 0;
}
int main()
{
int a[100][100];
int n;
cout<<"Enter the value of N in NxN matrix : ";
cin>>n;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
a[i][j] = i*n +j;
}
}
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cout<<a[i][j]<<" ";
}cout<<"\n";
}cout<<"\n\n";
//
printDiagonal(a, n-1, n-1);
system("pause");
return 0;
}
```

When the interviewer said "wo loops", I presume he meant wo nested loops. Anyway, below is the single loop solution, though we could also implement it with recursion, which as noted earlier, would be an overkill.

```
void print_bl_tr_diag(int a[][], int n)
{
for (int j = n - 1; j >= 0; j--)
printf("%d\n", a[j][n - j - 1]);
}
```

//Given an n*n matrix. Print the main diagonal elements starting from bottom-left to top-right.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void diag(int a[][10],int i,int j,int n)

{

if(i<0 || j>n-1)

{

getch();

exit(0);

}

printf("%d\t",a[i][j]);

diag(a,i-1,j+1,n);

}

int main()

{

int n,i,j,a[10][10];

printf("Enter the value of n(n*n)\n");

scanf("%d",&n);

printf("Enter the matrix\n");

for(i=0;i<n;i++)

for(j=0;j<n;j++)

scanf("%d",&a[i][j]);

diag(a,n-1,0,n);

return 0;

}

- Anonymous February 14, 2012