C++: During winter when it is very cold, typically, everyone would like to know
ID: 641004 • Letter: C
Question
C++: During winter when it is very cold, typically, everyone would like to know the windchill factor, especially, before going out. Meteorologist use the following formula to compute the windchill factor, W:
where V is the wind speed in miles per hour and T is the temperature in degrees Fahrenheit. Write a program that prompts the user to input the wind speed, in miles per hour, and the temperature in degrees Fahrenheit. The program then outputs the windchill factor. Your program must contain at least two functions: one to get the user input and the other to determine the windchill factor.
Explanation / Answer
C++ Program:
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
static double gWindChill[11][12];
double calcWindChill (int degree, int mph);
double lookupWindChill (int degree, int mph);
double interpolateWindChill (int degree, int mph);
int
main (void)
{
int degree, mph;
for (int i = 0; i < 11; i++)
for (int j = 0; j < 12; j++)
gWindChill[i][j] = calcWindChill (-45 + i * 5, 5 + j * 5);
while (true)
{
static double result;
cout << " Degree (F): ";
cin >> degree;
cout << " Wind speed (MPH): ";
cin >> mph;
if ((degree < -45 || degree > 45) ||
(mph < 3 || mph > 60 || mph % 3 != 0))
{
cout << " At least one value is out of range! "
<< " Ranges are: "
<< " - Temperature (-45 - 45) "
<< " - Wind speed (3 - 60) in increments of 5 " << endl;
return 1;
}
result = lookupWindChill (degree, mph);
cout<<fixed << showpoint << setprecision(1) << " Wind Chill for (" << degree << ", " << mph << "): "
<< result << " ";
}
return 0;
}
double
calcWindChill (int degree, int mph)
{
return 35.74 + 0.6215*degree -
35.75*pow (mph, 0.16) + 0.4275*degree*pow (mph, 0.16);
}
double
lookupWindChill (int degree, int mph)
{
degree += 10;
mph -= 5; mph /= 5;
if (degree % 5 == 0) {
degree /= 5;
return gWindChill[degree][mph];
}
else
return interpolateWindChill (degree, mph);
}
double
interpolateWindChill (int degree, int mph)
{
int mod;
double lower, higher;
mod = degree % 5;
lower = gWindChill[(degree - mod) / 5][mph];
higher = gWindChill[(degree - mod + 5) / 5][mph];
return ((5-mod)/5.0) * lower + (mod/5.0) * higher;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.