#02

// iarray_demo.c --- array input/output and equality demo.
#include <stdio.h>
#include <stdbool.h>
void iarray_read(int *a, int n) {
  for(int i = 0; i < n; ++i) {
    printf("%d> ", i+1); scanf("%d", a+i);
  }
}
void iarray_print(int *a, int n) {
  for(int i = 0; i < n; ++i) { printf(" %2d", a[i]); }
  printf("\n");
}
bool iarray_equal(int *a, int *b, int n) { // !!WITH BUG!!
  for(int i = 0; i <= n; ++i) {
    if(a[i] != b[i]) { return false; }
  }
  return true;
}
char *bool2str(bool b) { return b ? "true" : "false"; }
int main(void) {
  int a[4], b[4];
  iarray_read(a, 4); iarray_print(a, 4);
  iarray_read(b, 4); iarray_print(b, 4);
  printf("equal: %s\n", bool2str(iarray_equal(a, b, 4)));
  return 0;
}
// test_array_equal.c --- unit test of array_equal
#include <stdio.h>
#include <stdbool.h>
void iarray_read(int *a, int n) {
  for(int i = 0; i < n; ++i) {
    printf("%d> ", i+1); scanf("%d", a+i);
  }
}
void iarray_print(int *a, int n) {
  for(int i = 0; i < n; ++i) { printf(" %2d", a[i]); }
  printf("\n");
}
bool iarray_equal(int *a, int *b, int n) { // !!WITH BUG!!
  for(int i = 0; i <= n; ++i) {
    if(a[i] != b[i]) { return false; }
  }
  return true;
}
char *bool2str(bool b) { return b ? "true" : "false"; }
void expect_bool(bool b1, bool b2, char *msg) {
  printf("%s %s:%s %s\n", (b1==b2)?"OK":"NG",
         bool2str(b1), bool2str(b2), msg);
}
int main(void) {
  int a[] = {0, 1, 2, 3, 4, 5}, b[] = {9, 1, 2, 3, 4, 6};
  expect_bool(iarray_equal(a, b, 5), false, "01234 : 91234");
  expect_bool(iarray_equal(a+1, b+1, 5), false, "12345 : 12346");
  expect_bool(iarray_equal(a+1, b+1, 4), true, "1234 : 1234");
  expect_bool(iarray_equal(a+1, b+1, 3), true, "123 : 123");
  expect_bool(iarray_equal(a, b, 0), true, "[] : []");
  return 0;
}
void expect_int(int i1, int i2, char *msg) {
  printf("%s %d:%d %s\n", (i1==i2)?"OK":"NG", i1, i2, msg);
}
void expect_bool(double d1, double d2, char *msg) {
  printf("%s %g:%g %s\n", (d1==d2)?"OK":"NG", d1, d2, msg);
}
bool iarray_equal(int *a, int *b, int n) {
  for(int i = 0; i < n; ++i) {
    if(a[i] != b[i]) { return false; }
  }
  return true;
}
void expect_iarray(int *a, int *b, int n, char *msg) {
  printf("%s %s\n", iarray_equal(a, b, n)?"OK":"NG", msg);
  iarray_print(a, n); iarray_print(b, n);
}
#include <stdio.h>
// your iarray_max here
void expect_int(int i1, int i2, char *msg) {
  printf("%s %d:%d %s\n", (i1==i2)?"OK":"NG", i1, i2, msg);
}
int main(void) {
  int a[] = {9,0,0,1,2,3}, b[] = {-1,-3,-2,-4,-1};
  expect_int(iarray_max(a, 6), 9, "9 0 0 1 2 3");
  expect_int(iarray_max(a+1, 5), 3, "0 0 1 2 3");
  expect_int(iarray_max(b, 5), -1, "-1 -3 -2 -4 -1");
  return 0;
}
#include <stdio.h>
#include <stdbool.h>
// your iarray_revese here
bool iarray_equal(int *a, int *b, int n) {
  for(int i = 0; i < n; ++i) {
    if(a[i] != b[i]) { return false; }
  }
  return true;
}
void iarray_print(int *a, int n) {
  for(int i = 0; i < n; ++i) { printf(" %2d", a[i]); }
  printf("\n");
}
void expect_iarray(int *a, int *b, int n, char *msg) {
  printf("%s %s\n", iarray_equal(a, b, n)?"OK":"NG", msg);
  iarray_print(a, n); iarray_print(b, n);
}
int main(void) {
  int a[] = {8,5,2,4,1}, b[] = {1,4,2,5,8};
  iarray_reverse(a, 5); expect_iarray(a, b, 5, "85241 -> 14258");
  return 0;
}
#include <stdio.h>
#include <stdbool.h>
// your iarray_sort here
bool iarray_equal(int *a, int *b, int n) {
  for(int i = 0; i < n; ++i) {
    if(a[i] != b[i]) { return false; }
  }
  return true;
}
void iarray_print(int *a, int n) {
  for(int i = 0; i < n; ++i) { printf(" %2d", a[i]); }
  printf("\n");
}
void expect_iarray(int *a, int *b, int n, char *msg) {
  printf("%s %s\n", iarray_equal(a, b, n)?"OK":"NG", msg);
  iarray_print(a, n); iarray_print(b, n);
}
int main(void) {
  int a[] = {8,5,2,4,1}, b[] = {1,2,4,5,8}; 
  iarray_sort(a, 5); expect_iarray(a, b, 5, "85241 -> 12458");
  return 0;
}
#include <stdio.h>
#include <stdbool.h>
// your iarray_add here
bool iarray_equal(int *a, int *b, int n) {
  for(int i = 0; i < n; ++i) {
    if(a[i] != b[i]) { return false; }
  }
  return true;
}
void iarray_print(int *a, int n) {
  for(int i = 0; i < n; ++i) { printf(" %2d", a[i]); }
  printf("\n");
}
void expect_iarray(int *a, int *b, int n, char *msg) {
  printf("%s %s\n", iarray_equal(a, b, n)?"OK":"NG", msg);
  iarray_print(a, n); iarray_print(b, n);
}
int main(void) {
  int a[] = {8,5,2,4,1}, b[] = {1,1,2,2,3}, c[] = {9,6,4,6,4};
  iarray_add(a, b, 5); expect_iarray(a, c, 5, "85241+11223 -> 96464");
  return 0;
}
#include <stdio.h>
// your iarray_inject here
int iadd(int x, int y) { return x + y; }
int imax(int x, int y) { return (x > y) ? x : y; }
void expect_int(int i1, int i2, char *msg) {
  printf("%s %d:%d %s\n", (i1==i2)?"OK":"NG", i1, i2, msg);
}
int main(void) {
  int a[] = {8,5,2,4,1};
  expect_int(iarray_inject(a, 5, iadd), 20, "8+5+2+4+1");
  expect_int(iarray_inject(a, 5, imax), 8, "max(8,5,2,4,1)");
  return 0;
}
int iadd(int x, int y) { return x + y; }
int imax(int x, int y) { return (x > y) ? x : y; }
// ivec_demo.c --- int vector demonstration.
#include <stdio.h>
#include <stdlib.h>
void iarray_read(int *a, int n) {
  for(int i = 0; i < n; ++i) {
    printf("%d> ", i+1); scanf("%d", a+i);
  }
}
void iarray_print(int *a, int n) {
  for(int i = 0; i < n; ++i) { printf(" %2d", a[i]); }
  printf("\n");
}
int *ivec_new(int size) {
  int *a = (int*)malloc((size+1) * sizeof(int));
  a[0] = size; return a;
}
void ivec_read(int *a) { iarray_read(a+1, a[0]); }
void ivec_print(int *a) { iarray_print(a+1, a[0]); }
int *ivec_concat(int *a, int *b) {
  int *c = ivec_new(a[0]+b[0]);
  for(int i = 1; i <= a[0]; ++i) { c[i] = a[i]; }
  for(int i = 1; i <= b[0]; ++i) { c[i + a[0]] = b[i]; }
  return c;
}
int main(void) {
  int *a, *b, *c;
  a = ivec_new(3); ivec_read(a);
  b = ivec_new(2); ivec_read(b);
  c = ivec_concat(b, a); ivec_print(c);
  free(a); free(b); free(c);
  return 0;
}
// test_ivec_concat.c --- unit test for ivec_concat.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int *ivec_new(int size) {
  int *a = (int*)malloc((size+1) * sizeof(int));
  a[0] = size; return a;
}
int *ivec_concat(int *a, int *b) {
  int *c = ivec_new(a[0]+b[0]);
  for(int i = 1; i <= a[0]; ++i) { c[i] = a[i]; }
  for(int i = 1; i <= b[0]; ++i) { c[i + a[0]] = b[i]; }
  return c;
}
bool iarray_equal(int *a, int *b, int n) {
  for(int i = 0; i < n; ++i) {
    if(a[i] != b[i]) { return false; }
  }
  return true;
}
void iarray_print(int *a, int n) {
  for(int i = 0; i < n; ++i) { printf(" %2d", a[i]); }
  printf("\n");
}
void expect_iarray(int *a, int *b, int n, char *msg) {
  printf("%s %s\n", iarray_equal(a, b, n)?"OK":"NG", msg);
  iarray_print(a, n); iarray_print(b, n);
}
int main(void) {
  int a[] = {3,1,2,3}, b[] = {2,4,5}, c[] = {5,1,2,3,4,5};
  int *p = ivec_concat(a, b);
  expect_iarray(p, c, 6, "[1,2,3]+[4,5]=[1,2,3,4,5]");
  return 0;
}
// cdseq.h --- constant difference sequence API.
struct cdseq *cdseq_new(int s, int d);
int cdseq_get(struct cdseq *r);
void cdseq_free(struct cdseq *r);
// cdseq_demo.c -- cdseq demonstration.
#include <stdio.h>
#include "cdseq.h"
int main(void) {
  struct cdseq *s1 = cdseq_new(1, 2);
  struct cdseq *s2 = cdseq_new(0, 3);
  int i;
  for(i = 0; i < 6; ++i) {
    printf(" %2d", cdseq_get(s1));
    printf(" %2d", cdseq_get(s1));
    printf(" %2d", cdseq_get(s2));
  }
  printf("\n"); cdseq_free(s1); cdseq_free(s2);
  return 0;
}
// cdseq.c -- cdseq implementation.
#include <stdlib.h>
#include "cdseq.h"
struct cdseq { int value, diff; };
struct cdseq *cdseq_new(int s, int d) {
  struct cdseq *r = (struct cdseq*)malloc(sizeof(struct cdseq));
  r->value = s; r->diff = d; return r;
}
int cdseq_get(struct cdseq *r) {
  int v = r->value; r->value += r->diff; return v;
}
void cdseq_free(struct cdseq *r) {
  free(r);
}
// test_cdseq_1.c --- unit test for cdseq.
#include <stdio.h>
#include "cdseq.h"
void expect_int(int i1, int i2, char *msg) {
  printf("%s %d:%d %s\n", (i1==i2)?"OK":"NG", i1, i2, msg);
}
int main(void) {
  struct cdseq *s = cdseq_new(2, 3);
  expect_int(cdseq_get(s), 2, "2+3*0 = 2");
  expect_int(cdseq_get(s), 5, "2+3*1 = 5");
  expect_int(cdseq_get(s), 8, "2+3*2 = 8");
  return 0;
}