Calculating the First Day of the Week using the Julian Method

  • Comp Sci
  • Thread starter FRANCLI
  • Start date
  • Tags
    C++ Code
In summary, the code provided has a problem in calculating the first day of the year for years 2000 and onwards. The issue lies in one of the boolean expressions, which is going in the wrong direction. The correct expression should be b>=2000&&b<= 2099. The use of [code] tags is recommended for better formatting and readability.
  • #1
FRANCLI
12
0
Hello...
the is my code:
#include<iostream.h>
#include<iomanip.h>

void show(int m, int w, int n)
{
cout<<endl<<"Sun Mon Tue Wed Thu Fri Sat\n";
cout<<setw((w*5)+1);
for(int r=1;r<=(7-w);r++) cout<<r<<" ";
int count=0;
cout<<endl;
for (;r<=n;r++)
{
if(r<10)
{
cout<<r<<" ";
count++;
}
else
{
cout<<r<<" ";
count++;
}
if((count%7)==0)
cout<<endl;
}
}
void print(int v, int q,int y)
{
int nd;
switch (v)
{
case 1 :
nd=31;
cout<<"\n\n\nJan\n";
show(v,q,nd);
break;

case 2:
if(y%4==0)
nd=29;
else
nd=28;
cout<<"\n\n\nFeb\n";
show(v,q,nd);
break;

case 3 :
nd=31;
cout<<"\n\n\nMar\n";
show(v,q,nd);
break;

case 4:
nd=30;
cout<<"\n\n\nApr\n";
show(v,q,nd);
break;

case 5 :
nd=31;
cout<<"\n\n\nMay\n"; show(v,q,nd);
break;

case 6:
nd=30;
cout<<"\n\n\nJun\n";
show(v,q,nd);
break;

case 7 :
nd=31;
cout<<"\n\n\nJul\n";
show(v,q,nd);
break;

case 8 :
nd=31;
cout<<"\n\n\nAug\n";
show(v,q,nd);
break;

case 9 :
nd=30;
cout<<"\n\n\nSep\n";
show(v,q,nd);
break;

case 10 :
nd=31;
cout<<"\n\n\nOct\n";
show(v,q,nd);
break;

case 11:
nd=30;
cout<<"\n\n\nNov\n";
show(v,q,nd);
break;

case 12:
nd=31;
cout<<"\n\n\nDec\n";
show(v,q,nd);
break;
}

}

int main()
{ int x;
int year;
cout<<" Welcome \n *******"<<endl<<"enter the year";
cin>>year;
int a= (int)(year/100);
int b= a*100 ;
int c= (year-b);
int d;
if((b>=1700&&b<=1799)||(b>=2100&&b<=2199)||(b>=2500&&b<=2599))
d=4;
else if((b>=1800&&b<=1899)||(b>=2200&&b<=2299)||(b>=2600&&b<=2699))
d=2;
else if((b>=1900&&b<=1999)||(b>=2300&&b<=2399))
d=0;
else if((b>=2000&&b>=2099)||(b>=2400&&b<=2499))
d=6;
int e=(int)(c/4);
for(int i=1;i<=12;i++)
{
switch(i)
{
case 10:
x=0;
break; case 1: if((year%4)==0) x=6; else x=0; break; case 2: if((year%4)==0) x=2;else x=3; break;
case 3 : case 11:
x=3;
break;
case 4: case 7:
x=6;
break;
case 5 :
x=1;
break;
case 6 :
x=4; break;
case 8 :
x=2; break;
case 12 : case 9 :
x =5;
break;}



int z;

z=((c+d+e+x+1)%7);

print(i,z,year); //(Still Julian Calendar in Great Britain)
}
cout<<"\n\n PleAsE GrAde uS weLL "<<endl<<" ***********";
return 0;
}
I have written a code but there is a problem in this code .
The code gives a true calendar until 1999 but when it comes to the year 2000 the first day of the year is saturday but my code gives that the first day of the year is sunday ,, and so on for the other years that follows this year and I don't know really what is the problem.
First I use Julian method to calculate the first day of the week ,and here is an example to illustrate this method.

January 1, 2000

Look up the 2000s in the centuries table: 6
This is the table :
1700–1799 4 (Still Julian Calendar in Great Britain and its colonies until 1752)
1800–1899 2
1900–1999 0
2000–2099 6
2100–2199 4
2200–2299 2
2300–2399 0
2400–2499 6
2500–2599 4
2600–2699 2

Note the last two digits of the year: 00
Divide the 00 by 4: 0/4 = 0 and drop the fractional part: 0
Look up January in the months table: 6 (leap)
This is the table:
January 0 (in leap year 6)
February 3 (in leap year 2)
March 3
April 6
May 1
June 4
July 6
August 2
September 5
October 0
November 3
December 5

Add all numbers from steps 1–4 to the day of the month (in this case, 19): 1+6+00+0+6=13.
Divide the sum from step 5 by 7 and find the remainder: 13/7=1 remainder 6
Find the remainder in the days table: 6=Saturday.
This is the table:

Sunday 0
Monday 1
Tuesday 2
Wednesday 3
Thursday 4
Friday 5
Saturday 6
 
Physics news on Phys.org
  • #2
FRANCLI said:
The code gives a true calendar until 1999 but when it comes to the year 2000 the first day of the year is saturday but my code gives that the first day of the year is sunday ,, and so on for the other years that follows this year and I don't know really what is the problem.
I'm pretty sure the following line of code is what is causing your problem. The inequality is going the wrong direction.
Code:
else if((b>=2000&&b>=2099)||(b>=2400&&b<=2499))

The first boolean expression should be b>=2000&&b<= 2099
...............|...

See if that fixes your problem.

BTW, use [ code] and [ /code] tags (without leading spaces inside the brackets) to preserver your formatting and make the code more readable.
 
  • #3
^^
Yes, your are right ..
merci beaucoup ...
 

Related to Calculating the First Day of the Week using the Julian Method

What is C++ code and how is it related to the calendar?

C++ is a programming language used to write software applications. It can be used to create programs that interact with a computer's operating system and manipulate data. In the context of the calendar, C++ code can be used to create and manage calendar applications and functions.

How can C++ code be used to display a calendar?

C++ code can be used to create a calendar by utilizing programming concepts such as functions, loops, and conditional statements. The code can be written to display the current date and time, as well as allow for the user to navigate through different months and years.

Can C++ code be used to add events to a calendar?

Yes, C++ code can be used to add events to a calendar by storing the event information in variables and using file input/output to save the data. The code can also be written to allow for event reminders and notifications.

How does C++ code handle leap years in the calendar?

C++ code can handle leap years in the calendar by using conditional statements to check if the current year is a leap year. If it is, the code can adjust the number of days in February accordingly.

Is C++ code the only language that can be used to create a calendar?

No, there are many programming languages that can be used to create a calendar. C++ is a popular choice due to its versatility and ability to interact with a computer's operating system, but other languages such as Java, Python, and JavaScript can also be used to create calendar applications.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
Back
Top