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

标签 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]);
}

最佳答案

亡命之​​徒,

根据要求,可读性强,所有评论均已合并,但名称未更改以保护有罪的人:-)。

注意:添加了错误检查,但矩阵单元格中的值除外,但是任何对 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 必要的大小。

关于c - 显示输入数字指定的矩阵的对角线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33401351/

相关文章:

java - 将函数的前 2 个参数之外的所有参数传递给另一个函数

c - MPI 将矩阵划分为 block

matlab - 向量到行总和为 1 的矩阵

c - 当尝试传递结构节点*然后使用 printf 打印时,我不断收到段错误

java - 安卓NDK : what happens to memory allocated with malloc when app finishes?

javascript - 基于旋转填充二维数组(矩阵)

python - 矩阵和标量符号的混合

Python 矩阵表示为 (40000,)

c - 如何读入数字作为命令参数?

c - Windows Defender 防病毒 API