![]() |
|
#1
|
|||
|
|||
Counting a certain interval through a loopIn another function of this program I'm working on, I need to count how many leap years go by in a certain time period. So, from like 1950 to 2000 for instance, I need to keep track of every 4 and count how many go by. So I need to count how many 4's go by from 1-50. I know I need a loop to do this, but I'm not sure how or which kind to use (for, while, do while, etc). Could someone show me how it's done? Thanks!
|
|||
|
#2
|
||||
|
||||
Re: Counting a certain interval through a loopAny of them will work. First figure out how you'd do it on paper, write down the steps, then figure out what you need to program those steps.
__________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#3
|
|||
|
|||
Re: Counting a certain interval through a loopWell its been a couple hours since u posted so if your still having problems i'll help you along a litle. I think your making this more complicated than you need to. However there are many ways to solve this problem... many...
Think about it logically, what would you do to find how many leap years were from 1950 to 2000 without a computer. I know for me i wouldnt count from 1 - 50 i would probly divide 50/4 = 12.5, problem right? You cant have .5 of a year. But you'll see that when the computer does this calculation it comes out with 12... odd right? Figure out why it does that. It has something to with the type of variables you use. __________________
There are 10 kinds of people in this world. Those who speak binary and those who dont. |
|
#4
|
||||
|
||||
Re: Counting a certain interval through a loopThis won't work if you're going from 1890 to 1940.
__________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#5
|
||||
|
||||
Re: Counting a certain interval through a loophi...
Consider this concept when calculating leap year: Every year divisible by 4 is a leap year. But every year divisible by 100 is NOT a leap year Unless the year is also divisible by 400, then it is still a leap year. here is one sample function that returns the number of leap years between two given years. CPP / C++ / C Code:
i.e if the year y1 is 2001, we increment i to 2004 so that we can reduce the looping time. if the year y1 is 2000, we make no changes. then the value of count is increased if the year is found to be a leap year. |
|
#6
|
|||
|
|||
Re: Counting a certain interval through a loopQuote:
Not true, i just tried it and got the right answer with my code i wrote the other day to check. CPP / C++ / C Code:
i put in 1890 and 1940 it returned a value of 12 __________________
There are 10 kinds of people in this world. Those who speak binary and those who dont. |
|
#7
|
|||
|
|||
Re: Counting a certain interval through a loopQuote:
Try some more tests. 1990-2040 1900 was not a leap year; 2000 was, so the answer should be different. (Your program gives the same answer as it does for 1890-1940.) Make the test even more obvious: 1998-2003 1898-1903 Again, these should give different answers. A quick visual check of your program shows otherwise. Try some more tests (you don't even need to test a range that includes a century year to see that your formula needs some fixing): 2003-2005 Since 2004 was a leap year, the answer should be 1. Another question: When you enter, say 1940 and 1944, are you counting both ends or just one, or both? (They are both leap years: should the answer be 0 or 1 or 2?) Bottom line: You can't prove a program is correct by testing. It is possible to make a program that gives the correct answers for my test cases but still fails for other ranges of years. The program must be "correct by design". Testing can give you confidence in your algorithm, and you should test (a lot), but testing still can't prove it's right. Regards, Dave "You should make things as simple as possible, but no simpler." ---Albert Einstein |
|
#8
|
|||
|
|||
Re: Counting a certain interval through a loopQuote:
Good point. I was thinking about it wrong. Leap years have to be counted from a specific starting date. I guess you could pick any date that is a leap year and use that as a guide. I See what your saying now. I guess i just jumped the gun on this one and didnt think about the logic behind it enough. __________________
There are 10 kinds of people in this world. Those who speak binary and those who dont. |
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Array 1 dimensional help please asap | lion123 | C Programming Language | 10 | 18-Feb-2005 22:53 |
| messy loop help please | sammacs | C Programming Language | 6 | 26-Nov-2004 16:18 |
| Nested for loop with function | Tori | C++ Forum | 11 | 08-Nov-2004 14:02 |
| coding a sentinel controlled loop | tommy69 | C++ Forum | 2 | 10-Mar-2004 15:52 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The