Alex is found of doing string programs. One day his professor gave him a string based question to solve in a linear time complexity. The program is to change a strit to its diagonal string inter the form of X ( I. e Diagonal from left right and from right left) help him finish it
Here is program:
int main()
{
int arr[5][5];
int i, j;
i = 5 - 1;
j = 0;
for (int num = 1; num <= 5 * 5; num++)
{
arr[i][j] = num;
int i0, j0;
i0 = i;
j0 = j;
if ((i != 0) && (j != 0))
{
i--;
j--;
}
else if ((j == 0) && (i == 0))
{
i = 5 - 2;
j = 5 - 1;
}
else if (j == 0)
{
j = 5 - i0;
i = 5 - 1;
}
else
{
i = 5 - 2 - j0;
j = 5 - 1;
}
}
puts("Result Array:");
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
cout << arr[i][j] << "\t";
}
cout << "\n";
}
}
Comments
Leave a comment