大多数代码似乎都可以,需要帮助找到最小的部分。我必须写什么条件才能找到用户输入的最小数字,并将零作为最小数字排除?
#include<iostream>//header
#include <string>
using namespace std;
int main() {
int n , sentinel = 0, max = 0, min, count = 0, sum = 0;
double avg;
cout << "Enter a series of number terminated by 0:" << endl;//prompt user to input a series of number
do {
cin >> n;//read the input
for (int i = 0; i < n; i++) {
if (n>max)
max = n;
}
for (int i = 0; i < n; i++) {
if (n<min)
min = n;
}
sum = sum + n;
count++;
} while (n!= sentinel);
count--;
avg = sum / count;
cout << "You have enter " << count << " integers" << endl;//display how may input user enter
cout << "Average is " << avg << endl;//display average
cout << "Max is " << max << endl;//display maximum number
cout << "Min is " << min << endl;//display minimum number
system("pause");
return 0;
}
答案 0 :(得分:0)
您可以从int min;
的最大可能值和int max;
的最小值开始:
int min = std::numeric_limits<int>::max();
int max = std::numeric_limits<int>::min();
然后为min
输入的必须小于其初始值,而为max
输入的内容必须大于其初始值。