c++ - C++帮助 - 查找用户输入中的最小值

143 4

我的问题是找出用户定义数目的输入的最小值,

感谢你提供的任何帮助,谢谢!

更改最大值/最小值的值不工作


#include <iostream>


using namespace std;



int main() 


{



 double number, numberitems, sum = 0, average, max, min;



 cout <<"Enter number of items: n";


 cin >> numberitems;



 //Make sure user can not enter negatives


 if ( numberitems < 0 ) {


 //no request to perform sum


 std::cout <<"I said not to enter a negative number... 'n";


 std::cin.clear(); //clear bad input flag


 return 1;


 }



 //Get the user's values


 for (int i = 0; i < numberitems; i++)


 {


 std::cout <<"Enter any NON-negative number:";


 std::cin >> number; 


 std::cout << 'n';



 //Maximum value entered


 if (number > max) {


 max = number;


 }



 //minimum value entered


 if (number < min) {


 min = number;


 }



 //Make sure user can not enter negatives


 if ( number < 0 ) {


 //no request to perform sum


 std::cout <<"I said not to enter a negative number... 'n";


 std::cin.clear(); //clear bad input flag


 return 1;


 }



 //Sum of all the numbers


 sum = sum + number;



 //Average of all the numbers


 average = sum / numberitems;



 }


 std::cout << endl;


 std::cout << endl;


 std::cout <<"Sum of numbers entered is:" << sum << 'n';


 std::cout <<"Average of numbers entered is:" << average <<'n';


 std::cout <<"The Highest number entered was:" << max <<'n';


 std::cout <<"The Lowest number entered was:" << min <<'n';


 return 0;


}



临时修复:3


#include <iostream>


using namespace std;



int main() 


{



 int numberitems;


 double number, sum = 0, average; 


 double max = 0;


 double min = 99999999999999999999999999999999999999999999999;



 cout <<"Enter number of items: n";


 cin >> numberitems;



 //Make sure user can not enter negatives


 if ( numberitems < 0 ) {


 //no request to perform sum


 std::cout <<"I said not to enter a negative number... 'n";


 std::cin.clear(); //clear bad input flag


 return 1;


 }



 //Get the user's values


 for (int i = 0; i < numberitems; i++)


 {


 std::cout <<"Enter any NON-negative number:";


 std::cin >> number; 


 std::cout << 'n';



 //Maximum value entered


if (number >= max) {


 // debug - (remove before submission)


 std::cout <<"number is the new max! number =" << number


 <<", max =" << max << std::endl;


 // end of debug


 max = number;


}



//minimum value entered


if (number <= min) {


 // (debug - remove before submission)


 std::cout <<"number is the new min! number =" << number 


 <<", min =" << min << std::endl;


 // end of debug


 min = number;


}



 //Make sure user can not enter negatives


 if ( number < 0 ) {


 //no request to perform sum


 std::cout <<"I said not to enter a negative number... 'n";


 std::cin.clear(); //clear bad input flag


 return 1;


 }



 //Sum of all the numbers


 sum = sum + number;



 //Average of all the numbers


 average = sum / numberitems;



 }


 std::cout << endl;


 std::cout << endl;


 std::cout <<"Sum of numbers entered is:" << sum << 'n';


 std::cout <<"Average of numbers entered is:" << average <<'n';


 std::cout <<"The Highest number entered was:" << max <<'n';


 std::cout <<"The Lowest number entered was:" << min <<'n';


 return 0;


}



时间: 原作者:

58 2

在计算最小和最大值时,应将变量初始化为默认值,


double max, min; // BUG - not initialized


double max = 0; // CORRECT - initialized


double min = DBL_MAX; // CORRECT - initialized



最大程度上,初始化通常为-DBL_MAX (注意: 非DBL_MIN ),但是在这种情况下,如果不存在负值,则最好是0。

原作者:
111 0

min和max未初始化。

然而

例如,以下内容帮助你:


//Get the user's values


for (int i = 0; i < numberitems; i++)


{


 std::cout <<"Enter any NON-negative number:";


 std::cin >> number; 


 std::cout << 'n';



 //Maximum value entered


 if (number > max) {


 // debug - (remove before submission)


 std::cout <<"number is the new max! number =" << number


 <<", max =" << max << std::endl;


 // end of debug


 max = number;


 }



 //minimum value entered


 if (number < min) {


 // (debug - remove before submission)


 std::cout <<"number is the new min! number =" << number 


 <<", min =" << min << std::endl;


 // end of debug


 min = number;


 }



原作者:
52 3

你需要为minmax设置初始值,对于min,我建议使用DBL_MAX的初始值,它在头cfloat中定义。

原作者:
...