Store and process date records Dry Run in C

Store and process date records is an interactive C dry run visualizer from the Structures Arrays programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Store and process date records Program Code

#include <stdio.h>

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

int isValidDate(struct Date d) {
    if(d.year < 1900 || d.year > 2100) return 0;
    if(d.month < 1 || d.month > 12) return 0;
    
    int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    // Leap year check
    if(d.month == 2) {
        int isLeap = (d.year % 4 == 0 && d.year % 100 != 0) || (d.year % 400 == 0);
        if(isLeap) daysInMonth[1] = 29;
    }
    
    return (d.day >= 1 && d.day <= daysInMonth[d.month - 1]);
}

void displayDate(struct Date d) {
    printf("%02d/%02d/%04d", d.day, d.month, d.year);
}

int compareDates(struct Date d1, struct Date d2) {
    if(d1.year != d2.year) return d1.year - d2.year;
    if(d1.month != d2.month) return d1.month - d2.month;
    return d1.day - d2.day;
}

int main() {
    struct Date dates[50];
    int n;
    
    printf("Enter number of dates: ");
    scanf("%d", &n);
    
    for(int i = 0; i < n; i++) {
        printf("\nDate %d (dd mm yyyy): ", i+1);
        scanf("%d %d %d", &dates[i].day, &dates[i].month, &dates[i].year);
        
        if(!isValidDate(dates[i])) {
            printf("Invalid date! Please re-enter.\n");
            i--; // Repeat this index
        }
    }
    
    printf("\n========== VALID DATES ==========\n");
    for(int i = 0; i < n; i++) {
        printf("Date %d: ", i+1);
        displayDate(dates[i]);
        printf("\n");
    }
    
    // Find earliest date
    int earliest = 0;
    for(int i = 1; i < n; i++) {
        if(compareDates(dates[i], dates[earliest]) < 0) {
            earliest = i;
        }
    }
    
    printf("\nEarliest Date: ");
    displayDate(dates[earliest]);
    printf("\n");
    
    return 0;
}

View the complete Structures Arrays programs page.

Program Console Store and process date records Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.