Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include // (Task 1) Declare a new struct type called Date. This structure // could be used to store a calendar date in

image text in transcribed

#include #include #include

// (Task 1) Declare a new struct type called Date. This structure // could be used to store a calendar date in an application which requires // timekeeping on a daily basis. // // Members: // Year - an int which records the year. // // Month - an int which records the month, with valid values ranging // from 1 to 12. // // Day - an int which records the day in the month, ranging from 1 to // 31, subject to the rules set out in the documentation comment of the // Date_Valid function.

typedef struct Date { // (Task 1.1) Declare the fields of the struct in the order listed above. } Date;

// (Task 2) Define a function called Date_Read which uses scanf to get the // data for a Date. Fields are to be entered as three separate int values // in the format "%d/%d/%d". // // Parameters: // date_ptr - the address of a Date which must be populated by the // function call. // // Returns: // The function must return a boolean value indicating the status of the // I/O operation. The status is true if and only if three integer values // have been successfully parsed and saved in date_ptr. // // Do not try to perform other data validation in this function.

INSERT RETURN_TYPE FUNCTION_NAME ( PARAMETER_LIST ) { // (Task 2.1) Insert logic to read three integer values from standard input // and save them in the appropriate fields of date_ptr. Use scanf, but do // NOT use printf or any other output function. }

// (Task 3) Define a function called Date_Write which uses printf to // display the value of a Date structure. // // Parameters: // date - a Date structure that will be displayed. // // Returns: // Nothing.

INSERT RETURN_TYPE FUNCTION_NAME ( PARAMETER_LIST ) { // (Task 3.1) Print the horizontal and vertical position of SCREEN-POS_VAR // with format string "%d/%d/%d". Do NOT insert a linefeed. }

// (Task 4) Define a function called Date_Compare which compares two // Date values. Your implementation may assume that these values are // valid dates. // // Parameters: // date1 - a Date structure. // date2 - a Date structure. // // Returns: // An int which is: // -1 if the date represented by date1 is before that represented by // date2; // 0 if the two values represent the same date; // +1 otherwise.

INSERT RETURN_TYPE FUNCTION_NAME ( PARAMETER_LIST ) { INSERT CODE HERE } // (Task 5) Implement the Date_Valid function which determines if the // supplied date is valid: // * Year must be greater than or equal to 1. // * Month must between 1 and 12 inclusive. // * Day must be at least 1, with upper limits given below: // 30 days: September, April June, and November. // 31 days: January, March, May, July, August, October, December. // 28 or 29: February (usually 28, but 29 in a leap year). // // A year is a leap year if it is divisible by 400, or if it is // divisible by 4 but not divisible by 100. // // Parameters: // date - a Date value. // // Returns: // Returns true if and only if the supplied date is valid according to // the definition listed above.

INSERT RETURN_TYPE FUNCTION_NAME ( PARAMETER_LIST ) { INSERT CODE HERE }

// (Task 6) Define a function called Date_Match which compares a query date to // the elements of an array of Date objects. The function returns the // address of the first object in the list which satisfies a designated criterion. // // Parameters: // query - a Date structure. // dates - an array of Date structures. // num_dates - an int which tells the function how many elements there // are in the array. // criterion - an int (guaranteed to be -1, 0, or 1) which defines the // matching criterion. // // Returns: // A pointer to a Date object. // If num_dates is equal to or less than 0: this value will be NULL. // If the query is not valid: this value will be NULL. // If there is no valid element x in the array which // Date_Compare(x,query) == criterion // then this value will be NULL. // Otherwise: the return value will be the address of the first valid // Date x in the array for which // Date_Compare(x,query) == criterion.

INSERT RETURN_TYPE FUNCTION_NAME ( PARAMETER_LIST ) { INSERT CODE HERE }

#define MAX_ITEMS (100)

int main(void) { Date query; printf("Input query date using format %s, with year first and day last: ", "%d/%d/%d"); Date_Read(&query);

Date ref_dates[MAX_ITEMS] = { {0,0,0} }; int num_items;

// Get number of ref_dates. printf("Please enter number of items (up to %d) that will be processed: ", MAX_ITEMS); scanf("%d", &num_items);

// if number of ref_dates exceeds array size, restrict it to that value. if (num_items > MAX_ITEMS) { num_items = MAX_ITEMS; }

for (int i = 0; i

for (int i = 0; i

if (!Date_Valid(ref_dates[i])) { printf(" is not valid. "); continue; }

int cmp = Date_Compare(ref_dates[i], query); if (cmp 1) { printf("Error!!! Date_Compare is broken. "); exit(1); } char * labels[] = { "less than", "equal to", "greater than" }; printf(" is %s ", labels[cmp + 1]); Date_Write(query); printf(" "); }

const int criterion = -1; Date * cmp = Date_Match(query, ref_dates, num_items, criterion);

if (cmp) { printf("The first valid date matching the search criterion is "); Date_Write(*cmp); } else { printf("There is no valid date matching the search criterion. "); }

return 0; }

image text in transcribedimage text in transcribedimage text in transcribed

C Language. Complete the type definition for a struct type called Date, and then implement the following functions which perform meaningful operations on objects of this type. Date_Read - Read three integers into a Date object reference d by a pointer. Date Write - Display the value of a Date object. Date_Valid -Determine whether a Date object represents a valid date in the standard calendar Date Compare- Determine the ordering relationship (i.e. one value is before, the same, or after another) between two Date objects Date_Match - Search an array of Date objects to find the first date having a specified relationship to a query date Instructions are included as in-line comments in the test driver below

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

LO2 Compare three types of individual incentives.

Answered: 1 week ago