如何检查方阵的主对角线中的元素是否相同?

时间:2014-04-11 01:32:12

标签: c arrays matrix multidimensional-array

我必须编写一个程序来检查方形矩阵(nxn)的主对角线中的元素(数字)是否相同(如果它们是,则返回1,如果不是则返回0),使用的函数是由main()召集。矩阵从文件中读取,在main()完成。

到目前为止,这是我的函数:(函数checkdiag()似乎不起作用,只有main()函数打印出数据)

#include <stdio.h>
int checkdiag(int matrix[][100], int size)
{
    int i,j;
    for (i=0; i<size; i++)
    {
        for (j=0; j<size; j++)
        {
            if (matrix[i][100]==matrix[j][100])
            {
                return (1);
                printf ("\nThe elements in the main diagonal of the matrix are the same.\n");
            }
            else
            {
                return (0);
                printf ("\nThe elements in the main diagonal are not the same.\n");
            }
        }
    }
}
int main (void)
{
    int matrix[100][100];
    int size, diag;
    int i,j;
    FILE *data;
    data= fopen("data10.txt", "r");`

    fscanf (data, "%d", &size);
    printf ("The size of the matrix is %dx%d, and the matrix is:\n", size, size);
    for (i=0; i<size; i++)
    {
        for (j=0; j<size; j++)
        {
            fscanf (data, "%d", &matrix[i][j]);
            printf (" %d% ", matrix[i][j]);
        }
        printf ("\n");
    }
    diag= checkdiag(matrix, size);
}

如果有人可以帮我看看我哪里出错了,我会很感激的!

P.S我正在使用的文件是:

3
4 5 6
7 8 9
3 6 7

文件(3)中的第一个值是矩阵的大小。 (即3x3)

5 个答案:

答案 0 :(得分:0)

您的printf语句在您的退货声明之后,因此它们永远不会执行。较高的编译器警告级别应警告您无法访问的代码。此外,您使用的值100表示您要使用变量j

但是,您会发现您的算法在任何情况下都是错误的。在第一次比较后停止比较。你需要检查所有对角元素。

答案 1 :(得分:0)

请尝试此checkdiag()

int checkdiag(int matrix[][100], int size)
{
    int i;
    for (i=1; i<size; i++)
    {
        if (matrix[i][i] != matrix[0][0])
        {
            printf ("\nThe elements in the main diagonal are not the same.\n");
            return 0;
        }
    }
    printf ("\nThe elements in the main diagonal of the matrix are the same.\n");
    return 1;
}

答案 2 :(得分:0)

  • 由于此函数只能处理方形矩阵,并且你传递了大小,也许你应该在原型中正确使用它:int checkdiag(int matrix[size][size], int size)
  • 主对角线中的元素具有相同的列索引和行索引,因此您只需要一个循环。
  • 当您发现主对角线上的第一个元素与[0] [0]处的元素不同时,您可以返回,但如果第一个比较为真,则无法结束成功。你必须比较所有元素。
  • 执行return语句后没有语句,除非存在避免return的控制语句。
  • " %d% "printf的错误格式字符串。您需要使用第二个%字符格式字符串转义文字%,如下所示:%%

答案 3 :(得分:0)

#include <stdio.h>

int main (void){

FILE *file = fopen("data.txt","r");
int size, i, j;

fscanf(file, "%d" , &size);
int matrix[size][size];

// Read the data into the matrix
for(i=0; i<size; i++){
    for(j=0; j<size; j++){
        fscanf(file, " %d", &matrix[i][j] );
    }
}

//determine if all diagonal entries in the matrix match the one at matrix[0][0]
i=matrix[0][0];
j=0;
while(j < size ){
    if(matrix[j][j] != i)
        break;
    else{
        j++;
    }
}

//if j is equal to the size of the matrix then voila! diagonal entries match.
if(j==size){
    printf("The diagonal is the same\n");
    return 1;
}else{
    printf("The diagonal is not the same\n");
    return 0;
}
}

返回:对角线不一样。输入:

5
1 2 3 4 5
2 1 4 5 6
2 3 1 5 6
2 3 4 1 6
2 3 4 4 2

返回:对角线相同。输入:

5
1 2 3 4 5
2 1 4 5 6
2 3 1 5 6
2 3 4 1 6
2 3 4 4 1

答案 4 :(得分:0)

而不是cout<<;语句相应地返回1或0.

 #include <iostream>
    using namespace std;

    int main(int arc, char *argv[]) {
        int array[100][100];
        int size;
        cout << "Enter size\n";
        cin >> size;
        cout << "Enter matrix\n";
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                cin >> array[i][j];
            }
        }

        for (int i = 1; i < size; i++) {
            if (array[i][i] != array[i-1][i-1]) {
                cout << "Not Same ";
                break;      
            } else if (i == (size - 1)) {
                cout << "Same";
            } else {
                continue;
            }
        }



    }
相关问题