C Programming Homework #12: Structures

Due: Monday, May 2, 2016

See Lesson #26 in my C tutorial.

  1. Consider a structure that represents calendar dates as follows:

      struct Date {
          int year;
          int month;
          int day;
      };
    

    Write a program that accepts a date from a user and then prints out the next date. Your program should have a function that reads a date from the input, a function that advances a date by one day, and a function that displays a date. Your main program should just call these three functions and thus be very simple. Here are suggested declarations of the functions:

      struct Date get_date( void );
      struct Date next_date( struct Date today );
      void put_date( struct Date today );
    

Note that advancing a date to the next day is tricky because of the variable number of days in a month. Ignoring leap years for the moment, you can deal with this by using an initialized array of constant month lengths. Inside the next_date function you might have:

      const int month_lengths[] = { 31, 28, 31, 30, 31, 30,
                                    31, 31, 30, 31, 30, 31 };
    

After advancing the day member of the date you can check to see if it has gone too far by comparing it with the appropriate month length. I will leave it as optional the handling of leap years (be aware of the full leap year rules!).

Submit your source file to Moodle.


Last Revised: 2016-04-27
© Copyright 2016 by Peter C. Chapin <PChapin@vtc.vsc.edu>