1,105,417 Community Members

leap year

Member Avatar
mathgirl
Newbie Poster
11 posts since Apr 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

#include <iostream>
using namespace std;
 
bool LeapYear(int y)
{
return ( y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0);
}
    
double isLeapYear(double x)
{
    if( x > -10000 && x < 10000)
    return x;
}
        
int main()
{
    int x;
    cout << "Please enter start and end of a range of years: ";
    cin >> x;
 if (isLeapYear(x))
    {
    if (LeapYear(x))
        cout << x;
    }
}

that's what i have, any ideas how to fix it?
thanks!

Member Avatar
Ancient Dragon
Achieved Level 70
27,637 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,037 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

>>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.

Member Avatar
mathgirl
Newbie Poster
11 posts since Apr 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

when i enter years, it doesn't return the one's that are leap years.

Member Avatar
Ancient Dragon
Achieved Level 70
27,637 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,037 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

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.

Member Avatar
mathgirl
Newbie Poster
11 posts since Apr 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

#include <iostream>
using namespace std;
    
bool isLeapYear(int y)
{
return ( y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0);
}
     
int main()
{
    int x, z;
    cout << "Please enter start and end of a range of years: ";
    cin >> x >> z;
    
    for(int i = x; i <= z; i++)
    {
    if (isLeapYear(i))
    {
        cout << i << endl;
   }
    }
}
Member Avatar
Aia
Nearly a Posting Maven
2,307 posts since Dec 2006
Reputation Points: 1,977 [?]
Q&As Helped to Solve: 236 [?]
Skill Endorsements: 20 [?]
 
0
 

The function doesn't have to return a value to be able to display what the year is.

void leapYear(int year)
{
    if((year % 100) == 0 )
    {
        printf("%d is a century year\n", year);
    }
    else if(((year % 4) == 0) || ((year % 400) == 0))
    {
        printf("%d is a leap year\n", year);
    }
    else
    {
        printf("%d is a not century\n", year);
    }
}
Member Avatar
John A
Vampirical Lurker
5,285 posts since Apr 2006
Reputation Points: 1,896 [?]
Q&As Helped to Solve: 408 [?]
Skill Endorsements: 12 [?]
Team Colleague
 
0
 

>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.

Member Avatar
Aia
Nearly a Posting Maven
2,307 posts since Dec 2006
Reputation Points: 1,977 [?]
Q&As Helped to Solve: 236 [?]
Skill Endorsements: 20 [?]
 
0
 
int leapYear(int year)
{
   return((!(year % 100 == 0))&&((year % 4 == 0) || (year % 400) == 0));
}

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

if( leapYear( year ))
{
    printf("%d ", year);
}
Member Avatar
Ancient Dragon
Achieved Level 70
27,637 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,037 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

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";

int counter = 0;
for(int i = x; i <= z; i++)
{
    if (isLeapYear(i))
    {
        cout << i << " ";
        if( ++counter == 5)
        {
            cout << "\n";
            counter = 0;
         }
     }
   }
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article