Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

If necessary, create a new project named Advanced26 Project and save it in the C

ID: 3808370 • Letter: I

Question

If necessary, create a new project named Advanced26 Project and save it in the Cpp8/Chap11 folder. Also create a new source file named Advanced26. Declare a 12-element int array named in each month to the days. Assign the number of days number of array, for February. Code it displays the days corresponding the program so that example, when the to the month number entered by the user. For if number 7, the program the number 31. However, should display user year. The rules for enters the number 2, the program should ask the user for the year is a leap determining 11-5. If the number year, the whether a year is a leap year are shown in displaying the days program will need to add 1 to the number of days before message and on the screen. The program should also display an Save user enters an invalid month number Use a sentinel value to end the program the then run the program. Test the program using the number 1, and then using numbers 3 through 12. Test it using the number 2 and the year 2015. Then, test it the number 2 and the 2016. Also test it using an number, such as 20 If the year number is not evenly divisible by 4, it is not a leap year. If the year number is evenly divisible by 4 and is not evenly divisible then it is a leap year. If the year number is evenly divisible by both 4 and 100 and is divisible by 400, then it is a leap year; otherwise, it is not a leap year.

Explanation / Answer

#include <iostream>
using namespace std;
bool isLeapYear(int y) {
       if (y % 4 != 0) return false;
   if (y < 1582) return true;
   return (y % 100 != 0) || (y % 400 == 0);
      
   }
int main() {
   int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
   int month, year, addDay = 0;
   cout<<"Enter a number(-1 to quit): ";
   cin >> month;
   while(month != -1){
   if(month < 1 or month > 12) {
   cout<<"Invalid month"<<endl;
   }
   else{
   if(month == 2){
   cout<<"Enter the year: ";
   cin >> year;
   if(isLeapYear(year)){
   addDay = 1;
   }
   }
   cout<<"Number of days in month "<<month<<" is "<<(days[month-1]+addDay)<<endl;
  
   }
       cout<<"Enter a number(-1 to quit): ";
   cin >> month;

   }
   return 0;
}

Output:

Enter a number(-1 to quit): 2

Enter the year: 2016

Number of days in month 2 is 29
Enter a number(-1 to quit): -1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote