计算矩阵中的空对角线

时间:2014-06-27 06:53:30

标签: matrix diagonal

我试图创建一个计算方形矩阵中空对角线数量的程序,但我似乎无法找到使索引在矩阵中正确运行的正确方法。这是我到目前为止所提供的错误代码:

# include<stdio.h>

# define MAX 100

int DiagonNull (int n, int A[MAX][MAX]) {
int i, j, count, null;

banda = 0;

for(i = n - 1; i >= 0; i--){
    count = 0;
    for(j = 0; j <= n && j < i - 1; j++){
        if (A[i][j] == 0)
            count++;
    }
    if (count == n - i) /* n - i = number of elements in diagonal */
        null++;
    else
        i = - 1;
}
return null;
}

int main () {

int n, A[MAX][MAX], i, j, null;
printf ("Enter value of n to create a square matrix A of order n: ");
scanf ("%d", &n);

printf ("Enter the elements of matrix A: ");
for (i = 0; i < n; i++){
    for (j = 0; j < n; j++){
        scanf("%d", &A[i][j]);
    }
}

null = DiagonNull (n, A);
printf ("Matrix has null %d diagonals", null);

return 0;
}

0 个答案:

没有答案