So I my teacher asked me to make a code and I did. Most test cases pass, but 1 hidden test fails. I really do not know how to test which one failed and why it failed. Any ideas?
code:
#include <iostream>
using namespace std;
//Conidtional Sections
// Making each conditional to a function
int y;
int m;
int d;
//Checking if year is in range
bool inRange(int y, int m, int d) {
if (y < 2022) {
return true;
}
else if (y == 2000) {
if (m == 2 && d <= 28) {
return true;
}
}
else {
return false;
}
}
//Checking if leap year:
bool leapYear(int y) {
if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) {
return true;
}
return false;
}
// If date is valid:
bool valid(int y, int m, int d) {
if (m <= 0 || m >= 13) {
return false;
}
if (d <= 0 || d >= 32) {
return false;
}
// Fixing Months
if (m == 4 || m == 6 || m == 9 || m == 11) {
return d <= 30;
}
// checking if leap year(situational)
if (m == 2) {
if (leapYear(y)) {
return d <= 29;
}
else {
return d <= 28;
}
}
// if all of them are okay
return true;
}
int zeller(int y, int m, int d) {
int h, k, j;
if (m > 2)
m = m;
else {
m = m + 12;
y--;
}
k = y % 100;
j = y / 100;
h = (d + ((13 * (m + 1)) / 5) + k + (k / 4) + j / 4 - (2 * j)) % 7;
// Fixing H if its negative:
if (h < 0) {
h += 7;
}
if (h < 0) {
h += 7;
}
switch (h) {
case (0):
cout << "Saturday" << endl;
break;
case (1):
cout << "Sunday" << endl;
break;
case (2):
cout << "Monday" << endl;
break;
case (3):
cout << "Tuesday" << endl;
break;
case (4):
cout << "Wednesday" << endl;
break;
case (5):
cout << "Thursday" << endl;
break;
case (6):
cout << "Friday" << endl;
break;
}
return 0;
}
// Main solution after functions:
int main() {
int yyyy, mm, dd;
cout << "Please enter your birth date (yyyy mm dd): ";
cin >> yyyy >> mm >> dd;
if (inRange(yyyy, mm, dd)== true && valid(yyyy, mm, dd)==true) {
zeller(yyyy, mm, dd);
}
else if ((inRange(yyyy, mm, dd) == false && valid(yyyy, mm, dd)== true)) {
cout << "Out of range" << endl;
}
else {
cout << "Invalid date" << endl;
}
return 0;
}
code:
#include <iostream>
using namespace std;
//Conidtional Sections
// Making each conditional to a function
int y;
int m;
int d;
//Checking if year is in range
bool inRange(int y, int m, int d) {
if (y < 2022) {
return true;
}
else if (y == 2000) {
if (m == 2 && d <= 28) {
return true;
}
}
else {
return false;
}
}
//Checking if leap year:
bool leapYear(int y) {
if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) {
return true;
}
return false;
}
// If date is valid:
bool valid(int y, int m, int d) {
if (m <= 0 || m >= 13) {
return false;
}
if (d <= 0 || d >= 32) {
return false;
}
// Fixing Months
if (m == 4 || m == 6 || m == 9 || m == 11) {
return d <= 30;
}
// checking if leap year(situational)
if (m == 2) {
if (leapYear(y)) {
return d <= 29;
}
else {
return d <= 28;
}
}
// if all of them are okay
return true;
}
int zeller(int y, int m, int d) {
int h, k, j;
if (m > 2)
m = m;
else {
m = m + 12;
y--;
}
k = y % 100;
j = y / 100;
h = (d + ((13 * (m + 1)) / 5) + k + (k / 4) + j / 4 - (2 * j)) % 7;
// Fixing H if its negative:
if (h < 0) {
h += 7;
}
if (h < 0) {
h += 7;
}
switch (h) {
case (0):
cout << "Saturday" << endl;
break;
case (1):
cout << "Sunday" << endl;
break;
case (2):
cout << "Monday" << endl;
break;
case (3):
cout << "Tuesday" << endl;
break;
case (4):
cout << "Wednesday" << endl;
break;
case (5):
cout << "Thursday" << endl;
break;
case (6):
cout << "Friday" << endl;
break;
}
return 0;
}
// Main solution after functions:
int main() {
int yyyy, mm, dd;
cout << "Please enter your birth date (yyyy mm dd): ";
cin >> yyyy >> mm >> dd;
if (inRange(yyyy, mm, dd)== true && valid(yyyy, mm, dd)==true) {
zeller(yyyy, mm, dd);
}
else if ((inRange(yyyy, mm, dd) == false && valid(yyyy, mm, dd)== true)) {
cout << "Out of range" << endl;
}
else {
cout << "Invalid date" << endl;
}
return 0;
}