我写了一个家庭作业问题的代码:
“编写一个可以获得二维数组的代码,并打印出与其邻居平均值最接近的单元格的行和列的索引。”
我编写了这段代码,但它只打印row = 0 column = 0。
我的代码:
#include <stdio.h>
#include <math.h>
#define MAX_SIZE 50
int main()
{
double matrix[MAX_SIZE][MAX_SIZE], average=0, distance=0, sum=0 ;
int i=0, j=0, size_row, size_colum, min_distance_row=0, min_distance_col=0, friend_num=0, min_distance=0;
printf("Please enter the matrix number of rows and number of colums\n");
scanf("%d%d", &size_row, &size_colum);
printf("Please enter the matrix\n");
for(i=0; i<size_row; i++)
{
for(j=0; j<size_colum; j++)
{
scanf("%lf", &matrix[i][j]);
}
}
for(i=0; i<size_row; i++)
{
for(j=0, sum=0, friend_num=0; j<size_colum; j++)
{
if(i!=0)
{
friend_num++;
sum+= matrix[i-1][j];
}
if(i!=size_row-1)
{
friend_num++;
sum+=matrix[i+1][j];
}
if(j!=0)
{
friend_num++;
sum+=matrix[i][j-1];
}
if(j!=size_colum-1)
{
friend_num++;
sum+=matrix[i][j+1];
}
average= (sum/friend_num) ;
distance=fabs(matrix[i][j]-average);
if(i==0&&j==0)
{
min_distance=distance;
}
else
{
if(distance<min_distance)
{
min_distance=distance;
min_distance_row=i;
min_distance_col=j;
}
}
}
}
printf("Row %d, Column %d", min_distance_row, min_distance_col);
return 0;
}
任何人都可以看到我的错误吗?
谢谢