C++ :: Aufgabe #300
2 Lösungen

Hitzeindex (gefühlte Temperatur)
Anfänger - C++
von JKooP
- 04.10.2020 um 12:08 Uhr
Die auf dem Thermometer, vor allem im Sommer, angezeigt Temperatur stimmt häufig nicht mit der gefühlten Temperatur überein. Denn je feuchter die Luft, desto wärmer nehmen wir die Temperatur wahr. Deshalb wurde der Hitzeindex (HI) eingeführt, der generell für Temperaturen ab 27°C und einer relativen Luftfeuchte von mehr als 40% angewendet wird.
Schreibe eine Methode/Funktion, die Temperatur (t) und Luftfeuchte (h) entgegennimmt und den Hitzeindex (hi) ausgibt.
hi = c1 + c2*t + c3*h +
c4*t*h + c5*t*t + c6*h*h +
c7*t*t*h + c8*t*h*h +
c9*t*t*h*h
c1 = -8.784695,
c2 = 1.61139411,
c3 = 2.338549,
c4 = -0.14611605,
c5 = -1.2308094e-2,
c6 = -1.6424828e-2,
c7 = 2.211732e-3,
c8 = 7.2546e-4,
c9 = -3.582e-6
Viel Spaß
Schreibe eine Methode/Funktion, die Temperatur (t) und Luftfeuchte (h) entgegennimmt und den Hitzeindex (hi) ausgibt.
hi = c1 + c2*t + c3*h +
c4*t*h + c5*t*t + c6*h*h +
c7*t*t*h + c8*t*h*h +
c9*t*t*h*h
c1 = -8.784695,
c2 = 1.61139411,
c3 = 2.338549,
c4 = -0.14611605,
c5 = -1.2308094e-2,
c6 = -1.6424828e-2,
c7 = 2.211732e-3,
c8 = 7.2546e-4,
c9 = -3.582e-6
Viel Spaß
Lösungen:

#include <iostream> #define c1 -8.784695 #define c2 1.61139411 #define c3 2.338549 #define c4 -0.14611605 #define c5 -1.2308094e-2 #define c6 -1.6424828e-2 #define c7 2.211732e-3 #define c8 7.2546e-4 #define c9 -3.582e-6 using namespace std; double getheatindex(double t, double h) { double hi; hi = c1 + (c2 * t) + (c3 * h) + (c4 * t * h) + (c5 * t * t) + (c6 * h * h) + (c7 * t * t * h) + (c8 * t * h * h) + (c9 * t * t * h * h); return hi; } int main() { double t, h, hi; cout << "Temperatur (int C): "; cin >> t; cout << "Luftfeuchtigkeit (in %): "; cin >> h; hi = getheatindex(t, h); cout << "\nHitzeindex = " << hi << " C\n"; }
C++ 17
C-Code

#include <iostream> #include <vector> #include <tuple> #include <iomanip> using namespace std; vector<tuple<double, double, double>>vc{ {-8.784695, 0, 0}, {1.61139411, 1, 0}, {2.338549, 0, 1}, {-0.14611605, 1, 1}, {-1.2308094e-2, 2, 0}, {-1.6424828e-2, 0, 2}, {2.211732e-3, 2, 1}, {7.2546e-4, 1, 2}, {-3.582e-6, 2, 2}, }; double get_heat_index(double t, double h) { if (t > 27 && h > 40) { auto sum{ 0.0 }; for (const auto& i : vc) { sum += (get<0>(i) * pow(t, get<1>(i)) * pow(h, get<2>(i))); } return sum; } return 0; } int main() { double t, h; cout << "Temperatur [C]: "; cin >> t; cout << "Luftfeuchte [%]: "; cin >> h; auto hi{ get_heat_index(t, h) }; std::cout << fixed << setprecision(2) << "Hitzeindex: " << hi << endl; }