ex6-1.c

/* code: ex6-1.c   (v1.16.00) */
#include <stdio.h>

int main ()
{
  int a, b, c, d, e;
  int sum, avg;

  a = 30;
  b = 20;
  c = 10;
  d = 25;
  e = 15;
  sum = a + b + c + d + e;
  avg = sum / 5;
  printf ("%d\n", avg);

  return 0;
}




ex6-10.c

/* code: ex6-10.c   (v1.16.00) */
#include <stdio.h>
#include <string.h>

int main ()
{
  char s0[] = "aaaaa";
  char s1[] = "bbbbb";
  char s2[] = "aaaaaaa";
  int i;
  printf ("strcmp(str1, str2)\n");
  i = strcmp (s0, s0);
  printf ("[%s] [%s] (%d)\n", s0, s0, i);
  i = strcmp (s0, s1);
  printf ("[%s] [%s] (%d)\n", s0, s1, i);
  i = strcmp (s1, s0);
  printf ("[%s] [%s] (%d)\n", s1, s0, i);
  i = strcmp (s0, s2);
  printf ("[%s] [%s] (%d)\n", s0, s2, i);

  return 0;
}




ex6-2.c

/* code: ex6-2.c   (v1.16.00) */
#include <stdio.h>

int main ()
{
  int a[10];
  int i, sum, avg;

  a[0] = 30;
  a[1] = 20;
  a[2] = 10;
  a[3] = 25;
  a[4] = 15;
  sum = 0;
  for (i = 0; i < 5; i++)
    sum += a[i];

  avg = sum / 5;
  printf ("%d\n", avg);

  return 0;
}




ex6-3.c

/* code: ex6-3.c   (v1.16.00) */
#include <stdio.h>

int main ()
{
  int a[10] = { 30, 20, 10, 25, 15 };
  int i, sum, avg;

  sum = 0;
  for (i = 0; i < 5; i++)
    sum += a[i];

  avg = sum / 5;
  printf ("%d\n", avg);

  return 0;
}




ex6-4.c

/* code: ex6-4.c   (v1.16.00) */
#include <stdio.h>
#include <stdlib.h>

#define ARRAY_SIZE 10

int main ()
{
  int a[ARRAY_SIZE];
  int i;

  for (i = 0; i < ARRAY_SIZE; i++)
    a[i] = rand () % 100;

  for (i = 0; i < ARRAY_SIZE; i++)
    printf ("%03d ", a[i]);

  return 0;
}




ex6-5.c

/* code: ex6-5.c   (v1.16.00) */
#include <stdio.h>

int main ()
{
  int i, j;
  int a[3][4] = {
    {0, 10, 20, 30},
    {40, 50, 60, 70},
    {80, 90, 100, 110}
  };

  for (i = 0; i < 3; i++) {
    for (j = 0; j < 4; j++) {
      printf ("array[%d][%d]=%3d\n", i, j, a[i][j]);
    }
  }

  return 0;
}




ex6-6.c

/* code: ex6-6.c   (v1.16.00) */
#include <stdio.h>

int main ()
{
  int i, j, k;
  int a[2][3][4] = {
    {{0, 1, 2, 3},
     {4, 5, 6, 7},
     {8, 9, 10, 11}},
    {{0, 10, 20, 30},
     {40, 50, 60, 70},
     {80, 90, 100, 110}}
  };

  for (i = 0; i < 2; i++) {
    for (j = 0; j < 3; j++) {
      for (k = 0; k < 4; k++) {
	printf ("array[%d][%d][%d]=%3d\n", i, j, k, a[i][j][k]);
      }
    }
  }

  return 0;
}




ex6-7.c

/* code: ex6-7.c   (v1.16.00) */
#include <stdio.h>

int main ()
{
  char s[4];
  s[0] = 'O';
  s[1] = 'U';
  s[2] = 'J';
  s[3] = '\0';
  printf ("%s\n", s);

  return 0;
}




ex6-8.c

/* code: ex6-8.c   (v1.16.00) */
#include <stdio.h>

/* ------------------------------------------- */
void string_copy (char *target, char *source)
{
  int i;
  i = 0;
  while (source[i] != '\0') {
    target[i] = source[i];
    i++;
  }
  target[i] = '\0';
}

/* ------------------------------------------- */
int main ()
{
  char s[20] = "University";
  char t[20];

  string_copy (t, s);
  printf ("%s\n", t);

  return 0;
}




ex6-9.c

/* code: ex6-9.c   (v1.16.00) */
#include <stdio.h>
#include <string.h>

/* ------------------------------------------- */
int main ()
{
  char s[20] = "University";
  char t[20];
  strcpy (t, s);
  printf ("%s\n", t);

  return 0;
}




q6-1.c

/* code: q6-1.c   (v1.16.00) */
#include <stdio.h>

int main ()
{
  float a[5];
  int i;
  float sum, avg;

  a[0] = 30.0;
  a[1] = 20.0;
  a[2] = 10.0;
  a[3] = 25.0;
  a[4] = 15.0;
  sum = 0.0;
  for (i = 0; i < 5; i++)
    sum += a[i];

  avg = sum / 5.00;
  printf ("%f\n", avg);

  return 0;
}




q6-2.c

/* code: q6-2.c   (v1.16.00) */
#include <stdio.h>
#define TABLE 9
int main ()
{
  int i, j;
  int a[TABLE][TABLE];

  for (i = 0; i < TABLE; i++) {
    for (j = 0; j < TABLE; j++) {
      a[i][j] = (i + 1) * (j + 1);
    }
  }
  for (i = 0; i < TABLE; i++) {
    for (j = 0; j < TABLE; j++) {
      printf ("%02d ", a[i][j]);
    }
    printf ("\n");
  }
  return 0;
}




q6-3.c

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

int main ()
{
  int i, j, k;
  int array[2][3][4];

  for (i = 0; i < 2; i++) {
    for (j = 0; j < 3; j++) {
      for (k = 0; k < 4; k++) {
	array[i][j][k] = (rand () % 100) + 1;
      }
    }
  }
  for (i = 0; i < 2; i++) {
    for (j = 0; j < 3; j++) {
      for (k = 0; k < 4; k++) {
	printf ("%03d ", array[i][j][k]);
      }
      printf ("\n");
    }
    printf ("\n");
  }
  return 0;
}




q6-4.c

/* code: q6-4.c   (v1.16.00) */
#include <stdio.h>
#include <string.h>

/* ------------------------------------------- */
int main ()
{
  char s0[] = "aaaaa";
  char s1[] = "bbbbb";
  char s2[] = "aaaaaaa";
  int i;
  printf ("strncmp(str1, str2, 3)\n");
  i = strncmp (s0, s0, 3);
  printf ("[%s] [%s] (%d)\n", s0, s0, i);
  i = strncmp (s0, s1, 3);
  printf ("[%s] [%s] (%d)\n", s0, s1, i);
  i = strncmp (s1, s0, 3);
  printf ("[%s] [%s] (%d)\n", s1, s0, i);
  i = strncmp (s0, s2, 3);
  printf ("[%s] [%s] (%d)\n", s0, s2, i);

  return 0;
}




q6-5.c

/* code: q6-5.c   (v1.16.00) */
#include <stdio.h>
#include <string.h>

int main ()
{
  char s0[] = "abcdefg";
  int i;
  i = strlen (s0);
  printf ("[%s] (%d)\n", s0, i);
  return 0;
}




q6-6.c

/* code: q6-6.c   (v1.16.00) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 10

struct student
{
  int id;
  char grade;
  char name[128];
};
typedef struct student STUDENT_TYPE;

/* ------------------------------------------- */
int main ()
{
  STUDENT_TYPE db1[MAX];
  STUDENT_TYPE *db2[MAX];
  int i;

  printf ("database1\n");
  for (i = 0; i < MAX; i++) {
    db1[i].id = 100 + i;
    db1[i].grade = 'a' + rand () % 5;
    strcpy (db1[i].name, "John Doe");
    printf ("%d %c %s\n", db1[i].id, db1[i].grade, db1[i].name);
  }

  printf ("\n");
  printf ("database2\n");
  for (i = 0; i < MAX; i++) {
    db2[i] = malloc (sizeof (STUDENT_TYPE));
    db2[i]->id = 200 + i;
    db2[i]->grade = 'a' + rand () % 5;
    strcpy (db2[i]->name, "John Doe");
    printf ("%d %c %s\t\t", db2[i]->id, db2[i]->grade, db2[i]->name);
    printf ("%d %c %s\n", (*db2[i]).id, (*db2[i]).grade, (*db2[i]).name);
  }
  for (i = 0; i < MAX; i++) {
    free (db2[i]);
  }


  return 0;
}




EOF