You are on page 1of 6

Q1.

not a question
Q2.
in S1, it runs the case that meets the value of ch and then stops.
u >> you
o >> ohh
e >> eee
in S2, it runs the case that meets the value of ch and ALL SUBSEQUENT cases
after it.
u >> you
o >> ohh you
e >> eee eye ohh you
Q3.
if (ch == a || ch == e || ch == i || ch == o || ch == u) {
printf("vowel");
} else {
printf("consonant");
}
Q4.
type = isdigit(ch) ? digit : non-digit;
* should only use conditional statements for simple expressions
Can be chained:
type = isdigit(ch) ? (ch == a ? this is a : this is not a) : non-digit;

char ch; // character


char *type; // string
Q5.
ch = getchar();
while (ch != EOF) {
putchar(ch);
ch = getchar();

}
Q6.
atoi() less flexible than using sscanf() - can read multiple inputs.
(a)
No, since the code following the for loop only extends for one line.
(b)
scans for an integer in the string stored in argv[1]. The integer is stored in N.
(c)
Could just use scanf(str, &ptr), which causes the input prompt to occur when you run
the program rather than before you run the program.
Since <stdlib.h> is imported, could use strtod(str, &ptr) functionality. Since this reads
it as a float you may need to typecast the result to an integer afterwards.
(d)
assert(argc > 1); checks that you have given input to the program before runtime
"Error: no input integer found on start-up of program"
assert(N > 0); checks that the value of N has changed from its default value
"Error: input integer cannot be <= 0"
assert(a != NULL); checks that a was able to successfully allocated memory in the
heap.
"Error: program was unable to allocate memory in the heap for array"
Could replace them with perror(str) or printf(str) followed by an exit(EXIT_FAILURE)
for more meaningful error messages.
(e)
undefined since even the 1st for loop for setting 'all' elements to 1 initialises its
counter from i = 2.
(f)
all the for loops initialise their counters as i = 2, so a[0] and a[1] are never read.
(g)
<- INTERESTING
prints out prime numbers using the "Sieve of Eratosthenes" method. The algorithm
starts on 2 and marks all multiples of 2 up to the value of N as not prime (when you

do a[i*j] = 0). Then the loop goes to the next prime, and so forth until i == N.
Q7.
argc: records number of input arguments you gave to the program before runtime.
This is used to keep track of the number of string elements in argv[].
e.g.
$ ./test 1 2 3
causes:
argc = 4
argv[] = ["./test", "1", "2", "3"];
argv: array of strings. Stores all the arguments you typed in.
stdin: (standard input) a channel in which input data can be given to the program.
The standard form of input is IMPLEMENTATION SPECIFIC, but often it is related to
a device (most commonly a keyboard).
Stream: abstraction of a construct that allows you to send or receive an
UNKNOWN sequence of bytes.
stdout: (standard output) a channel in which the program sends output data through.
Implementation specific, but often to a monitor.
stderr: (standard error) an output channel specific for sending error messages
through. Made separate to stdout because it can be useful to separate error
information (for devs) from other output information (for users).
stderr: for logging debugging output
exit: terminates the program. Requires an integer as a return parameter since main()
is an int.
Q8.
(a)
argc: 4
argv[0]: ./myprog
argv[1]: hello
argv[2]: there,
argv[3]: John Shepherd

<- INTERESTING

// quotes (single or double) allow for string format

> is Shell syntax, so it isnt accepted as an argument


\> is required to get > as an argument

gets printed to stdout, stdout is redirected by the > to myFile


(b)
stdout
(c)
stdin
Q9.
(a) Incorrect - correct one should be (b)
(b) Correct - inputs to stdin, an integer into the variable x
(c) Correct - outputs to stdout, the contents of the variable x
(d) Correct - identical to (b)
(e) Incorrect (most likely) - stderr is an output stream, fscanf demands input
It might work? But stderr is just an output buffer.
(f) Correct - identical to (c)
(f) Correct - like (c), but prints to stderr stream instead
Q10.
void swap (a, i, j) {
array_type temp = a[i];
a[i] = a[j];
a[j] = temp;
}
Q11.
????
Q12.
(a) N times
(b) 1 time (0 times doesnt count since the search function would be unsuccessful)
(c) ????
Pre- and post- conditions are the requirements of the input parameters, the
domain of the output parameters
Pre-conditions: defined(integer, val) && defined(integer, a[0..N-1]) && N >= 0

Post-condition: i within [0..N] && N >= 0


(d)
int search (int val, int a[], int N) {
int i = 0;
while (i < N) {
if (a[i] == val) {
break;
}
i++;
}
int result;
if (i == N) {
result = -1;
} else {
result = i;
}
return result;
}
Q13. ???? (not sure)
(a.i) selection sort in increasing order
Always n times
(a.ii)
executions = sum of the positioning of each element (algorithm: n - j + 1)
- e.g. for 4 elements: (3-1 +1) + (3-2 +1) + (3-3 +1) = 3 + 2 + 1 = 6
- this suggests: executions = { sum from k = 1 to k = n - 1 }
// n is num elements
(a.iii)
at best, 0 times, meaning that a[] is already sorted in increasing order
(b.i)
Always n times
(b.ii)
Same as (a.ii)
(b.iii)
Same as (b.ii); occurs when the list is in reverse order

(c)
Best: already ordered
Worst: reverse order
(d)
selection sort in increasing order
Post-condition: a[] sorted in increasing order (????)
Q14.
Abstract types are combinations of normal data types, alongside complementing
functionality where the normal types interact with one another to affect the abstract
types data.
ADTs useful for abstracting data allowing for reduced complexity of code.
(????) First class types are types that (something about extended privileges ->
extended usage)
Q15. I already did this in Mandelbrot
struct _complex *Complex;
Complex newComplex (double real, double imag);
Complex freeComplex (Complex c);
void printComplex (Complex c);
Complex addComplex (Complex c1, Complex c2);
Complex subtractComplex (Complex c1, Complex c2);
Complex multComplex (Complex c1, Complex c2);
Complex divComplex (Complex c1, Complex c2);
Complex complexRtOfUnity (Complex c);
// Complex complexToPolar (Complex c1);
Q16. This linked list question is pretty easy and the hardest bit in it was already done
in the lab exercises.

You might also like