我必须编写一个程序,检查矩形矩阵(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)
return(1)应该只发生在完成循环之后。所有条目必须匹配才能保持一致,但只有一个不匹配表明对角线不是全部的一个值。 – Jiminion