CS108
NB: Answer Question 1 and EITHER Question 2 OR Question 3. That is, you must answer Question 1, and one other question.
Question 1. (There are 4 parts to this question).
1.
wc -l
: counts the number of lines entered from standard input
but
cd
: change the current working directory to the default (HOME) directory
ls -la
csh
setenv EDITOR emacs
ps
grep path essay*
chmod go+rx *
nohup myprog >& myfile &
[10 marks]
b) The UNIX commands sort
and grep
are examples of filters.
Explain what a filter is. What rules must be followed in order for a command or
program to be eligible for use as a filter?
[15 marks]
c) What is a process? Explain the typical life cycle of a process in the UNIX environment, and describe the sorts of events that cause a process to change from one state to another.
[15 marks]
d) While running an interactive job a novice UNIX user pressed CTRL-Z
. Then
the UNIX command line prompt was displayed. What happened? What needs to be
done to restart the job?
[10 marks]
2
[5 marks]
b) Write a function is_member(char x, char *string)
which returns 1
(one) if x
occurs anywhere in the character array, string
, and
0 otherwise. You must use pointers to traverse the array.
[10 marks]
c) Explain the use of the following constructs
(*p)++
(*string++)[0]
p->x
[12 marks]
d) Write a program in ANSI C which counts the number of characters, words and lines read from standard input. The program will print out the totals, but will not print out the input that the user has typed. Additionally, the program should print out the longest word that was typed in. If there is more than one word of the same maximum length, it is sufficient to print the first one only.
This example Input
This is an
example of the sorts of terminal
inputs that could be typed in
would produce this example Output
Chars = 74, words = 15, lines = 3
Longest word is terminal
[23 marks]
3
N.B. The program does not contain any logical errors. Only one of the functions does not contain at least one error.
You can mark the errors, and write the explanation, on the attached worksheet. Be sure to write your candidate number on the worksheet.
#include <stdio.h> #include <ctype.h> int getcode(void); int checkcode(float); void print_error(char [], int); void make_string(char [], char []); main() /* control function */ { int code, c; char e_string[10] = " "; if ((d = checkcode(getcode())) != 3) { print_error(e_string, d); } else printf("Welcome...\n"); } int getcode(void) /* obtain code */ { int code = 0; int c; while ((c = getchar()) != EOF && c != '\n') { if (! isdigit(c)) return -1; /* non-digit keyed in */ else { code = (code * 10) + (c - '0'); /* build up number */ } /* after code keyed in, is it in range? */ if (code > 9999 || code <= 999) return -2; /* not in range */ else return code; } int checkcode(float code) /* validate code */ { if (code < 0) { return code; } return code % 13; } /* original version of checkcode() commented out int checkcode(float code) /* validates code */ { if (code <= 0) return code; else return code % 13; } */ print_error(char e_string[], int e_type) /* print the message */ { char e_string[]; char reason[50]; e_string[] = "Error: "; switch (e_type) { case -1: make_string(e_string, "Warning: "); make_string(reason, "Code is not all numeric") break; case -2: make_string(reason, "Code is not right length"); break; default: make_string(reason, "Invalid code entered"); break; } printf("%s %s\n", e_string, reason); } void make_string(char e_string[], char x[]) { int i; for (i = 0; x[i] != '\0'; i++) e_string[i] = x[i]; e_string[i] = '\0'; }[28 marks]
b) Write an ANSI C function called getstring
that reads a line of input
from the terminal, terminated by the end of file character or newline, and
stores it in a character array called buffer
, which you should declare.
What result should getstring
return, in order for it to be useful for
functions that use it?
[12marks]
c) Describe the behaviour of the following C function fragment.
(array[]
is an integer array containing integers, i
, c
and
d
are declared as integers. i
contains the number of array cells
in array[]
):
c = d = 0; do { c += array[d++]; } while (d < i);What would the behaviour have been if the array subscript was coded
++d
instead of d++
?
[10 marks]