Monday's Lecture

Structured Data

struct.c

// Keep a structure of data together.
// 2017-08-28    Jashank Jeremy <{jashankj,z5017851}@cse.unsw.edu.au>

#include <stdio.h>
#include <stdlib.h>

#define MAX_NAME_LENGTH 64

typedef struct _student {
    int studentID;
    char name[MAX_NAME_LENGTH];
    int tutorial;
    int week01mark;
    int assign0mark;
} student;

void readStudent (student *s);
void printStudent (student s);

int main (int argc, char *argv[]) {
    student s;

    readStudent (&s);
    printStudent (s);

    return EXIT_SUCCESS;
}

void readStudent (student *sp) {
    printf ("Enter a student ID: ");
    scanf ("%d", &(sp->studentID));

    printf ("Enter a tutorial number: ");
    scanf ("%d", &(sp->tutorial));

    printf ("Enter a week 1 mark: ");
    scanf ("%d", &(sp->week01mark));

    printf ("Enter an assign0 mark: ");
    scanf ("%d", &(sp->assign0mark));
}

void printStudent (student s) {
    printf ("Student ID: %d\n", s.studentID);
    printf ("Tutorial: %d\n", s.tutorial);
    printf ("Week 1 mark: %d\n", s.week01mark);
    printf ("Assign0 mark: %d\n", s.assign0mark);
}

complex.c

// Maths is all too complex.
// 2017-08-28    Jashank Jeremy <{jashankj,z5017851}@cse.unsw.edu.au>

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct _complex {
    double re;
    double im;
} complex;

void complexPrint (complex w);
complex complexAdd (complex w, complex z);
complex complexSub (complex w, complex z);
int complexEqual (complex a, complex b);

int main (int argc, char *argv[]) {
    complex w = {
        .re = 0,
        .im = 0
    };

    printf ("The zero complex number is: ");
    complexPrint (w);
    printf ("\n");

    complex x = {
        .re = 42,
        .im = 17
    };

    printf ("The other complex number is: ");
    complexPrint (x);
    printf ("\n");

    complex sum = complexAdd (w, x);

    printf ("Their sum is: ");
    complexPrint (sum);
    printf ("\n");

    complex difference = complexSub (sum, x);
    printf ("And the zero, again, is: ");
    complexPrint (difference);
    printf ("\n");

    printf ("The values ");
    if (complexEqual (difference, w)) {
        printf ("are");
    } else {
        printf ("are not");
    }
    printf (" equal.\n");

    return EXIT_SUCCESS;
}

void complexPrint (complex w) {
    printf ("(%lf + %lf i)", w.re, w.im);
}

complex complexAdd (complex w, complex z) {
    complex value;
    value.re = w.re + z.re;
    value.im = w.im + z.im;
    return value;
}

complex complexSub (complex w, complex z) {
    complex value = {
        .re = w.re - z.re,
        .im = w.im - z.im
    };
    return value;
}

int complexEqual (complex a, complex b) {
    return (a.re == b.re) && (a.im == b.im);
}

purple.c

// Draw a purple image, in our BMP image framework.
// 2018-08-28    Jashank Jeremy <{jashankj,z5017851}@cse.unsw.edu.au>

void drawPurple(pixel pixels[BOARD_SIZE][BOARD_SIZE]) {

    int y = 0;
    while (y < BOARD_SIZE) {
        int x = 0;
        while (x < BOARD_SIZE) {
            // WebCMS 3 purple #825dc4
            pixels[y][x].red   = 0x82;
            pixels[y][x].green = 0x5d;
            pixels[y][x].blue  = 0xc4;

            x++;
        }
        y++;
    }

}