CS 1723, First Exam, Selected Answers, 30 Sept, 1998 PROBLEM 1:============================================================= runner% cat exam1_3.c #include #include struct a_tag { char ch; int num; }; void main(void) { struct a_tag a[26]; int i; for (i = 0; i < 26; i++) { a[i].ch = 'a' + i; a[i].num = i + 1; } for (i = 0; i < 26; i++) printf("Index: %2i, ch: %c, num: %2i\n", i, a[i].ch, a[i].num); } runner% cc -o exam1_3 exam1_3.c runner% exam1_3 Index: 0, ch: a, num: 1 Index: 1, ch: b, num: 2 Index: 2, ch: c, num: 3 ... (21 lines missing) Index: 24, ch: y, num: 25 Index: 25, ch: z, num: 26 PROBLEM 3:============================================================= runner% cat main.c /* * main.c * simple command interpreter * commands: 'i' for insert, 'd' for delete */ #include #include "queue.h" void main(void) { char c; while (1) { c = getchar(); switch (c) { case 'i': if (!full()) { c = getchar(); insert(c); printf("Inserted: %c\n", c); } else printf("No room\n"); break; case 'd': if (!empty()) { c = delete(); printf("Deleted: %c\n", c); } else printf("Empty queue\n"); break; case 'q': exit(0); default: printf("Illegal command: %c\n", c); break; } /* end of case */ while ((c = getchar()) != '\n') ; } } --------------------------------------------------------- runner% cat queue.h /* * queue.h -- queue header file */ typedef char Queuetype; Queuetype delete(void); void insert(Queuetype); int empty(void); int full(void); --------------------------------------------------------- runner% cat queue.c /* * queue.c -- queue implementation */ #include #include "queue.h" #define Q_SIZE 4 /* maximum queue size */ static Queuetype q[Q_SIZE]; /* actual queue */ static int fp = 0; /* front pointer */ static int rp = 0; /* rear pointer */ static int qs = 0; /* size of queue */ Queuetype delete(void) { if (!empty()) { qs--; fp = (fp + 1)%Q_SIZE; return q[fp]; } else { printf("Underflow\n"); return 0; } } void insert(Queuetype c) { if (!full()) { qs++; rp = (rp + 1)%Q_SIZE; q[rp] = c; } else printf("Overflow\n"); } int empty(void) { return qs == 0; } int full(void) { return qs == Q_SIZE; } --------------------------------------------------------- runner% queue ia Inserted: a ib Inserted: b ic Inserted: c d Deleted: a d Deleted: b d Deleted: c q runner% PROBLEM 4.============================================================= runner% cat exam4.c #include #include #include void strcpy1(char *, char *); void main(void) { char s[] = "UTSA"; char *t; t = (char *) malloc(strlen(s) + 1); strcpy1(t, s); printf("s:""%s"", t:""%s""\n", s, t); printf(" t[1], char: %c\n", t[1]); printf(" *t, char: %c\n", *t); printf(" &t[0], string: %s\n", &t[0]); printf(" t+2, string: %s\n", t+2); printf("*(t+2), char: %c\n", *(t+2)); } void strcpy1(char *a, char *b) { while (*a++ = *b++) ; } runner% lint -m -u exam4.c function returns value which is always ignored printf runner% cc -o exam4 exam4.c runner% exam4 s:UTSA, t:UTSA t[1], char: T *t, char: U &t[0], string: UTSA t+2, string: SA *(t+2), char: S runner%