刚开始学c++,想问一下为什么最后出来的avg是整数,不是应该是4.5吗(20个数字是0到9重复两遍的时候)
#include<iostream>
using namespace std;
int main(void) {
int a[20], i, count1 = 0, count2 = 0, sum1 = 0, sum2 = 0, sum = 0;
cout << "请输入20个数字";
for (i = 0; i < 20; i++) {
cin >> a[i];
sum += a[i];
if (a[i] % 2 == 0) {
count2++;
sum2 += a[i];
}
else {
count1++;
sum1 += a[i];
}
}
int max = a[0];
int min = a[0];
for (i = 1; i < 20; i++) {
if (max < a[i]) {
max = a[i];
}
if (min > a[i]) {
min = a[i];
}
}
double avg = sum / 20;
cout << "奇数的个数是:" << count1 << " " << "奇数和为:" << sum1 << endl;
cout << "偶数的个数是:" << count2 << " " << "偶数和为:" << sum2 << endl;
cout << "最大值为:" << max << " " << "最小值为:" << min << " " << "平均值为:" << avg;
return 0;
}