Hey guys I am having a problem knowing where to start on writing this Leap Year program that wants a day number output. Here is the scenario:

Write a function dayNumber that returns the day number (1 to 366) in a year for a date that is provided as input data. Your function should accept the month (1 through 12), day, and year as integers. As an example. For example, January 1, 1994 is day one. December 31 2004 is day 366 because its a leap year. A leap year is divisible 4, except that any year is divisible by 100 is a leap year only if it is divisible by 400. Write a second function that returns true if its argument, a year, is a leap year.

Also I cannot use loops or arrays of any sort. Just calling on these subprograms. Any help would be much appreciated!

The Neophyte Programmer.

Recommended Answers

All 14 Replies

Start with the leap year function:
This function must return true or false, it's input is a year value.
Test if the year value is divisible by 100, if so then test if it's divisible by 400, if it does return true.
Else (if it not divisible by 100) test if it is divisible by 4, if it does return true.
else(none of the above applies) return false

Work that out in code and test it with a small program. Succes.

Here is what I have for the function so far what is wrong it is giving me an error on the if ((year % 4......
What am I doing wrong?

#include <iostream>

using namespace std;

bool checkLeapYr(int year)
{
 bool isLeap;
 
 if ((year%4=0) && (year %100!== 0) || (year%400 == 0)) 
 {
  isLeap = true;            
  return isLeap;
 }
 else 
 {
  isLeap=false;
  return isLeap;
 }        
}

First don't use an assignment operator in an if statement
year%4 = 0 better use == here.
Second you don't have to declare an extra variable isLeap. Just use return true or return false.
Perhaps you could rename your function IsLeap. Carry on!

Ok I made those changes and I am still getting an error in the same place. Please be patient with me, these concepts are very new to me.

#include <iostream>

using namespace std;

bool checkLeapYr(int year)
{
 if ((year % 4 == 0) && (year % 100 !== 0) || (year % 400 == 0)) 
 {
  return true;            
  
 }
 else
 { 
 return false;
}        
}

Hey! I did not notice until now, look after the and operator : !== is not an operator, use !=

Ok great that worked so what is the next step now that I have the Leap Year function going?

What about testing that function in a smal program as pointed out earlier?
For the rest that's up to you, I should start thinking of making a list like so:
jan 0
feb 31
mar 59
apr 90
etc.
meaning the number of days elapsed since the beginning of that month.

Can I get anymore detail of what I need to be doing logically?

Well you have made a function how do you know it works. TEST it.
Make a small program.
Input : year.
Feed year to your leapyear function does it work as expected? Is 1800 a leap year is -200 a leap year is 1952 a leap year is 2003 a leap year? and so on. When you start a bigger project including this leapyear function and you encounter an error you can be certain the error is NOT in this function.

The list you have to make is this not obvious?
It is a predefined array of numbers you can use in a formula like:
day + list(month-1) + (add 1 if month is > 2 and year is leap)

EDIT:
Oops I just noticed you have this silly option not to use arrays...
Use a Switch case statement:
switch(month)
{
case 1 : numberofdays = days;
break;
case 2 : numberofdays = 31 + days;
break;
case 3 : numberofdays = 59 + days + ((1 if year is leap))
etc.

But I can't use an array. It can either be a switch or if statements.

Here is a leap year program to show it works:

#include <iostream>

using namespace std;

bool checkLeapYr(int year)
{
 if ((year%4==0) && (year %100!= 0) || (year%400 == 0))

 {
  return true;            
  
 }
 else
 { 
 return false;
}        
}




int main()                              
{   

int year;
cout << "Enter year to see if it a leap year. " << endl;
cin >> year;
if(checkLeapYr(year)) 
{
cout << year << " is a leap year." << endl;
} else {
cout << year << " is a not leap year." << endl;
}
system("PAUSE");

Ok this is what I have so far, can you tell me what is wrong with it?

#include <iostream>

using namespace std;

bool leapYr(int year)
{
 if ((year%4==0) && (year %100!= 0) || (year%400 == 0))

 {
  return true;            
  
 }
 else
 { 
 return false;
}        
}


int dayNumber (day, month, year, leapYear)

{
    if 
    (year != leapYear(year)) 


{ 
switch(month)
{
case 1 : dayNumber = day;
break;
case 2 : dayNumber = 31 + day;
break;
case 3 : dayNumber = 59 + day; 
break;
case 4 : dayNumber = 89 + day;
break;  
case 5 : dayNumber = 120 + day;
break;  
case 5 : dayNumber = 150 + day;
break; 
case 5 : dayNumber = 181 + day;
break;  
case 5 : dayNumber = 212 + day;
break;   
case 5 : dayNumber = 232 + day;
break;  
case 5 : dayNumber = 263 + day;
break;  
case 5 : dayNumber = 293 + day;
break;  
case 5 : dayNumber = 324 + day;
break;  
default;
return -1; }


else

{ 
switch(month)
{
case 1 : dayNumber = day;
break;
case 2 : dayNumber = 31 + day;
break;
case 3 : dayNumber = 59 + day + 1; 
break;
case 4 : dayNumber = 89 + day;
break;  
case 5 : dayNumber = 120 + day;
break;  
case 5 : dayNumber = 150 + day;
break; 
case 5 : dayNumber = 181 + day;
break;  
case 5 : dayNumber = 212 + day;
break;   
case 5 : dayNumber = 232 + day;
break;  
case 5 : dayNumber = 263 + day;
break;  
case 5 : dayNumber = 293 + day;
break;  
case 5 : dayNumber = 324 + day;
break;  
default;
return -1; }
}
int main()                              
{   

int year, month day;

cout << "Enter date to see what day number it falls in a certain year. " << endl;
cin >> day >> cin >> month >> cin >> year;

cout << "For " << day << "-" << month << "-" << ": " << year dayNumber (day, month, year, leapYear) << endl; 

system("PAUSE");

Sorry,

Here it is fixed without all case 5's

#include <iostream>

using namespace std;

bool leapYr(int year)
{
 if ((year%4==0) && (year %100!= 0) || (year%400 == 0))

 {
  return true;            
  
 }
 else
 { 
 return false;
}        
}


int dayNumber (day, month, year, leapYear)

{
    if 
    (year != leapYear(year)) 


{ 
switch(month)
{
case 1 : dayNumber = day;
break;
case 2 : dayNumber = 31 + day;
break;
case 3 : dayNumber = 59 + day; 
break;
case 4 : dayNumber = 89 + day;
break;  
case 6 : dayNumber = 120 + day;
break;  
case 7 : dayNumber = 150 + day;
break; 
case 8 : dayNumber = 181 + day;
break;  
case 9 : dayNumber = 212 + day;
break;   
case 10 : dayNumber = 232 + day;
break;  
case 11 : dayNumber = 263 + day;
break;  
case 12 : dayNumber = 293 + day;
break;  
case 13 : dayNumber = 324 + day;
break;  
default;
return -1; }


else

{ 
switch(month)
{
case 1 : dayNumber = day;
break;
case 2 : dayNumber = 31 + day;
break;
case 3 : dayNumber = 59 + day + 1; 
break;
case 4 : dayNumber = 89 + day;
break;  
case 5 : dayNumber = 120 + day;
break;  
case 6 : dayNumber = 150 + day;
break; 
case 7 : dayNumber = 181 + day;
break;  
case 8 : dayNumber = 212 + day;
break;   
case 9 : dayNumber = 232 + day;
break;  
case 10 : dayNumber = 263 + day;
break;  
case 11 : dayNumber = 293 + day;
break;  
case 12 : dayNumber = 324 + day;
break;  
default;
return -1; }
}
int main()                              
{   

int year, month day;

cout << "Enter date to see what day number it falls in a certain year. " << endl;
cin >> day >> cin >> month >> cin >> year;

cout << "For " << day << "-" << month << "-" << ": " << year dayNumber (day, month, year, leapYear) << endl; 

system("PAUSE");
}

Looks OK. The switch in the else needs + 1 from case 3 to case 12 Every month from march on needs one day more because of leapyear. Alternatively you could also use one switch and let your leapyearfunction return 0 or 1. But as you did it is OK.

commented: A great help +1

I finally got it working thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.