-
Counting
I have come to a blank on how to do this. I have to calculate how many days there are between a range of years. I have declared everything and I have gave it the rules to how many days there are in a year due to leap years and nonleapyears. I know I need to create a loop that will add all those days but I dont know where to start at for this loop. Do i use a for loo or a while loop. If I use one of these loops what would be the structure. I am not asking for the code by anymeans because i want to figure that out myself I just need to get my wheels spinning and am at a blank right now. Tahnks for any help.
-
From what i understand you need to run a loop based on the number of years you are adding, this could be a for.
Inside the loop just iadd up a counter to stand for the total number of days, and be sure to check for a leap year.
-
I don't think you need a loop, I think you can calculate it.
EndYear - StartYear + 1 = YearRange
YearRange / 4 = LeapYears
If the first occurance of a leap year starting at StartYear is equal to or less than YearRange%4 then the calculation would be:
( YearRange / 4 ) + 1 = LeapYears
Then finally
days = ( 365 * (YearRange - LeapYears) ) + (364 * LeapYears )
Its been a long day so I would double check my thinking here. If its not right I think its pretty close.
-
Are you sure you have to use a loop?
You can find out the number of years divisible by 4, the number of
years divisible by 100, the number of years divisible by 400, and so on.
Then you can draw one of those nice venn diagrams and count how
many leap years.
-
Ok here is what I got, I am still stuck on it and it makes me mad. I hate not being able to figure something out. This is supposed to be an intro to C++ class but i think the teacher is making it harder then that. I dont feel a student should have to work about 2-3 hours a day for a week straight on one problem for an intro class. here is what i have for the code so far. Before all of this I have the user inputting in the years. That part works fine. What i need the program to do is add up all the days in the range of years then also output which of the years in that range are leapyears. Any help is appreciated. Just something to get me int hte right direction.
int leapyear;
int year;
if (year % 400 == 0)
year = leapyear;
else if (year % 4 != 0 || year % 100 == 0)
year != leapyear;
if (year == leapyear)
days = 366;
else
days = 365;
for (year = year1; year <=year2; year++)
-
There is an alternative (potentially) called difftime() located in time.h if you are allowed to use precompiled functions and know how to handle structs. It returns the difference in time between two separate dates, which can then be manipulated as you deem necessary.
I don't see any way around using a loop in the slug em out way, but it shouldn't be hard to write the loop.
create function to check if year is leap year--isLeapYr().//This is the "hard thingy".
create a variable to hold total days and intitalize to zero.
create two variables to hold the two years
Obtain input for the two years.
Then use a loop to deterimine if a given year starting at the first year incrementing by one until the last year is a leap year by calling isLeapYr() on each year in the interval. If yes add 366, if no add 365, to total. Even with a thousand year interval, the task will be completed before you can blink your eye with todays CPUs.
.