# Thread: help with a loop

1. ## help with a loop

here is my problem: the user input a started date for a year, and let said user input integer 6 for friday and the month is Jan and year is 2000. so January 2000 started on Saturday and ends on Monday.

how do i do a loop so it will calculated the correct start date for any given month follows?

2. How would you do it in your head or on paper?

3. Originally Posted by Daved
How would you do it in your head or on paper?
in my program.

here is the question.
user inputs a year. the day which that year start and lastly a certain month from that year.

here is what i have so far.
i can get the program to to run and display something like this.
what year: 2000
first day for this year: 7 (for sunday)
month to display in this year: 1(for January)
output:
Jan 2000
S M T W T F S
_________1 (
2 3 -----------
--------------
--------------
--------------
30,31

and that is where i stuck on for now,
how do i code a loop that will
get the proper start day for any given month the user wish to display in that year?

here is what i start so far
a function call
int getStartDay(int dayVal, int yearVal, MonthNum)
{
int inputStartDay = dayVal;

4. That's a good start. You still have to figure out how you would do it on paper and turn that into an algorithm.

Because you've gotten a good start I'll give you a hint. I would add up the number of days in the months before the selected month. For example, if they choose January, there are 0 days before January. If they choose February, there are 31 days before February. I would then divide that number by 7 and look at the remainder. For example, 31/7 = 4 remainder 3. So that means that February starts on the day of the week that is three days after the day that January starts on.

5. so

Jan 2000
1 2 3 4 5 6 7 (start day)
S M T W T F S (days in a week)
x x x x x x x 1

x 31

here is what i got

from Jan
31&#37;7 = 3
3 is the start day for Feb 2000. which is Tuesday and i check the calender and it is correct.
for March 2000.
31+29+31 =91%7 =0 now what do i do.

from calender March 2000 started on Wednesday.

6. >> 31+29+31 =91
Are you sure that's how many days are before March?

7. Year 2000
Jan - 31 day
Feb - 29 day

before March = 60 days

before April = 91 days

8. i am talking to myself now.

from Jan 31&#37;7 =3, which is the date in the week Feb start
from Jan & Feb 60%7=4, which is the date in the week Mar start
from Jan, Feb & Mar 91%7=0, so Apr start on the same date as Jan
so for this year the correct start day for a certain month is just the remainder from the modulus 7.

then when i apply this to another year like 2001. it doesn't work. somehow i know i need to add the original start day from jan to the equation to make it work. but how?

9. You still just have to count the days from January 1 2000. Make sure you account for leap years. Otherwise it should still work.

10. what do you mean?

11. You said when you apply this to another year it doesn't work. You're saying that if the user wants to know what day March 2001 starts on, your code isn't working, right?

Just count the number of days since January 2000. So for March 2001, that's 366 days in 2000 plus 31 days in January plus 28 days in February. Get the remainder from 7 on that and it will tell you what day March 2001 starts on.

12. thanks for the help, Daved.
i can't believe i took so long to figuring out such a simple concept.

Popular pages Recent additions