Hey skamen, I love these types of problems, so I decided to spend some time on this. First off, I've found some errors in your "numDaysFrom1800" function:

1) Your loop near the beginning of the function:

for (int i=0; i < year; i++) {...

needs to start at 1800, NOT 0, otherwise you're counting all leap years from year 0 to the current year.

2) In your case statement, case 3 should use 28 in the else part, not 29:

CORRECTED:

Code:

case 3:
if (thisYear)
days = days + 31 + 29+ day;
else
days = days + 31 + 28 + day;
break;

also in the case statement, the months after july all have the wrong number of days. July has 31 days which is right, but so does august, then sept has 30 and so on...

CORRECTED:

Code:

case 9:
if (thisYear)
days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day;
else
days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day;
break;
case 10:
if (thisYear)
days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
else
days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
break;
case 11:
if (thisYear)
days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
else
days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
break;
case 12:
if (thisYear)
days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
else
days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
break;

NOTE: Your code as written is going to give you a "daysSince" of 1 for Jan. 1, 1800. I'm not sure if you need it to be zero or not. I've written all my code to be consistent with yours in this respect.

Your leapCheck function also needed a little something. To check if given year is a leap year you check to see if it is divisible by 4, which you've done. The part that needs fixing is when that given year is a century (ie ends in 00, eg 1800, 1900...). A century divisible by 4 is not always a leap year, only if it also divisible by 400. I see you've tried to account for this with:

Code:

if ((year%400==0)||(year%4==0))

However, centuries like 1800 and 1900, which are not leap years, slip through. The year%400 part fails, which is correct, but your code then goes on to check the same year in the year%4 part, which does succeed, and goes on to count it as a leap year.

Below I've corrected this to account for the checking of centuries:

Code:

bool leapCheck (int year) {
if ((year % 100) == 0) {
//is century
if ((year % 400) == 0)
return true;
}
else
if ((year % 4) == 0)
return true;
return false;
}

Ok, errors fixed. Now, on with your questions.

QUESTION 1 (Days of week):

This one was quite easy. All you need is the number of days since Jan 1, 1800, which we have calculated, and a starting point for the days of the week. You know that Jan 1, 1800 was a wednesday. The day of the week can be figured out by this one line function:

Code:

int DayOfWeek(int days) {
return (days-1) % 7;
}

I should explain. I'm using the numbering system:

Wednesday = 0, Thursday = 1, Friday = 2, Saturday = 3, Sunday = 4, Monday = 5, Tuesday = 6

I subtract one from days to allow the use of this numbering system (eg if days = 1, this is the first day January 1, 1800, which is a wednesday, which is zero after the subtraction.). The reason I do this is so that mod 7 will give us one of a possible seven values (0,1,2,3,4,5,or 6) beginning with wednesday. If you don't subtract one, then Tuesday is zero. I just thought subtracting one made more sense since for our purpose, the beginning of time is Wednesday Jan 1, 1800. You could then use a switch if you like to display the day of week as a string. Sorry, that probably confused you more than helped.

QUESTION 2 (convert days since back to date):

This one's a little bit more work, and I can't just give the code outright, however I will explain how I did it.

1)

variables:

int curYear = 1800;

int daysleft = days; //days is the number of days given

What you need to do is break up daysleft into how many whole years, and how many days in the current year, which is then used to figure out the month and day. Goes something like this:

while daysleft is greater than or equal to maximum days in a year (ie 366), determine if curYear is a leap year (you've got a function for this). Subtract number of days in curYear from days left.

After this looping, curYear will hold the year you're looking for, and daysleft will tell you how many days into this year you are.

2) Determine month and day. I'll give you part. What you need to do, is to write the function "daysInMonth", that determines, you guessed it, how many days are in the month given by the variable "mon".

Code:

int mon = 1;
int dim = 0;
bool isLeapYear = leapCheck(curYear);
while (daysleft > (dim = daysInMonth(mon, isLeapYear))) {
daysleft -= dim;
mon++;
}

At the end of this daysleft holds the day of the month, mon holds the number of the month (Jan = 1...). Again you can use a switch statement to turn these integers into strings.

That's about it, this has turned into one monster of a post. Hope this all helped, if you haven't got it already.

kernel mustard