Completely new to programming, i come from only excel experience so any help appreciated. The expressions calculate the altitude and bearing of the sun given your latitude
, the hour of the day t
and day of the year day
.
The altitude calculation works fine, however the bearing calculation has sporadic nonsence negative values in the output.
These values which are stored in their respective arrays will be later used for other claculations.
Here's my block of code:
#include <iostream>
#include <cmath>
using namespace std;
float degrees(float radians) { // gets degrees
return (radians * (180 / 3.14));
}
float radians(float degrees) { //gets radians
return (degrees * (3.14 / 180));
}
int main()
{
float latitude = 54;
// cout << "Enter latitude (in degrees): ";
// cin >> latitude;
float const sin_thi = sin(radians(latitude));
float const cos_thi = cos(radians(latitude));
// cout << "sin_thi: " << sin_thi << endl;
// cout << "cos_thi: " << cos_thi << endl;
float cos_omega[24];
// required for later calculation
for (int t = 0; t <= 23; t++)
{
cos_omega[t] = cos(radians((15 * (t - 12))));
// cout << cos_omega[t] << endl;
}
// gathering sun altitude and bearing data, storing into heap multidimentional arrays "alt_sun[][]" and "bearing_sun[][]".
auto alt_sun = new int [365][24];
auto bearing_sun = new int [365][24];
for (int day = 0; day <= 364; day++)
{
for (int t = 0; t <= 23; t++)
{
alt_sun[day][t] = degrees(asin(((0.4 * cos(radians(0.99 * (day - 173)))) * sin_thi) + ((sin(((0.4 * cos(radians(0.99 * (day - 173)))) - 1.57)))) * (cos_omega[t] * cos_thi)));
if (cos_omega[t] > 0)
bearing_sun[day][t] = 360 - degrees(acos((((0.4 * cos(radians(0.99 * (day - 173)))) * cos_thi) - ((sin(((0.4 * cos(radians(0.99 * (day - 173)))) - 1.57))) * (cos_omega[t] * sin_thi))) / (cos(radians(alt_sun[day][t])))));
else
bearing_sun[day][t] = degrees(acos((((0.4 * cos(radians(0.99 * (day - 173)))) * cos_thi) - ((sin(((0.4 * cos(radians(0.99 * (day - 173)))) - 1.57))) * (cos_omega[t] * sin_thi))) / (cos(radians(alt_sun[day][t])))));
}
}
for (int day = 0; day <= 364; day++) // to test calculation
{
cout << "Day " << day << ": " << bearing_sun[day][12] << endl;
}
}
Is there something fundamentally wrong with what im doing?
Expected results for the printed bearing values are to range from about 350 - 360 (roughly due to rounding on my previously made excel calculation). For example the first value should be about 355.
The code should run without user input and the problem should be explicit now.
My first thought was that the bad results were only happening when the else
statement was used in the loop. But my limited amateur testing, like just running the single expression outside of the for
loop and if
statement, produced the same results. Which in my mind leaves only the expression itself, or the array as a possible cause.
Thanks