Answer to Question #310436 in C++ for Mansi Abasaheb pingal

Question #310436

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


1
Expert's answer
2022-03-12T12:46:47-0500

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";
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment