GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Sep-2005, 18:15
Darth Predator Darth Predator is offline
New Member
 
Join Date: Sep 2005
Posts: 15
Darth Predator is on a distinguished road

Counting a certain interval through a loop


In 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  
Old 26-Sep-2005, 18:56
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,372
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Counting a certain interval through a loop


Any 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  
Old 26-Sep-2005, 23:56
monalin monalin is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
monalin is on a distinguished road

Re: Counting a certain interval through a loop


Well 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  
Old 27-Sep-2005, 01:40
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,372
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Counting a certain interval through a loop


This 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  
Old 27-Sep-2005, 08:06
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Counting a certain interval through a loop


hi...
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:
int count_leap(int y1, int y2)
{
	int i=y1,count=0;
	if(i%4!=0)                  
		i=i+(4-i%4);          //assign i to the next possible year divisible by 4
	for(;i<=y2;i+=4)
		if ((i % 400 == 0)||(i % 4 == 0 && i % 100 != 0))
			count++;
	return count;
}
the value of i is assigned to the next possible year divisible by 4.
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  
Old 27-Sep-2005, 10:37
monalin monalin is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
monalin is on a distinguished road

Re: Counting a certain interval through a loop


Quote:
This won't work if you're going from 1890 to 1940.

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:
#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
	int x,y,i,years;

	cin >> x;
	cin >> y;

	years=y-x;
	years/=4;

	cout << years << endl;
	cin >> i;

	return 0;
}

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  
Old 27-Sep-2005, 11:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,303
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Counting a certain interval through a loop


Quote:
Originally Posted by monalin
Not true, i just tried it and got the right answer with my code i wrote the other day to check.


i put in 1890 and 1940

it returned
a value of 12

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  
Old 28-Sep-2005, 01:34
monalin monalin is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
monalin is on a distinguished road

Re: Counting a certain interval through a loop


Quote:
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?)

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 GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 17:39.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.