Zeller's Formula

This is a discussion on Zeller's Formula within the C++ Programming forums, part of the General Programming Boards category; Hi ya'll I was wondering if someone was able to help me with Zeller's formula, I am trying to write ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    Unhappy Zeller's Formula

    Hi ya'll I was wondering if someone was able to help me with Zeller's formula, I am trying to write a program to find the day of the week using his formula. I need to write a program that takes a date from the user and uses Zeller's Formula to determine the day of the week. I am to use a switch statement to convert the final day of week number given by zeller's into a day of the week string for output. Will someone please help me, and I need to use functions to make my program more readable. This is what I have, but it just is too complicated, I was wondering if someone could help me out a little bit, clean it up and make it actually work where I get the correct day? Please help I am appreciative of any help that can help me!

    Code:
    #include <iostream>
    #include <iomanip> 
    #include <string> 
    
    using namespace std; 
    
    string DayoftheWeek(int , int , int ); 
    
    int main() 
    { 
    	int mon, day, year; 
    	char YorN; 
    	string WeekDay; 
    
    	{ 
    	cout << "Enter the number of the month:"; 
    	cin >> mon; //month of year 
    	cout << "Enter day of the month:"; 
    	cin >> day; //day of month 
    	cout << "Enter the year:"; 
    	cin >> year; //year number 
    
    	cout << "The day of the week for the date you have entered is: "; 
    
    	WeekDay = DayoftheWeek(mon,day,year); //function call 
    	cout << WeekDay; 
    	cout << ".\n"; 
    	cout << "\n\n\nEnter Y to try another, N to stop.\n\t\t"; 
    	cin >> YorN; 
    	} 
    	while(YorN=='Y'||YorN=='y'); 
    
    	return 0; 
    } 
    
    string DayoftheWeek(int a,int b, int year) 
    
    //To find the day of week.
    
    { 
    	string WeekDay;
    	int c,d,w,x,y,z,r;
    	c = year % 100; 
    	d = year / 100; 
    
    	if(a <= 2) 
    	{ 
    		a += 10; 
    	} 
    	else 
    	{ 
    		a -= 2; 
    	} 
    	{ 
    	w = (13 * a - 1) / a; 
    	x = c / 4; 
    	y = d / 4; 
    	z = w + x + y + b + c - 2 * d; 
    	r = z % 7; 
    	} 
    	switch(r) 
    	{ 
    	case 0: WeekDay = "Sunday"; 
    		break;
    	case 1: WeekDay = "Monday"; 
    		break;
    	case 2: WeekDay = "Tuesday"; 
    		break;
    	case 3: WeekDay = "Wednesday"; 
    		break;
    	case 4: WeekDay = "Thursday"; 
    		break;
    	case 5: WeekDay = "Friday"; 
    		break;
    	case 6: WeekDay = "Saturday"; 
    		break;
    	}
    	return WeekDay;
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    http://mathforum.org/dr.math/faq/faq.calendar.html

    Using integers will cause integer arithmetic to be performed which will cause the decimal to be truncated. Here is an example:
    Code:
    int int1 = 10;
    int int2 = 7;
    int answer = int1/3 + int2/4; 
    cout<<answer<<endl;  //4
    You can get remainders with the "modulus" operator or remainder operator: %. Here is an example:
    Code:
    cout<<int1%3<<endl; //1
    These are horrible parameter names:
    Code:
    string DayoftheWeek(int a,int b, int year)
    What is an 'a'? What is a 'b'?
    Last edited by 7stud; 11-13-2005 at 11:10 PM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    Follow-up Zeller's

    Quote Originally Posted by 7stud
    http://mathforum.org/dr.math/faq/faq.calendar.html

    Using integers will cause integer arithmetic to be performed which will cause the decimal to be truncated. Here is an example:
    Code:
    int int1 = 10;
    int int2 = 7;
    int answer = int1/3 + int2/4; 
    cout<<answer<<endl;  //4
    You can get remainders with the "modulus" operator or remainder operator: %. Here is an example:
    Code:
    cout<<int1%3<<endl; //1
    These are horrible parameter names:
    Code:
    string DayoftheWeek(int a,int b, int year)
    What is an 'a'? What is a 'b'?
    Where do you suggest I put that in?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Maybe write it a little bigger next time, my vision... it's not so good.

    He didn't tell you to put anything in, he was fixing your work for you, which you so unappreciativly didn't accept. Just turn your ints into floats, find remainders with modulus , and use more meaningful variable names.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    I'm sorry

    I wasn't trying to be rude, I just thought it was better understandable to make the writing bigger. And hey I appreciate your help, I am just sort of stressed out, I don't know what I got myself into...

    Thanks for all your help, and any other suggestions would be great, it helps I just don't understand ...

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    O and hey, can you tell me why I still keep getting the wrong weekday? What do you see as wrong in my code?

  7. #7
    C++ Newbie
    Join Date
    Nov 2005
    Posts
    49
    Strange, I have another different zeller's algorithm here, is this valid?

    Code:
    if(month < 3) { mp = 0; yp = year-1; }
    else { mp = int(0.4*month+2.3); yp = year }
    t = int(yp/4)-int(yp/100)+int(yp/400);
    return(365*year+31*(month-1)+day+t-mp)%7;
    Sometimes it outputs negative numbers, you will have to add 7 to it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Distance Formula in my program..... I need help fast!!!
    By Mackology101 in forum C Programming
    Replies: 3
    Last Post: 09-23-2004, 11:10 PM
  2. how to change char to formula
    By kosong in forum C Programming
    Replies: 2
    Last Post: 06-09-2003, 05:31 AM
  3. Math formula (js -->C++)
    By Demon1s in forum C++ Programming
    Replies: 20
    Last Post: 05-02-2003, 06:22 AM
  4. Need help with this formula -b/2a..Please read.
    By foofoo in forum C Programming
    Replies: 4
    Last Post: 06-05-2002, 12:59 AM
  5. Formula string conversion
    By Gr3g in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2002, 09:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21