If we are given start row, start column, endrow, endcolumn. How to print a diagonal line in an array using ONLY while loop in C? Print a 4 in the diagonal position.
#include<stdio.h>
void space(int n)
{
for(int i=0;i<n;i++)printf(" ");
}
void southeast(int m[][3],int s_row,int s_col,int e_row,int e_col)
{
int temp_col=s_col;
int temp_row=s_row;
while(temp_row<=e_row)
{
space(temp_row-s_row);
printf("%d\n",m[temp_row][temp_col]);
temp_row++;
temp_col++;
}
}
void northwest(int m[][3],int s_row,int s_col,int e_row,int e_col)
{
int temp_col=e_col;
int temp_row=e_row;
while(temp_row<=s_row)
{
space(temp_row-e_row);
printf("%d\n",m[temp_row][temp_col]);
temp_row++;
temp_col++;
}
}
void northeast(int m[][3],int s_row,int s_col,int e_row,int e_col)
{
int temp_col=e_col;
int temp_row=e_row;
while(temp_row<=s_row)
{
space(s_row-temp_row);
printf("%d\n",m[temp_row][temp_col]);
temp_row++;
temp_col--;
}
}
void southwest(int m[][3],int s_row,int s_col,int e_row,int e_col)
{
int temp_col=s_col;
int temp_row=s_row;
while(temp_row<=e_row)
{
space(e_row-temp_row);
printf("%d\n",m[temp_row][temp_col]);
temp_row++;
temp_col--;
}
}
int main()
{
int m[][3]={{1,2,3},{4,5,6},{7,8,9}};
int s_row=2,s_col=0;
int e_row=0,e_col=2;
if(s_row<e_row && s_col<e_col)southeast(m,s_row,s_col,e_row,e_col);
else if(s_row>e_row && s_col>e_col)northwest(m,s_row,s_col,e_row,e_col);
else if(s_row>e_row && s_col<e_col)northeast(m,s_row,s_col,e_row,e_col);
else
southwest(m,s_row,s_col,e_row,e_col);
return 0;
}
Output
One thing I could not understand what does "Print a 4 in the diagonal position." mean? Tell me in the comment.
Get Answers For Free
Most questions answered within 1 hours.