Give a matrix m with N lines and N columns and two numbers i and j representing the position in the array of the first element of a parallel to the main diagonal. Show all the elements from the top down from that parallel to the diagonal.
Input data
From the first line, read the N number on the keypad. The following N lines read N natural numbers representing the coordinates of the matrix. The last line reads the numbers i and j.
Output data
The program will display on screen a number of numbers representing the elements on the diagonal parallel.
Restrictions and clarifications
- 1 <= N <= 50
- 0 < i ,j <= N
- 1 <= m[i][j] <= 100
always i = 1 or j = 1
The lines and columns are numbered 1 to N
#include<iostream>
using namespace std;
int
main ()
{
int m[51][51], i, j, n, d;
cout << "n=";
cin >> n;
cout << "intoducere elemente matrice= " << endl;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
{
//cout << "m[" << i << "," << j << "]=";
cin >> m[i][j];
}
cout << "i,j=";
cin >> i >> j;
if (i < j)
{
d = j - 1;
}
else
{
d = i - 1;
}
if (i < j)
{
cout << "parcugere matrice deasupra diagonalei principala= " << endl;
for (i = 1; i < n; i++)
{
j = i + d;
if (j <= n)
{
cout << m[i][j] << " ";
}
}
cout << endl;
}
else
{
cout << "parcugere matrice sub diagonala principala= " << endl;
for (i = 2; i <= n; i++)
{
j = i - d;
if (j > 0)
{
cout << m[i][j] << " ";
}
}
cout << endl;
}
return 0;
}
Anyone please you confirm this algorithm is correct for this problem?
If you have any solution please help me.Thanks.