Class HW Help C++

  • 本文关键字:C++ Help HW Class c++
  • 更新时间 :
  • 英文 :


我正在为我的课堂作业创建这个程序。我不明白为什么我没有得到最低降雨量的正确值,我一直得到0。我认为我没有给它分配正确的值。任何帮助都会很棒!谢谢

enter code here
#include <iomanip>
#include <iostream>
using namespace std; 
double Average_Rain(double, double);


int main ()
{
double total, average, months, min =0, max = 0;
months = 12.00;

double rainAmount[12];
min == rainAmount[0];
max == rainAmount[0];

for (int i = 0; i < 12; i++){
cout << "Enter the inches of rain for month " << i + 1 << ":" << endl;
cin >> rainAmount[i];
while (rainAmount[i] < 0){
cout << "Sorry the amount of rain for the month cannot be less than 0." << endl;
cout << endl;
cout << "Re-enter the inches of rain this month. " << endl;
cin >> rainAmount[i];
}
if (min > rainAmount[i])
min = rainAmount[i];
if (max < rainAmount[i])
max = rainAmount[i];
total += rainAmount[i];
}
average = Average_Rain(total,months);
cout << "The totlal amount of rain for the year was " << setprecision(2) << fixed << total << " inches." << endl;
cout << setprecision(2) << "The total average of rain fall for the year was " << average << " inches." << endl;
cout << "The smallest amount of rain was " << min << " inches." << endl;
cout << "The largest amount of rain was " << max << " inches." << endl;

}
double Average_Rain(double total, double months){
return total / months;
}
#include <iomanip>
#include <iostream>
#include <limits>
using namespace std;  // usually this is bad to globaly use namespaces !
inline double Average_Rain(double total, double months){
return total / months;
}
int main () {
double total = 0; // need to be initialized !
double min = std::numeric_limits<double>::max(), max = std::numeric_limits<double>::lowest();  // need to be initialized properly !
const double months = 12.00;
double rainAmount[12];
// min == rainAmount[0]; // do nothing 
// max == rainAmount[0]; // do nothing 
for (int i = 0; i < 12; i++) {
cout << "Enter the inches of rain for month " << i + 1 << ":" << endl;
cin >> rainAmount[i];
while (rainAmount[i] < 0 ){
cout << "Sorry the amount of rain for the month cannot be less than 0." << endl;
cout << endl;
cout << "Re-enter the inches of rain this month. " << endl;
cin >> rainAmount[i];
}
if (min > rainAmount[i])
min = rainAmount[i];
if (max < rainAmount[i])
max = rainAmount[i];
total += rainAmount[i];
}
double average = Average_Rain(total,months);
cout << "The totlal amount of rain for the year was " << setprecision(2) << fixed << total << " inches." << endl;
cout << setprecision(2) << "The total average of rain fall for the year was " << average << " inches." << endl;
cout << "The smallest amount of rain was " << min << " inches." << endl;
cout << "The largest amount of rain was "  << max << " inches." << endl;
return 0;
}

可运行代码

最新更新