Solve C++ Leap Year Function Homework

  • Context: Comp Sci 
  • Thread starter Thread starter White Ink
  • Start date Start date
  • Tags Tags
    C++ Function Year
Click For Summary

Discussion Overview

The discussion revolves around a homework assignment to write a C++ function that determines whether a given year is a leap year. Participants are sharing their code attempts, identifying errors, and discussing the correct logic for leap year determination.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a function that always returns 1, expressing confusion about the logic and requesting help to identify mistakes.
  • Another participant critiques the leap year definition used, suggesting that it should include the condition of being divisible by 4 and either not divisible by 100 or divisible by 400.
  • A participant points out logical flaws in the original code, noting that certain conditions would lead to incorrect returns of false for years that are not multiples of 400.
  • One participant advises caution with "else" statements, indicating that they may be misused in the provided code.
  • A later post presents an alternative approach to the problem, outlining a new structure for the leap year check but contains syntax errors and logical inconsistencies.

Areas of Agreement / Disagreement

Participants express differing views on the correct implementation of the leap year logic, with no consensus reached on a final solution. Multiple competing approaches and interpretations of the leap year rules are present.

Contextual Notes

Some code snippets contain syntax errors, such as the use of "count" instead of "cout," and logical errors in the flow of conditions that may affect the output. The discussion highlights the complexity of implementing the leap year rules correctly.

Who May Find This Useful

Students learning C++ programming, particularly those working on conditional logic and function implementation related to date calculations.

White Ink
Messages
18
Reaction score
0

Homework Statement



Write a function that prompts the user to enter a year and that returns a 1 if the entered year is a leap year, and a 0 otherwise.

A year is a leap year if it is divisible by 4.
In general, a year is not a leap year if it is divisible by 100, unless of course it is divisible by 400.

The Attempt at a Solution



I have done this much and when I test my program, it seems to return a value of 1 no matter what year I enter. I'm pretty awful at programming and I was wondering if anyone could spot where my mistake is.#include <iostream>

using namespace std;

bool leap(int) {
int n;
bool b;

if (n%4==0) { b=true; } // if n is divisible by 4, it is a leap year
else if (n%100==0) {
if (n%400==0) { b=true; }
else { b=false; }
} // if n is divisible by 100, check if it is divisible by 400, if it is, it is a leap year
else { b=false; } // if n is not divisible by 4 (=> it is not divisible by 400 either), then it is not a leap year

return b;
}

int main () {
int n;
count << "Please enter a year: " << '\n';
cin >> n;

count << leap(n);

}
 
Physics news on Phys.org
White Ink said:
A year is a leap year if it is divisible by 4.
In general, a year is not a leap year if it is divisible by 100, unless of course it is divisible by 400.
Wrong definition, so you get the wrong answer.

A year is a leap year if it is divisible by 4 and it either is not divisible by 100 or it is divisible by 400.
 
White Ink said:

Homework Statement



Write a function that prompts the user to enter a year and that returns a 1 if the entered year is a leap year, and a 0 otherwise.

A year is a leap year if it is divisible by 4.
In general, a year is not a leap year if it is divisible by 100, unless of course it is divisible by 400.

The Attempt at a Solution



I have done this much and when I test my program, it seems to return a value of 1 no matter what year I enter. I'm pretty awful at programming and I was wondering if anyone could spot where my mistake is.


#include <iostream>

using namespace std;

bool leap(int) {
int n;
bool b;

if (n%4==0) { b=true; } // if n is divisible by 4, it is a leap year
else if (n%100==0) {

This will only be accessed if n is not divisible by 4 which, if n is divisible by 100, is impossible.
if (n%400==0) { b=true; }
else { b=false; }
This says, if n is divisible by 400, b= true. Under any other condition, and it doesn't matter what happened if the first "if", b will be equal to false. In other words, if n is any number that isnot a multiple of 400, you return "false".
} // if n is divisible by 100, check if it is divisible by 400, if it is, it is a leap year
else { b=false; } // if n is not divisible by 4 (=> it is not divisible by 400 either), then it is not a leap year

return b;
}

int main () {
int n;
count << "Please enter a year: " << '\n';
cin >> n;

count << leap(n);

}

What you have written will return "true" if n is divisible by 400, otherwise it will return "false".
 
Be careful with your "else" statements. You are using at least one "else" statement in an unintended way.

- Warren
 
is this close to the solution?

using namespace std;
int main () {
//declare variable integer for year, y;
int y, r1, r2, r3;
//ask user to input a year;
count << "Enter a year and press ENTER " << endl;
//place year in int y;
cin >> y;
//divide year by 4 and find remainder;
r1 = y % 4;
r2 = y % 400;
r3= y % 100;
//main if statement: if rem of y/4 is 0 divide by 400, if there is no rem pri\
nt y is not leap year and end prg;
if (r1==0) {r2 = y % 400;
// if rem of y/400 is 0 then print y is leap year and end prg, otherwise divi\
de y/100;
if (r2==0) {count << y << " is a leap year." << endl;
//if rem of y/100=0 count y is not leap year, end prg, otherwise print y is \
leap year, end prg;
else if (r3==0) {count << y << " is NOT a leap year." << endl;
else count << y << " is a leap year" <<endl;}}}
else count <<y<< " is NOT a leap year." << endl;
return 0;
}
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K