gpt4 book ai didi

c - 二维数组中某些满足条件的元素不显示

转载 作者:行者123 更新时间:2023-12-01 03:02:21 26 4
gpt4 key购买 nike

我有一组浮点数,arr[n][m] ,其中n定义行数和 m对列执行相同的操作,用户使用 scanf() 函数输入它们。每个元素根据以下公式计算:

arr[i][j] = fabs(exp(i)-exp(j))+(i+j)/2.+1
我需要检查元素是否小于其两侧相邻元素的总和(在这种情况下忽略底部和顶部,但也可以计算它们)。它确实几乎正确地计算了它的数量,但由于某种原因,它忽略了位于最后两行中间的元素。然后我必须找到这些元素的总和,并用它们邻居的索引总和的平均值替换它们,计算 i+j:
    for (i = 0; i<n; ++i, puts(" ")) // it's easier to deal with the elements displayed in this way
for (j=1; j<m-1; ++j) // begin with the [0][1] because [0][0] doesn't have any neighbour on the left;
m-1 because the last element does not have any neighbour on the right
{
if (arr[i][j] < ((arr[i][j-1])+(arr[i][j+1])))
{
printf("%.2f ", arr[i][j]);
count++;
sum+= arr[i][j];
arr[i][j] = (i+j + i+j) / 4;
}
}

printf("\nThe number of such elements: %i", count);
printf("\nThe sum of such elements is: %.2f", sum);
因此,问题是关于计算最后两行中的中间值并将它们包含在 sum 公式中。

我在满足条件但不显示的元素下划线。

最佳答案

arr[i][j] = (i+j + i+j) / 4; 

这条线是干什么用的?您正在替换元素值,主要是使用更小的值。这就是比较失败的原因——左边的元素比原来小得多,所以总和不再大于中心元素。

关于c - 二维数组中某些满足条件的元素不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60283774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com