leap year

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2007
Posts: 11
Reputation: mathgirl is an unknown quantity at this point 
Solved Threads: 0
mathgirl mathgirl is offline Offline
Newbie Poster

leap year

 
0
  #1
Apr 5th, 2007
Leap Years
Def: A year is a century year if it is divisible by 100.
Def: A year is a non-century year if it is not a century year.
Def: A year is a leap year if it is a non-century year that is divisible by 4, or a century year that is divisible by 400. Nothing else is a leap year.
In a source file named leapyears.cpp, write a program that will prompt the user for the starting year and ending year for a range of years and print to the screen all leap years in that range, 5 years per line. You must write a separate function called isLeapYear that takes a year as a parameter and returns whether or not that year is a leap year. Your main function will call the isLeapYear function in a loop for every year within the range delineated by, and including, the starting and ending years.
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool LeapYear(int y)
  5. {
  6. return ( y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0);
  7. }
  8.  
  9. double isLeapYear(double x)
  10. {
  11. if( x > -10000 && x < 10000)
  12. return x;
  13. }
  14.  
  15. int main()
  16. {
  17. int x;
  18. cout << "Please enter start and end of a range of years: ";
  19. cin >> x;
  20. if (isLeapYear(x))
  21. {
  22. if (LeapYear(x))
  23. cout << x;
  24. }
  25. }
that's what i have, any ideas how to fix it?
thanks!
Last edited by WaltP; Apr 6th, 2007 at 2:45 am. Reason: Added CODE tags -- you actually typed right over what they are when you entered this post...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: leap year

 
0
  #2
Apr 5th, 2007
>>any ideas how to fix it?
fix what? you didn't say what's wrong with the code you posted.

why does isLeapYear() take a double as parameter? It should be an integer; afterall years do not contain fractions.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 11
Reputation: mathgirl is an unknown quantity at this point 
Solved Threads: 0
mathgirl mathgirl is offline Offline
Newbie Poster

Re: leap year

 
0
  #3
Apr 5th, 2007
when i enter years, it doesn't return the one's that are leap years.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: leap year

 
0
  #4
Apr 5th, 2007
read the assignment again, and re-read it again and again until you understand it. IsLeapYear() is supposed to return TRUE if the year is a leap year or FALSE is the year is not a leap year. The function LeapYear() that you posted seems to do that. The functon IsLeapYear() that you posted does not do that, actually I have no clue what its purposes is. Delete IsLeapYear() and rename LeapYear() function to IsLeapYear().

In function main() you have to enter two years -- your program only gets one year. Then also in main() you need to create a loop that calls IsLeapYear() for each year between the first and second years that you enter from the keyboard.

Don't forget -- years prior to 1582 AD do not have leap years because the Gregorian calendar wasn't created until February 24, 1582.
Last edited by Ancient Dragon; Apr 5th, 2007 at 11:36 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 11
Reputation: mathgirl is an unknown quantity at this point 
Solved Threads: 0
mathgirl mathgirl is offline Offline
Newbie Poster

Re: leap year

 
0
  #5
Apr 6th, 2007
You must write a separate function called isLeapYear that takes a year as a parameter and returns whether or not that year is a leap year. - not sure how
also not sure how to get it to print up to five leap years per line.
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool isLeapYear(int y)
  5. {
  6. return ( y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0);
  7. }
  8.  
  9. int main()
  10. {
  11. int x, z;
  12. cout << "Please enter start and end of a range of years: ";
  13. cin >> x >> z;
  14.  
  15. for(int i = x; i <= z; i++)
  16. {
  17. if (isLeapYear(i))
  18. {
  19. cout << i << endl;
  20. }
  21. }
  22. }
Last edited by WaltP; Apr 6th, 2007 at 2:47 am. Reason: Added CODE tags -- you actually typed right over what they are when you entered this post...
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: leap year

 
0
  #6
Apr 6th, 2007
The function doesn't have to return a value to be able to display what the year is.


  1. void leapYear(int year)
  2. {
  3. if((year % 100) == 0 )
  4. {
  5. printf("%d is a century year\n", year);
  6. }
  7. else if(((year % 4) == 0) || ((year % 400) == 0))
  8. {
  9. printf("%d is a leap year\n", year);
  10. }
  11. else
  12. {
  13. printf("%d is a not century\n", year);
  14. }
  15. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: leap year

 
0
  #7
Apr 6th, 2007
>The function doesn't have to return a value to be able to display what the year is.
It does according to the assignment:
You must write a separate function called isLeapYear that takes a year as a parameter and returns whether or not that year is a leap year.
Plus, as I see it, the program was supposed to be in C++, not C.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: leap year

 
0
  #8
Apr 6th, 2007
  1. int leapYear(int year)
  2. {
  3. return((!(year % 100 == 0))&&((year % 4 == 0) || (year % 400) == 0));
  4. }

This will return if is a leap year or not.
call it inside a loop that will start with the first year and stop at the wanted one.
Inside the loop

  1. if( leapYear( year ))
  2. {
  3. printf("%d ", year);
  4. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: leap year

 
0
  #9
Apr 6th, 2007
Originally Posted by mathgirl View Post
You must write a separate function called isLeapYear that takes a year as a parameter and returns whether or not that year is a leap year. - not sure how
also not sure how to get it to print up to five leap years per line.
Looks like you have almost completed the assignment. To print 5 leap years per line I would add another counter in that loop to count the number of leap years printed on the line -- when the counter reaches 5 then print a "\n";
  1. int counter = 0;
  2. for(int i = x; i <= z; i++)
  3. {
  4. if (isLeapYear(i))
  5. {
  6. cout << i << " ";
  7. if( ++counter == 5)
  8. {
  9. cout << "\n";
  10. counter = 0;
  11. }
  12. }
  13. }
Last edited by Ancient Dragon; Apr 6th, 2007 at 7:03 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC