これ
for( k = 0; k < n; k++ )
{
/// here's the error.
/// You assign the new value to total as (total = total + total + temps[k])
total += total + temps[k];
}
avgTemp = total / n;
する必要があります
for( k = 0; k < n; k++ ) { total += temps[k]; }
avgTemp = total / n;
また
for( k = 0; k < n; k++ ) { total = total + temps[k]; }
avgTemp = total / n;
反復合計を使用するとさらに良いでしょう。これにより、丸め誤差を回避できます。
avgTemp = temps[0];
for(k = 1 ; k < n ; k++) { total = (temps[k] + (double)(k-1) * total)/ (double)k; }
bames53はまた、コメントで素晴らしいSTLベースのコードを提供します。