关于计算数组大小的C ++问题 [英] C++ question about counting array size
问题描述
大家好,我犯了逻辑错误。对我的朋友来说这是一个简单的项目,但我找不到为什么我做错了。也许你可以帮助我。
所以我有矩阵,我想计算我的诊断以下所有矩阵负数的算术平均值。所以我正在做的是得到所有低于诊断数字并总结它们,latar我得到它的数量,只是div sum / countn。但是我不仅计入了负数,而且还得到了积极的数据:
1 2 3
-4 5 6
2 -4 6
以下的诊断; -4 2 -4
所以负数是2,但是我的代码是3(带负数和正数的3)。
提前感谢您的帮助:)
我的尝试:
Hello guys I am getting logical mistake. It is easy project for my friend, but I can't find why I am doing it wrong. Maybe you can help me.
So I have matrix and I want to count arithmetical average of all matrix negative number below my diagnol. So What I am doing is getting all below diagnol numbers and sum them, latar I am getting its count and just div sum / countn. But I am getting in my count not only negative numbers, but also an positives for ex:
1 2 3
-4 5 6
2 -4 6
below diagnol; -4 2 -4
so negative count is 2, but my code says it is 3 (it is 3 with negatives and positives).
Thanks for help in advance :)
What I have tried:
for (int i =0 ; i<row ; i++)
{
for (int j=0 ; j<col ; j++)
{
if ( i > j )
{
if (M[i][j]<0)
sum= sum + M[i][j];
countn++;
}
}
}
cout<<"neg suma: "<< sum <<endl<<"neg number: "<< countn<< endl << "Average for Negative: " << sum / countn << endl;
return 0;
}
推荐答案
替换
replace
if(M [i] [j]< 0)
sum = sum + M [i] [j];
countn ++;
if (M[i][j]<0)
sum= sum + M[i][j];
countn++;
with
with
if (M[i][j]<0)
{
sum= sum + M[i][j];
countn++;
}
这篇关于关于计算数组大小的C ++问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!