943,935 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4675
  • C RSS
Apr 5th, 2007
0

leap year

Expand Post »
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mathgirl is offline Offline
11 posts
since Apr 2007
Apr 5th, 2007
0

Re: leap year

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 5th, 2007
0

Re: leap year

when i enter years, it doesn't return the one's that are leap years.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mathgirl is offline Offline
11 posts
since Apr 2007
Apr 5th, 2007
0

Re: leap year

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 6th, 2007
0

Re: leap year

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mathgirl is offline Offline
11 posts
since Apr 2007
Apr 6th, 2007
0

Re: leap year

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. }
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Apr 6th, 2007
0

Re: leap year

>The function doesn't have to return a value to be able to display what the year is.
It does according to the assignment:
Quote ...
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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Apr 6th, 2007
0

Re: leap year

  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. }
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Apr 6th, 2007
0

Re: leap year

Click to Expand / Collapse  Quote originally posted by mathgirl ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: how to distinquish a set of characters form an interger?
Next Thread in C Forum Timeline: headder files ie stdio.h





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC