显示由输入数字指定的矩阵的对角线

时间:2015-10-28 20:48:34

标签: c matrix

创建一个创建矩阵的程序,并根据输入的数字K显示一个特定的矩阵。 Here's how it looks

对于K> 0和K< 0,我得到奇怪的数字。

int main()
{
    int A[20][20],N,K,i,j;
    printf("Dimension: ");
    scanf("%d",&N);
    printf("Enter K: ");
    scanf("%d",&K);
    printf("Enter elements\n");
    for(i=0;i<N;i++)
        for(j=0;j<N;j++)
            scanf("%d",&A[i][i]);
    if(K==0)
        for(i=0,j=0;i<N;i++,j++)
            printf("%d",A[i][j]);
    else if (K>0)
        for(j=K,i=0;j<N;j++,i++)
            printf("%d",A[i][j]);
    else
        for(i=-K,j=0;i<N;i++,j++)
            printf("%d",A[i][j]);
}

1 个答案:

答案 0 :(得分:1)

亡命之徒,

根据要求,可读,所有评论都包含在内,但名称不变以保护有罪: - )。

注意:除了矩阵单元格中的值之外,添加了错误检查,但是对scanf()的任何调用都应该进行错误检查,即使对于矩阵单元格中的值也是如此。

#include <stdio.h>
#include <stdlib.h>  // exit(), EXIT_FAILURE

#define MATRIX_MAX_SIZE (20)
#define MATRIX_MIN_SIZE (2)

int main( void )
{
    int A[MATRIX_MAX_SIZE][MATRIX_MAX_SIZE]; // max size of matrix
    int N;  // size of square matrix along one edge
    int K;  // which diagonal selected
    int i;  // row index
    int j;  // column index

    while(1)
    {
        printf( "Enter square matrix edge length: range %d...%d ",
                MATRIX_MIN_SIZE,
                MATRIX_MAX_SIZE );

        if( 1 != scanf("%d",&N) )
        {
            perror( "scanf for matrix edge length failed" );
            exit( EXIT_FAILURE );
        }

        // implied else scanf successful

        if( N < MATRIX_MIN_SIZE || N > MATRIX_MAX_SIZE )
        {
            printf( "Input value: %d is outside allowed range\n", N);
        }

        else
        { // else, valid matrix size selector
            break;
        }
    }

    while(1)
    {
        printf( "Enter which Diagonal to display: range is %d to %d: ",
                -(N-1),
                (N-1) );

        if( 1 != scanf("%d",&K) )
        {
            perror( "scanf for which diagonal to display failed" );
            exit( EXIT_FAILURE );
        }

        // implied else, scanf successful

        if( K < -(N-1) || K > (N-1) )
        {
            printf( "Diagonal Selector: %d is outside the available range\n", K );
        }

        else
        { // else, valid diagonal selector
            break;
        } // endif
    }

    printf( "Enter %d elements\n", (N*N) );

    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            // note: call to scanf() needs to have error checking added
            // note: mod to use proper column number 'j'
            scanf("%d",&A[i][j]);  
        } // end for
    } // end for

    if(0 == K) // always place literal value on left so compiler can catch syntax error
    {
        for( i=0,j=0; i<N; i++,j++ )
        {
            printf( "%d ", A[i][j] ); // insert space so output numbers separated
        } // end for
    }

    else if (K>0)
    {
        for( j=K,i=0; j<N; j++,i++ )
        {
            printf("%d ",A[i][j]); // insert space so output numbers separated
        } // end for
    }

    else
    {
        for( i=-K,j=0; i<N; i++,j++ )
        {
            printf("%d ",A[i][j]); // insert space so output numbers separated
        } // end for
    } // end if

    return 0;
} // end function: main

注意:最好不要使用最大矩阵大小,而是让用户输入一个值然后malloc所需的大小。