Challenge 9

Description

The purpose of this challenge is to use arrays and functions. This challenge calculates the day of the year given the nth day of the year.

Requirements

  1. Write a function string get_date(int day) that will return a string indicating the month and day of the year based on an integer input. For this function to return a string that is a combination of a month (a string) and a day (an integer), you will have to use theĀ to_string() function
    string get_date(int day)
    {
      // month will represent an index into the months array
      // day_of_month will represent the resulting day
      // within the calculated month
      int day_of_month, month;
      // more code here
    
      // to use to_string(), see the compiler requirements below
      // to_string() is a library function
      // you do not need to create it
      return months[month] + " " + to_string(day_of_month);  
    }
    
  2. In the function, declare a string array as below
    string months[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    
  3. In the function, declare an int array as below
    int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
  4. In main, ask the user to enter an integer between 1 and 365
  5. Display to the user the month and day by calling the get_date() function. (See interaction)

Hints

Your get_date() function can be written-at minimum-with two accumulators, a while loop and a subtraction or two.

DO NOT USE

Any built-in date/time functions

Sample Interaction / Output

Enter the nth day of the year: 5
Day 5 is Jan 5

[run it again]
Enter the nth day of the year: 59
Day 59 is Feb 28

[run it again]
Enter the nth day of the year: 365
Day 365 is Dec 31

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0009

Print Requirements