0% found this document useful (0 votes)
24 views

Worksheet 3 Functions and Structures Solution

The document discusses C code for defining a date structure, reading and validating dates, comparing two dates, and displaying dates in different formats. Functions are defined to read and validate a date by checking the day, month, and year values, display a date in various formats, and compare two dates by checking the year, month, and day values.

Uploaded by

hiba exol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Worksheet 3 Functions and Structures Solution

The document discusses C code for defining a date structure, reading and validating dates, comparing two dates, and displaying dates in different formats. Functions are defined to read and validate a date by checking the day, month, and year values, display a date in various formats, and compare two dates by checking the year, month, and day values.

Uploaded by

hiba exol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Abdelhamid Ibn Badis University of Mostaganem - Faculty of Exact Sciences and Computer Science

Department of Mathematics and Computer Science - Mathematics and Computer Science L1 (2023-2024)
Data Structures and Algorithms 2
Worksheet No.3: Subprograms - Functions and Procedures (Solution)
Exercise 1:
#include <stdio.h>

// Type declaration
typedef struct
{
int day, month, year;
} date;

// Prototypes

void readDate(date *);


void displayDate(date *, int);
int compareDates(date *, date *);

void readDate(date *d)


// Reads and verifies a date (day, month, year)
{
int leapYearCheck;
while (1)
{
// Date input
printf("\n Day: ");
scanf("%d", &d->day);
printf("\n Month: ");
scanf("%d", &d->month);
printf("\n Year: ");
scanf("%d", &d->year);

// Year validation
if ((d->year <= 1990) || (d->year > 2100))
{
printf("\n \7 Incorrect year!!, Please retry ");
continue;
}

// Month validation
if ((d->month < 1) || (d->month > 12))
{
printf("\n \7 Incorrect month!!, Please retry ");
continue;
}

// Day validation
if ((d->day < 1) || (d->day > 31))
{
printf("\n \7 Incorrect day!!, Please retry ");
continue;
}

switch (d->month)
{
case 4:
case 6:
case 9:
case 11:
if (d->day > 30)
{
printf("\n \7 Incorrect day!!, Please retry ");
continue;
}
break;
case 2:
if (d->day > 29)
{
printf("\n \7 Incorrect day!!, Please retry ");
continue;
}
leapYearCheck = d->year % 4;
if ((leapYearCheck != 0) && (d->day > 28))
{
printf("\n \7 Incorrect day!!, Please retry ");
continue;
}
break;
}
break;
}
}
void displayDate(date *d, int format)
// Displays a date according to the specified format
{
switch (format)
{
case 1:
printf(" %d - %d - %d", d->day, d->month, d->year);
break;
case 2:
printf(" %d - %d - %d", d->month, d->day, d->year);
break;
}
}

int compareDates(date *d1, date *d2)


// Compares two dates
// Returns "+1", "-1", or "0"
{
int diff;

// Compare the years


diff = d1->year - d2->year;
if (diff > 0)
return 1;
if (diff < 0)
return -1;

// Compare the months


diff = d1->month - d2->month;
if (diff > 0)
return 1;
if (diff < 0)
return -1;

// Compare the days


diff = d1->day - d2->day;
if (diff > 0)
return 1;
if (diff < 0)
return -1;

return 0;
}
int main()
{
date date1, date2;
int comparisonResult;

// Reading two date structures


printf("\nEnter the first date: ");
readDate(&date1);

printf("\nEnter the second date: ");


readDate(&date2);

// Comparing the dates


comparisonResult = compareDates(&date1, &date2);

printf("\nThe date: ");


displayDate(&date1, 1);

if (comparisonResult == 1)
printf(", is after the date: ");
else if (comparisonResult == -1)
printf(", is before the date: ");
else
printf(", is equal to the date: ");

displayDate(&date2, 1);

return 0;
}

You might also like