COMP1511 18s1 (webcms)
Code Examples from Lectures on extra_C
COMP1511 18s1 (flask)
    #include <stdio.h>

int x;

void f(int j) {
    x = j;
}

int main(void) {
    printf("%d\n", x); // prints 0
    f(42);
    printf("%d\n", x); // prints 42
    x = 1;
    printf("%d\n", x); // prints 1
    return 0;
}

    #include <stdio.h>

int f(int j) {
    static int x;
    x = x + j;
    return x;
}
int main(void) {
    printf("%d\n", f(1)); // prints 1
    printf("%d\n", f(2)); // prints 3
    printf("%d\n", f(4)); // prints 7
    return 0;
}

    #include <stdio.h>

int main(void) {
    int x = 42;
    int y = 11;
    double f;

    f = x / y;
    printf("42/11=%f\n", f); // prints 3.000000
    f = x/((double)y);       // convert y to a double
    printf("42/11=%f\n", f); // prints 3.818182
    return 0;
}



Read a list of driving licences from one file and a list of speeding incidents from another file; then print an infringement notice for each speeding incident.
    #include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LICENCE  300
#define MAX_SPEEDING 500
#define MAX_PLATE     10
#define MAX_NAME      20

struct date_time {
    int day;
    int month;
    int year;
    int hour;
    int minute;
};

struct speeding {
    struct date_time   when;
    double        km_over_limit;
    char          plate[MAX_PLATE];
};

struct licence {
    char plate[MAX_PLATE];
    char name[MAX_NAME];
};

int scan_licence(struct licence  *licence, FILE *fp);
int scan_incident(struct speeding *incident, FILE *fp);
int scan_date_time(struct date_time *d, FILE *fp);
void print_date_time(struct date_time date);
void print_notices(struct speeding *spd[], struct licence *licences[]);

// Read a list of driving licences from one file
// and a list of speeding incidents from another file;
// then print an infringement notice for each speeding incident.

int main(int argc, char *argv[]) {
    if (argc < 3) {
        printf("Usage: %s <licence_file> <incident_file>\n",argv[0]);
        exit(1);
    }

    struct licence  licences[MAX_LICENCE];
    FILE *licences_file = fopen(argv[1], "r");
    for (int n_licences = 0; n_licences < MAX_LICENCE; n_licences = n_licences + 1) {
        if (!scan_licence(&licences[n_licences], licences_file)) {
            break;
        }
    }
    fclose(licences_file);

    struct speeding incidents[MAX_SPEEDING];
    FILE *incidents_file = fopen(argv[1], "r");
    int n_incidents;
    for (n_incidents = 0; n_incidents < MAX_LICENCE; n_incidents = n_incidents + 1) {
        if (!scan_incident(&incidents[n_incidents], incidents_file)) {
            break;
        }
    }
    fclose(incidents_file);

    for (int i = 0; i < n_incidents; i = i + 1) {
        for (int j = 0; j < n_incidents; j = j + 1) {
            if (strcmp(licences[i].plate, incidents[j].plate) == 0) {
                printf("\n%s", licences[i].name);
                printf("On ");
                print_date_time(incidents[j].when);
                printf("\nyou were found traveling at %1.1f km/h above the speed limit\n", incidents[j].km_over_limit);
            }
        }
    }

    return 0;
}

//   Read a struct licence from the file pointer fp, in this format:
// John Smith
//  JJJ 472
// Mary Brown
//  ABC 123

int scan_licence(struct licence *licence, FILE *fp) {
    if (fgets(licence->name,  MAX_NAME, fp) && fgets(licence->plate, MAX_PLATE, fp)) {
        return 1;
    } else {
        return 0;
    }
}

//   Create a record of a speeding incident
//   and scan the relevant data from the file pointer fp,
//   in this format:
//   2/3/2005 3:45 87.6 JJJ 472

int scan_incident(struct speeding *incident, FILE *fp) {
    if (scan_date_time(&incident->when, fp)
        && fscanf(fp, "%lf", &incident->km_over_limit)
        && fgets(incident->plate, MAX_PLATE, fp)) {
        return 1;
    } else {
        return 0;
    }
}


// Read a date_time from the file pointer fp,
// in the format day/month/year hh:mm
int scan_date_time(struct date_time *d, FILE *fp) {
    return fscanf(fp,"%d/%d/%d %d:%d", &d->day, &d->month, &d->year, &d->hour, &d->minute) == 3;
}

//   Print a date_time in the format
//   day/month/year hh:mm
void print_date_time(struct date_time d) {
    printf("%d/%d/%d %d:%d", d.day, d.month, d.year, d.hour, d.minute);
}