ex9-1.c

/* code: ex9-1.c   (v1.20.00) */
#include <stdio.h>
#include <stdlib.h>

int main () {
  FILE *fptr;
  fptr = fopen ("ex9-1-output.txt", "w");
  fprintf (fptr, "The Open University of Japan\n");
  fclose (fptr);
  return 0;
}




ex9-2.c

/* code: ex9-2.c   (v1.20.00) */
#include <stdio.h>
#include <stdlib.h>

int main () {
  FILE *fptr;
  if (NULL == (fptr = fopen ("ex9-2-output.txt", "w"))) {
    fprintf (stderr, "ERROR: Can not open file [output2.txt]");
    exit (-1);
  }
  fprintf (fptr, "The Open University of Japan\n");
  fclose (fptr);
  return 0;
}




ex9-3.c

/* code: ex9-3.c   (v1.20.00) */
#include <stdio.h>
#include <stdlib.h>

#define IRIS_DATA "iris.dat"

int main () {
  FILE *fptr;
  float sl, sw, pl, pw;
  float s_sl, s_sw, s_pl, s_pw;
  char name[128];
  int n;

  if (NULL == (fptr = fopen (IRIS_DATA, "r"))) {
    fprintf (stderr, "ERROR: Can not open file [%s]", IRIS_DATA);
    exit (-1);
  }
  n = 0;
  s_sl = s_sw = s_pl = s_pw = 0.0;
  while (EOF != fscanf (fptr, "%f,%f,%f,%f,%s", &sl, &sw, &pl, &pw, name)) {
    s_sl += sl;
    s_sw += sw;
    s_pl += pl;
    s_pw += pw;
    n++;
  }
  printf ("iris data : %d\n", n);
  printf ("avg. sepal length: %f\n", s_sl / (float) n);
  printf ("avg. sepal width : %f\n", s_sw / (float) n);
  printf ("avg. petal length: %f\n", s_pl / (float) n);
  printf ("avg. petal width : %f\n", s_pw / (float) n);
  fclose (fptr);

  return 0;
}




q9-1.c

/* code: q9-1.c   (v1.20.00) */
#include <stdio.h>
#include <stdlib.h>

#define IRIS_DATA "iris.dat"
#define MAX_ARRAY 256

struct iris {
  float sl, sw, pl, pw;
  char name[128];
};
typedef struct iris IRIS_TYPE;

/* ------------------------------------------- */
int read_iris_data (IRIS_TYPE data[], int num, char *filename) {
  FILE *fptr;
  float sl, sw, pl, pw;
  char name[128];
  int n;
  if (NULL == (fptr = fopen (filename, "r"))) {
    fprintf (stderr, "ERROR: Can not open file [%s]", filename);
    exit (-1);
  }
  n = 0;
  while (EOF != fscanf (fptr, "%f,%f,%f,%f,%s", &sl, &sw, &pl, &pw, name)) {
    data[n].sl = sl;
    data[n].sw = sw;
    data[n].pl = pl;
    data[n].pw = pw;
    sprintf (data[n].name, "%s", name);
    n++;
    if (n >= num) {
      fprintf (stderr, "ERROR: Not enough array size");
      exit (-1);
    }
  }
  fclose (fptr);
  return n;
}

/* ------------------------------------------- */
IRIS_TYPE find_iris_avg (IRIS_TYPE data[], int num) {
  IRIS_TYPE avg;
  int i;
  float s_sl, s_sw, s_pl, s_pw;

  s_sl = s_sw = s_pl = s_pw = 0.0;
  for (i = 0; i < num; i++) {
    s_sl += data[i].sl;
    s_sw += data[i].sw;
    s_pl += data[i].pl;
    s_pw += data[i].pw;
  }

  avg.sl = s_sl / (float) num;
  avg.sw = s_sw / (float) num;
  avg.pl = s_pl / (float) num;
  avg.pw = s_pw / (float) num;
  sprintf (avg.name, "%s", "average");
  return avg;
}

/* ------------------------------------------- */
int main () {
  int num;
  IRIS_TYPE iris_data[MAX_ARRAY];
  IRIS_TYPE avg;

  num = read_iris_data (iris_data, MAX_ARRAY, IRIS_DATA);
  avg = find_iris_avg (iris_data, num);
  printf ("iris data : %d\n", num);
  printf ("avg. sepal length: %f\n", avg.sl);
  printf ("avg. sepal width : %f\n", avg.sw);
  printf ("avg. petal length: %f\n", avg.pl);
  printf ("avg. petal width : %f\n", avg.pw);

  return 0;
}




q9-2.c

/* code: q9-2.c   (v1.20.00) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ROW  256
#define COL  256
#define FILTER_SIZE 3

struct pgm {
  int row;
  int col;
  int max;
  float pixel[ROW][COL];
};
typedef struct pgm PGM_TYPE;


/* ------------------------------------------- */
void pgm_read (PGM_TYPE * image, char *filename) {
  FILE *infile;
  int i, j;
  char magic_number[32];

  if (NULL == (infile = fopen (filename, "r"))) {
    fprintf (stderr, "Can not open file [%s]", filename);
    exit (-1);
  }
  fscanf (infile, "%s", magic_number);
  fscanf (infile, "%d", &image->col);
  fscanf (infile, "%d", &image->row);
  fscanf (infile, "%d", &image->max);
  printf ("image:%s [%dx%d] (%d)\n",
	  magic_number, image->col, image->row, image->max);
  if (strcmp ("P2", magic_number)) {
    fprintf (stderr, "Not PGM(P2) file!");
    exit (-2);
  }
  for (i = 0; i < image->row; i++) {
    for (j = 0; j < image->col; j++) {
      fscanf (infile, "%f", &image->pixel[i][j]);
    }
  }
  fclose (infile);
}

/* ------------------------------------------- */
void pgm_write (PGM_TYPE * image, char *filename) {
  FILE *outfile;
  int i, j;

  if (NULL == (outfile = fopen (filename, "w"))) {
    fprintf (stderr, "Can not open file [%s]", filename);
    exit (-1);
  }
  fprintf (outfile, "%s\n", "P2");
  fprintf (outfile, "%d ", image->col);
  fprintf (outfile, "%d \n", image->row);
  fprintf (outfile, "%d \n", image->max);

  for (i = 0; i < image->row; i++) {
    for (j = 0; j < image->col; j++) {
      fprintf (outfile, "%2d ", (int) image->pixel[i][j]);
    }
    fprintf (outfile, "\n");
  }
  fclose (outfile);
}

/* ------------------------------------------- */
void convolution (PGM_TYPE * image_input, PGM_TYPE * image_output) {
  int i, j, k, l, px, py, sum;
  float dsum;

  float filter[FILTER_SIZE][FILTER_SIZE] = {
    {-1.0, -1.0, -1.0},
    {-1.0, +8.0, -1.0},
    {-1.0, -1.0, -1.0}
  };

  image_output->row = image_input->row;
  image_output->col = image_input->col;
  image_output->max = image_input->max;
  px = (FILTER_SIZE - 1) / 2;
  py = (FILTER_SIZE - 1) / 2;
  dsum = 0;
  for (i = 0 + px; i < image_input->row - px; i++) {
    for (j = 0 + py; j < image_input->col - py; j++) {
      dsum = 0.0;
      for (k = 0; k < FILTER_SIZE; k++) {
	for (l = 0; l < FILTER_SIZE; l++) {
	  dsum += filter[k][l] * image_input->pixel[i - px + k][j - py + l];
	}
      }
      sum = (int) dsum;
      if (sum > image_input->max) {
	sum = image_input->max;
      }
      if (sum < 0) {
	sum = 0;
      }
      image_output->pixel[i][j] = sum;
    }
  }
}

/* ------------------------------------------- */
int main () {
  PGM_TYPE *pgm_input;
  PGM_TYPE *pgm_output;

  pgm_input = malloc (sizeof (PGM_TYPE));
  pgm_output = malloc (sizeof (PGM_TYPE));

  pgm_read (pgm_input, "sample.pgm");
  convolution (pgm_input, pgm_output);
  pgm_write (pgm_output, "q9-2-laplacian8.pgm");

  free (pgm_input);
  free (pgm_output);

  return 0;
}




q9-3.c

/* code: q9-3.c   (v1.20.00) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ROW  256
#define COL  256
#define FILTER_SIZE 3

struct pgm {
  int row;
  int col;
  int max;
  float pixel[ROW][COL];
};
typedef struct pgm PGM_TYPE;


/* ------------------------------------------- */
void pgm_read (PGM_TYPE * image, char *filename) {
  FILE *infile;
  int i, j;
  char magic_number[32];

  if (NULL == (infile = fopen (filename, "r"))) {
    fprintf (stderr, "Can not open file [%s]", filename);
    exit (-1);
  }
  fscanf (infile, "%s", magic_number);
  fscanf (infile, "%d", &image->col);
  fscanf (infile, "%d", &image->row);
  fscanf (infile, "%d", &image->max);
  printf ("image:%s [%dx%d] (%d)\n",
	  magic_number, image->col, image->row, image->max);
  if (strcmp ("P2", magic_number)) {
    fprintf (stderr, "Not PGM(P2) file!");
    exit (-2);
  }
  for (i = 0; i < image->row; i++) {
    for (j = 0; j < image->col; j++) {
      fscanf (infile, "%f", &image->pixel[i][j]);
    }
  }
  fclose (infile);
}

/* ------------------------------------------- */
void pgm_write (PGM_TYPE * image, char *filename) {
  FILE *outfile;
  int i, j;

  if (NULL == (outfile = fopen (filename, "w"))) {
    fprintf (stderr, "Can not open file [%s]", filename);
    exit (-1);
  }
  fprintf (outfile, "%s\n", "P2");
  fprintf (outfile, "%d ", image->col);
  fprintf (outfile, "%d \n", image->row);
  fprintf (outfile, "%d \n", image->max);

  for (i = 0; i < image->row; i++) {
    for (j = 0; j < image->col; j++) {
      fprintf (outfile, "%2d ", (int) image->pixel[i][j]);
    }
    fprintf (outfile, "\n");
  }
  fclose (outfile);
}

/* ------------------------------------------- */
void convolution (PGM_TYPE * image_input, PGM_TYPE * image_output) {
  int i, j, k, l, px, py, sum;
  float dsum;

  float filter[FILTER_SIZE][FILTER_SIZE] = {
    {0.0, -1.0, -2.0},
    {1.0, 0.0, -1.0},
    {2.0, 1.0, 0.0}
  };

  image_output->row = image_input->row;
  image_output->col = image_input->col;
  image_output->max = image_input->max;
  px = (FILTER_SIZE - 1) / 2;
  py = (FILTER_SIZE - 1) / 2;
  dsum = 0;
  for (i = 0 + px; i < image_input->row - px; i++) {
    for (j = 0 + py; j < image_input->col - py; j++) {
      dsum = 0.0;
      for (k = 0; k < FILTER_SIZE; k++) {
	for (l = 0; l < FILTER_SIZE; l++) {
	  dsum += filter[k][l] * image_input->pixel[i - px + k][j - py + l];
	}
      }
      sum = (int) dsum;
      if (sum > image_input->max) {
	sum = image_input->max;
      }
      if (sum < 0) {
	sum = 0;
      }
      image_output->pixel[i][j] = sum;
    }
  }
}

/* ------------------------------------------- */
int main () {
  PGM_TYPE *pgm_input;
  PGM_TYPE *pgm_output;

  pgm_input = malloc (sizeof (PGM_TYPE));
  pgm_output = malloc (sizeof (PGM_TYPE));

  pgm_read (pgm_input, "sample.pgm");
  convolution (pgm_input, pgm_output);
  pgm_write (pgm_output, "q9-3-sobel-d.pgm");

  free (pgm_input);
  free (pgm_output);

  return 0;
}




q9-4.c

/* code: q9-4.c   (v1.20.00) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ROW  256
#define COL  256
#define FILTER_SIZE 3

struct pgm {
  int row;
  int col;
  int max;
  float pixel[ROW][COL];
};
typedef struct pgm PGM_TYPE;


/* ------------------------------------------- */
void pgm_read (PGM_TYPE * image, char *filename) {
  FILE *infile;
  int i, j;
  char magic_number[32];

  if (NULL == (infile = fopen (filename, "r"))) {
    fprintf (stderr, "Can not open file [%s]", filename);
    exit (-1);
  }
  fscanf (infile, "%s", magic_number);
  fscanf (infile, "%d", &image->col);
  fscanf (infile, "%d", &image->row);
  fscanf (infile, "%d", &image->max);
  printf ("image:%s [%dx%d] (%d)\n",
	  magic_number, image->col, image->row, image->max);
  if (strcmp ("P2", magic_number)) {
    fprintf (stderr, "Not PGM(P2) file!");
    exit (-2);
  }
  for (i = 0; i < image->row; i++) {
    for (j = 0; j < image->col; j++) {
      fscanf (infile, "%f", &image->pixel[i][j]);
    }
  }
  fclose (infile);
}

/* ------------------------------------------- */
void pgm_write (PGM_TYPE * image, char *filename) {
  FILE *outfile;
  int i, j;

  if (NULL == (outfile = fopen (filename, "w"))) {
    fprintf (stderr, "Can not open file [%s]", filename);
    exit (-1);
  }
  fprintf (outfile, "%s\n", "P2");
  fprintf (outfile, "%d ", image->col);
  fprintf (outfile, "%d \n", image->row);
  fprintf (outfile, "%d \n", image->max);

  for (i = 0; i < image->row; i++) {
    for (j = 0; j < image->col; j++) {
      fprintf (outfile, "%2d ", (int) image->pixel[i][j]);
    }
    fprintf (outfile, "\n");
  }
  fclose (outfile);
}

/* ------------------------------------------- */
void convolution (PGM_TYPE * image_input, PGM_TYPE * image_output) {
  int i, j, k, l, px, py, sum;
  float dsum;

  float filter[FILTER_SIZE][FILTER_SIZE] =
    { {1.00 / 16.00, 2.00 / 16.00, 1.00 / 16.00},
  {2.00 / 16.00, 4.00 / 16.00, 2.00 / 16.00},
  {1.00 / 16.00, 2.00 / 16.00, 1.00 / 16.00}
  };

  image_output->row = image_input->row;
  image_output->col = image_input->col;
  image_output->max = image_input->max;
  px = (FILTER_SIZE - 1) / 2;
  py = (FILTER_SIZE - 1) / 2;
  dsum = 0;
  for (i = 0 + px; i < image_input->row - px; i++) {
    for (j = 0 + py; j < image_input->col - py; j++) {
      dsum = 0.0;
      for (k = 0; k < FILTER_SIZE; k++) {
	for (l = 0; l < FILTER_SIZE; l++) {
	  dsum += filter[k][l] * image_input->pixel[i - px + k][j - py + l];
	}
      }
      sum = (int) dsum;
      if (sum > image_input->max) {
	sum = image_input->max;
      }
      if (sum < 0) {
	sum = 0;
      }
      image_output->pixel[i][j] = sum;
    }
  }
}

/* ------------------------------------------- */
int main () {
  PGM_TYPE *pgm_input;
  PGM_TYPE *pgm_output;

  pgm_input = malloc (sizeof (PGM_TYPE));
  pgm_output = malloc (sizeof (PGM_TYPE));

  pgm_read (pgm_input, "sample.pgm");
  convolution (pgm_input, pgm_output);
  pgm_write (pgm_output, "q9-4-gaussian.pgm");

  free (pgm_input);
  free (pgm_output);

  return 0;
}




EOF