Won't let me divide/what's wrong?

My code won't let me apply the division 'x/365'
there are other mistakes...

#include<iostream>
#include<cmath>
#include<math.h>
#define PI 3.14159265358979323846
using namespace std;
int main()
{
double h; //elevation of sun
long double beta; //latitude of observer
long double delta; //declination of sun
long double tau; //tau=360 degrees, corresponds to 24 hours
long double costau;
long double taudeg, tauhou, tau2;
//sin(h)=sin(beta)*sin(delta)+cos(beta)*cos(delta)*cos(tau)
double conversionfactor=0.017453; //PI/180; degrees to radians

/*AT SUNRISE OR SUNSET, WHEN sin(h)=0, and dividing by cos(beta)*cos(delta), we get:
cos(tau)=-(tan(beta)*tan(delta))
tau=COSH(cos(tau)) hyberbolic cosine in the inverse*/

int x; //number of days since vernal equinox
int jantomarch21=79;
int day, month;
int date;
cout<<"This program calculates daylength, at sunrise/sunset(h=0).\nInsert the date which length will be calculated(dd mm; separated by Enters)."<<endl;
cin>>day>>month;
cout<<"Insert the latitude from where the day length will be calculated."<<endl;
cin>> beta;
int calculatedays();
delta=23.5*sin((conversionfactor*(x/365))*360); //with x= number of days since vernal equinox(March 21)
costau=-(tan(beta*conversionfactor)*tan(delta*conversionfactor));
tau=cosh(costau);
taudeg=tau/conversionfactor;
tauhou=24*taudeg/360;
tau2=tauhou*2;
cout<<"Day: "<<month<<" "<<day<<endl;
cout<<"Latitude: "<<beta<<endl;
cout<<"Days since Vernal Equinox: "<<x<<endl;
cout<<"Declination of the sun: "<<delta<<endl;
cout<<"Tau in radians: "<<tau<<endl;
cout<<"Tau in degrees: "<<taudeg<<endl;
cout<<"Day in hours to solar noon: "<<tauhou<<endl;
cout<<"Day Length: "<<tau2<<endl;
return 0;
}
int calculatedays()
{
int month, date, day, x;
if(month=3)
{
date=(month-3)*30+day;
if (day<21)
{
x=-(date-21);
} else x=date-21;
}

if(month>3)
{
date=(month-3)*30+day;
x=date-21;
}
if(month<3)
{
date=-(month-3)*30+day;
x=date-21;
}
return x;
}
You need to assign a value to x before dividing it.

Note that division with two integers will give you an integer (the decimal part is simply lost). If you want x/365 to give you a floating point value between 0 and 1 you'll have to make sure at least one of x and 365 is a floating point value.

1
2
3
int x = 73;
std::cout << (x / 365) << '\n'; // Prints "0"
std::cout << (x / 365.0) << '\n'; // Prints "0.2" 

Uninitialised variables in function calculatedays() too.

1
2
3
int month, date, day, x;
if(month=3) // month contains garbage
{


 
    if(month=3) 
assigns the value 3 to month, then the condition will always be true. It should be
 
    if (month == 3)


I suspect you want to pass the values as input parameters:
int calculatedays(int day, int month)

Then in main() instead of just
 
    int x; //number of days since vernal equinox 

You'd have
1
2
3
    cin >> day >> month;
    // get number of days since vernal equinox
    int x = calculatedays(day, month);


Edit: corrected typo in last line above.

Last edited on
Please note some of the maths errors too: hyperbolic cosine (cosh) is NOT the inverse of the cosine function - that would be acos().

Note the corrections below. calculatedays() is only an approximation based on equal-length months - I think you should give a better result. Also, I think that March 21st is the 80th (not 79th) day of the year in a non-leap year.

I guess you were given this formula for day length, but if you are interested there are some slightly more accurate ones.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
#include<cmath>                                          // math.h not needed
#define PI 3.14159265358979323846
using namespace std;

int calculatedays( int day, int month );                 // prototype needed if definition given at end


//=========================================================================================
// sin( elevation )=sin(beta)*sin(delta)+cos(beta)*cos(delta)*cos(tau)
// At sunrise or sunset, when elevation = 0, and dividing by cos(beta)*cos(delta), we get:
//        cos(tau)=-tan(beta)*tan(delta) */
//=========================================================================================
//
// One reference source: http://www.jgiesen.de/astro/solarday.htm
//
//=========================================================================================


int main()
{
   double betaDeg; // latitude of observer               // given the approximation as a circular orbit, long double is overkill
   double deltaDeg; // declination of sun
   double tauRad, tauDeg; //360 degrees corresponds to 24 hours      // avoid confusion; state angle units
   double costau;             
   double tauhou;                                        // tau2 not really needed
   double degToRad = PI / 180;                           // more explicit than conversionFactor
   int day, month;
   int date;

   cout << "This program calculates daylength, at sunrise/sunset ( h = 0 ).\n\n";
   cout << "Insert the date for which length will be calculated( dd mm; separated by Enters ): ";
   cin >> day >> month;
   cout << "Insert the latitude from where the day length will be calculated: ";
   cin >> betaDeg;

   int x = calculatedays( day, month );                     // you need to calculate this; x = number of days since vernal equinox
   deltaDeg = 23.5 * sin( ( x / 365.0) * 360 * degToRad );  // 23.5 deg is (roughly) the tilt of the earth's axis
   costau = -tan( betaDeg * degToRad ) * tan( deltaDeg * degToRad );
   tauRad = acos( costau );                                 // acos, NOT cosh! - completely different things
   tauDeg = tauRad / degToRad;
   tauhou = 24 * tauDeg / 360;

   cout << "Day: " <<month << " " << day << endl;
   cout << "Latitude: " << betaDeg << endl;
   cout << "Days since Vernal Equinox: " << x << endl;
   cout << "Declination of the sun: " << deltaDeg << endl;
   cout << "Tau in radians: " << tauRad << endl;
   cout << "Tau in degrees: " << tauDeg << endl;
   cout << "Day in hours to solar noon: " << tauhou << endl;
   cout << "Day Length: "<< 2.0 * tauhou << endl;
}

int calculatedays( int day, int month )
{
   const int vernalEquinox = 80;                           // (approximate) position of the vernal equinox
   // YOU NEED TO CHANGE THIS - ONLY A VERY ROUGH APPROXIMATION (AND FLAWED BY INTEGER TRUNCATION)
   int x = 365.0 * ( month - 1 ) / 12  + day  - vernalEquinox;

   return x;
}



This is Manchester (UK) on 12th August. Looks about right.
This program calculates daylength, at sunrise/sunset ( h = 0 ).

Insert the date for which length will be calculated( dd mm; separated by Enters ): 8 12
Insert the latitude from where the day length will be calculated: 53.5
Day: 8 12
Latitude: 53.5
Days since Vernal Equinox: 144
Declination of the sun: 14.4592
Tau in radians: 1.92674
Tau in degrees: 110.394
Day in hours to solar noon: 7.3596
Day Length: 14.7192
Last edited on
Topic archived. No new replies allowed.