c1-1.c
/* code: c1-1.c (v1.18.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 () % 1000;
for (i = 0; i < ARRAY_SIZE; i++)
printf ("%03d ", a[i]);
return 0;
}
c1-2.c
/* code: c1-2.c (v1.18.00) */
#include <stdio.h>
int main () {
int i, j;
int a[3][4] = {
{100, 200, 300, 400},
{500, 600, 700, 800},
{900, 1000, 1100, 1200}
};
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
printf ("array[%d][%d]=%4d\n", i, j, a[i][j]);
}
}
return 0;
}
c1-3.c
/* code: c1-3.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
struct Student {
int id;
char grade;
float average;
};
typedef struct Student STUDENT_TYPE;
int main () {
STUDENT_TYPE a[5];
int i;
a[0].id = 100;
a[0].grade = 'A';
a[0].average = 510.20;
a[1].id = 101;
a[1].grade = 'C';
a[1].average = 350.00;
a[2].id = 105;
a[2].grade = 'B';
a[2].average = 450.50;
for (i = 0; i < 3; i++) {
printf ("%d ", a[i].id);
printf ("%c ", a[i].grade);
printf ("%2.2f\n", a[i].average);
}
return 0;
}
c1-4.c
/* code: c1-4.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 5000000
int main () {
int *array;
int i;
array = malloc (sizeof (int) * ARRAY_SIZE);
if (NULL == array) {
fprintf (stderr, "Error: malloc() \n");
exit (-1);
}
else {
for (i = 0; i < ARRAY_SIZE; i++) {
array[i] = rand () % 1000;
}
for (i = 0; i < 10; i++) {
printf ("%d ", array[i]);
}
free (array);
}
return 0;
}
c1-5.c
/* code: c1-5.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 13
/* ------------------------------------------- */
int linear_search (int array[], int n, int key) {
int i;
for (i = 0; i < n; i++) {
if (array[i] == key) {
return i;
}
}
return -1;
}
/* ------------------------------------------- */
void print_array (int array[], int n) {
int i;
for (i = 0; i < n; i++) {
printf ("%d ", array[i]);
}
printf ("\n");
}
/* ------------------------------------------- */
int main () {
int index, key;
int array[ARRAY_SIZE] = {
900, 990, 210, 50, 80, 150, 330,
470, 510, 530, 800, 250, 280
};
key = 800;
print_array (array, ARRAY_SIZE);
index = linear_search (array, ARRAY_SIZE, key);
if (index != -1) {
printf ("Found: %d (index:%d)\n", key, index);
}
else {
printf ("Not found: %d\n", key);
}
return 0;
}
c1-6.c
/* code: c1-6.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 13
/* ------------------------------------------- */
int binary_search (int array[], int num, int key) {
int middle, low, high;
low = 0;
high = num - 1;
while (low <= high) {
middle = (low + high) / 2;
if (key == array[middle]) {
return middle;
}
else if (key < array[middle]) {
high = middle - 1;
}
else {
low = middle + 1;
}
}
return -1;
}
/* ------------------------------------------- */
void print_array (int array[], int n) {
int i;
for (i = 0; i < n; i++) {
printf ("%d ", array[i]);
}
printf ("\n");
}
/* ------------------------------------------- */
int main () {
int index, key;
int array[ARRAY_SIZE] = {
50, 80, 150, 210, 250, 280, 330,
470, 510, 530, 800, 900, 990
};
key = 800;
print_array (array, ARRAY_SIZE);
index = binary_search (array, ARRAY_SIZE, key);
if (index != -1) {
printf ("Found: %d (index:%d)\n", key, index);
}
else {
printf ("Not found: %d\n", key);
}
return 0;
}
q1-1.c
/* code: q1-1.c (v1.18.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;
}
q1-2.c
/* code: q1-2.c (v1.18.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;
}
q1-3.c
/* code: q1-3.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
#include <search.h>
#define ARRAY_SIZE 10
/* ------------------------------------------ */
int int_compare (const void *va, const void *vb) {
int a, b;
a = *(int *) va;
b = *(int *) vb;
if (a < b) {
return (-1);
}
else if (a > b) {
return (1);
}
else {
return (0);
}
}
/* ------------------------------------------- */
void print_array (int array[], int n) {
int i;
for (i = 0; i < n; i++) {
printf ("%d ", array[i]);
}
printf ("\n");
}
/* ------------------------------------------- */
int main () {
int key;
int *r;
int array[ARRAY_SIZE] = {
10, 12, 16, 19, 28, 30, 38, 44, 70, 98
};
/* ordered array! */
key = 16;
print_array (array, ARRAY_SIZE);
r = (int *) bsearch (&key, array, ARRAY_SIZE, sizeof (int), int_compare);
if (r != NULL) {
printf ("Found: %d\n", key);
}
else {
printf ("Not found: %d\n", key);
}
return 0;
}
q1-4.c
/* code: q1-4.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
#include <search.h>
#define ARRAY_SIZE 100000000
/* ------------------------------------------ */
int int_compare (const void *va, const void *vb) {
int a, b;
a = *(int *) va;
b = *(int *) vb;
if (a < b) {
return (-1);
}
else if (a > b) {
return (1);
}
else {
return (0);
}
}
/* ------------------------------------------- */
void print_array (int array[], int n) {
int i;
for (i = 0; i < n; i++) {
printf ("%d ", array[i]);
}
printf ("\n");
}
/* ------------------------------------------- */
int main () {
int i, key;
int *r;
int *array;
array = malloc (sizeof (int) * ARRAY_SIZE);
printf ("random numbers (%d) ... ", ARRAY_SIZE);
fflush (stdout);
for (i = 0; i < ARRAY_SIZE; i++) {
array[i] = rand ();
}
printf ("done\n");
fflush (stdout);
printf ("sorting (%d) ... ", ARRAY_SIZE);
fflush (stdout);
qsort (array, ARRAY_SIZE, sizeof (int), int_compare);
printf ("done\n");
fflush (stdout);
key = array[5];
print_array (array, 10);
printf ("bsearch (%d)\n", ARRAY_SIZE);
fflush (stdout);
r = (int *) bsearch (&key, array, ARRAY_SIZE, sizeof (int), int_compare);
if (r != NULL) {
printf ("Found: %d\n", key);
}
else {
printf ("Not found: %d\n", key);
}
fflush (stdout);
free (array);
return 0;
}
q1-5.c
/* code: q1-5.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
/* ------------------------------------------ */
void shuffle (int *v, int n) {
int i, j, t;
for (i = 0; i < n; i++) {
j = rand () % n;
t = v[i];
v[i] = v[j];
v[j] = t;
}
}
/* ------------------------------------------ */
void rand_nd (int *v, int n) {
int i;
for (i = 0; i < n; i++) {
v[i] = i;
}
shuffle (v, n);
}
/* ------------------------------------------ */
int main () {
int i, n;
int *v;
n = 10;
v = malloc (sizeof (int) * n);
rand_nd (v, n);
for (i = 0; i < n; i++)
printf ("%d ", v[i]);
free (v);
return 0;
}
EOF