Worksheet 3 Functions and Structures Solution
Worksheet 3 Functions and Structures Solution
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
// 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;
}
}
return 0;
}
int main()
{
date date1, date2;
int comparisonResult;
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;
}