teaching machines

CS 330 Lecture 22 – Cisms

March 30, 2015 by . Filed under cs330, lectures, spring 2015.

Agenda

Code

sizes.cpp

#include <iostream>

int main(int argc, char **argv) {
  printf("sizeof(short): %d\n", sizeof(short));
  printf("sizeof(int): %d\n", sizeof(int));
  printf("sizeof(long): %d\n", sizeof(long));
  printf("sizeof(long long): %d\n", sizeof(long long));
  return 0;
}

blackjack.c

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

enum suit_t {
  DIAMONDS,
  HEARTS,
  CLUBS,
  SPADES
};

enum rank_t {
  ACE = 1,
  DEUCE,
  THREE,
  FOUR,
  FIVE,
  SIX,
  SEVEN,
  EIGHT,
  NINE,
  TEN,
  JACK = 10,
  QUEEN = 10,
  KING = 10
};

struct card_t {
  enum suit_t suit;
  enum rank_t rank;
};

int score(struct card_t *hand, int ncards) {
  int sum = 0;
  int naces = 0;

  for (int i = 0; i < ncards; ++i) {
    if (hand[i].rank == ACE) {
      ++naces;
    } else {
      sum += hand[i].rank;
    }
  }

  if (naces > 0) {
    if (sum + 11 + naces - 1 <= 21) {
      sum += 11 + naces - 1;
    } else {
      sum += naces;
    }
  }

  return sum;
}

int main(int argc, char **argv) {
  struct card_t hand[] = {
    {CLUBS, JACK},
    {SPADES, ACE},
  };

  printf("score(hand): %d\n", score(hand, 2));

  return 0;
}

Haiku

on progress
Our parents learned C
So that we could learn Java
So our kids could sext