Home > Back-end >  Elements of the right diagonal are not dispayning
Elements of the right diagonal are not dispayning

Time:12-14

I'm trying to display the elements of the right diagonal in 2d array, but the code shows only the first element of the diagonal. The problem is in if-clause if (i j == 2), but I don't know how to solve it

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <cmath>
#include <time.h>
#include <ctype.h>

int main () {
    double a[100][100];
    int n, m;
    int i, j, b, c;
    srand (time (NULL));
    scanf ("%d", &n);
    scanf ("%d", &m);
    for (  i = 0; i < n; i  )
    {
        for (  j = 0; j < m; j  )
        {
            a[i][j] = 0.09 * (rand () 00) - 0.5;
        }
    }
    printf ("Array A[N][M]: \n");
    for ( i = 0; i < n; i  )
    {
        printf ("\n");
        for ( j = 0; j < m; j  )
        {
            printf ("%6.0f", a[i][j]);
        }
    }

    printf ("\nElements of the right diagonal are: \n");
    for ( i = 0; i < n; i  )
    {
        for ( j = 0; i < m; j  )
        {
            if (i j == n)
            {
                printf ("%6.0f\t", a[i][j]);
                return 0;
            }
        }  
    }
    return 0;
}

Without return 0; in if-clause the code doesn't work at all

Hope for helping

I was trying to check various values in if-clause, but the situation hasn't changed

CodePudding user response:

There are a couple of problems in the posted code

// ...
#include <cmath>       // That's a C   header, while this question is tagged as C.

// ...
printf ("\nElements of the right diagonal are: \n");
for ( i = 0; i < n; i  )
{
    for ( j = 0; i < m; j  )
    { //         ^            There's no real need of a nested loop, but
      //                      in any case, the condition is wrong.
      //                      It should be 'j' there. 
        if (i j == n)
        {
            printf ("%6.0f\t", a[i][j]);
            
            // The following return statement will end the loop.
            return 0;
        }
    }  
}

You could either fix the previous snippet:

printf ("\nElements of the right diagonal are: \n");
for ( int i = 0; i < n; i  )
{
    for ( int j = 0; j < m; j  )
    { //             ^
        if ( i   j == n )
        {
            printf ("%6.0f\t", a[i][j]);
        }
    }  
}

Or use a single loop:

printf ("\nElements of the anti-diagonal are: \n");

// Find the limiting dimension for non-square matrices.
int k = m < n ? m : n;

for ( int i = 0; i < k;   i )
{
    printf("%6.0f\t", a[i][m - i - 1]);
    //                     ^^^^^^^^^
}

CodePudding user response:

I solved the problem:

printf ("\nElements of the right diagonal are: \n");
    for (j = 0; j < n; j  )
    {
        printf( "%6.0lf", a[n - j - 1][j] );
    }

CodePudding user response:

You don't want to check if (i j == 2), instead you should be checking if (i j == n). Any point along the diagonal will have coordinates that add up to the size of the square.

CodePudding user response:

It looks like the problem is that the inner for loop is checking if the condition i j == 2 is true. This condition will only be true for the first element of the diagonal, which is why only the first element is being printed.

To fix this, you can change the condition in the if statement to i == j, which will be true for all elements on the main diagonal. You should also change the inner for loop to use a different variable for the loop counter, because using i for both the outer and inner loops can cause confusion.

Here is an example of how you can modify your code to print all elements of the main diagonal:

int main() {
    double a[100][100];
    int n, m;
    int i, j, b, c;
    srand(time(NULL));
    scanf("%d", &n);
    scanf("%d", &m);
    for (i = 0; i < n; i  )
    {
        for (j = 0; j < m; j  )
        {
            a[i][j] = 0.09 * (rand() % 1000) - 0.5;
        }
    }
    printf("Array A[N][M]: \n");
    for (i = 0; i < n; i  )
    {
        printf("\n");
        for (j = 0; j < m; j  )
        {
            printf("%6.0f", a[i][j]);
        }
    }

    printf("\nElements of the main diagonal are: \n");
    for (i = 0; i < n; i  )
    {
        // Check if the element is on the main diagonal
        if (i == j)
        {
            printf("%6.0f\t", a[i][j]);
        }
    }

    return 0;
}
  • Related