Calculating the First Day of the Week using the Julian Method

  • Context: Comp Sci 
  • Thread starter Thread starter FRANCLI
  • Start date Start date
  • Tags Tags
    C++ Code
Click For Summary
SUMMARY

The forum discussion focuses on a C++ program that calculates the first day of the week using the Julian method. The code functions correctly for years up to 1999 but fails for the year 2000, incorrectly identifying January 1, 2000, as a Sunday instead of a Saturday. The issue is traced to an incorrect conditional statement in the code that checks the century range, specifically the line that should read "b>=2000&&b<=2099". The user is advised to correct this line to resolve the issue.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with Julian calendar calculations
  • Knowledge of conditional statements and logical operators in programming
  • Basic understanding of date and time algorithms
NEXT STEPS
  • Review C++ conditional statements and logical operators
  • Study Julian calendar algorithms in detail
  • Learn about leap year calculations and their implications
  • Explore debugging techniques in C++ to identify and fix logical errors
USEFUL FOR

C++ developers, programmers interested in calendar algorithms, and anyone looking to understand date calculations using the Julian method.

FRANCLI
Messages
12
Reaction score
0
Hello...
the is my code:
#include<iostream.h>
#include<iomanip.h>

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

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

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

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

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

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

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

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

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

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

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

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

}

int main()
{ int x;
int year;
count<<" 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)
}
count<<"\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
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.
 
^^
Yes, your are right ..
merci beaucoup ...
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K