C Basics: Types, Control and Functions · Write code · 3 marks
Write the function count_digits. It must work for every int, including INT_MIN, so it must not negate n.
// count_digits(n) returns the number of decimal digits of |n|.
// The number 0 has one digit.
int count_digits(int n);For example, count_digits(0) is 1, count_digits(-123) is 3 and count_digits(1000) is 4.
int count_digits(int n) {
int digits = 0;
do {
digits++;
n /= 10;
} while (n != 0);
return digits;
}A do-while loop runs the body once before testing, which makes 0 count as one digit without a special case. Division truncates toward zero, so n /= 10 works for negative numbers too and never overflows. One mark for the loop, one for handling 0, one for handling negative numbers.
Pointers, Arrays and Pointer Arithmetic · Write code · 4 marks
An array const char *words[] of strings is to be sorted with qsort. Write the comparator below. It may use strlen and strcmp from <string.h>.
// cmp_len(x, y) is a qsort comparator for an array of const char *.
// Strings are ordered by length, shortest first; strings of the same
// length are ordered alphabetically (as by strcmp).
// Returns <0, 0 or >0 as the string at x is before, equal to or after
// the string at y.
// requires: x and y point to elements of type const char *
int cmp_len(const void *x, const void *y);int cmp_len(const void *x, const void *y) {
const char *a = *(const char *const *)x;
const char *b = *(const char *const *)y;
size_t la = strlen(a);
size_t lb = strlen(b);
if (la != lb) {
return la < lb ? -1 : 1;
}
return strcmp(a, b);
}qsort passes the address of each element, and each element is a const char *, so the arguments are pointers to pointers and must be cast and dereferenced once to get the strings. Comparing lengths with a conditional avoids the unsigned subtraction la - lb. Marks: correct cast and dereference (2), length order (1), tie-break (1).
C Strings and Buffers · Predict the output · 2 marks
What does this program print?
#include <stdio.h>
int main(void) {
int a = -1;
int b = -1;
int n = sscanf("42 abc", "%d %d", &a, &b);
printf("%d %d %d\n", n, a, b);
char word[8];
n = sscanf(" hello world", "%7s", word);
printf("%d [%s]\n", n, word);
n = sscanf("7,8", "%d %d", &a, &b);
printf("%d %d %d\n", n, a, b);
return 0;
}1 42 -1
1 [hello]
1 7 -1sscanf returns the number of conversions that succeeded and stops at the first failure, leaving the remaining variables untouched. abc is not an integer, and neither is ,8 (a comma is not whitespace). %s skips leading blanks and reads up to the next one.
The Stack, the Heap and Dynamic Memory · Multiple choice · 2 marks
Assume malloc succeeds. Which is the first line of this code whose execution has undefined behaviour?
int *a = malloc(3 * sizeof(int));
for (int i = 0; i < 3; i++) {
a[i] = i + 1;
}
int *end = a + 3;
int x = *end;
int sum = a[0] + a[1] + a[2];
free(a);Line 5
Line 6
Line 7
Line 8
Answer: (B)
Line 6. A pointer one element past the end of an array (line 5) may be formed and compared, but it may not be dereferenced: *end reads memory outside the three-int block. Lines 7 and 8 are fine.
File I/O and the Standard Library · Write code · 2 marks
Write the function below. It must report failure if any of the file operations fails, including the final close, and must not leave the file open.
// append_line(path, text) appends text and then a newline to the
// file path, creating the file if it does not exist. Returns 0
// on success and -1 on failure.
// requires: text is a valid string
// effects: writes to a file
int append_line(const char *path, const char *text);int append_line(const char *path, const char *text) {
FILE *f = fopen(path, "a");
if (f == NULL) {
return -1;
}
if (fprintf(f, "%s\n", text) < 0) {
fclose(f);
return -1;
}
return fclose(f) == 0 ? 0 : -1;
}One mark for mode "a" with the NULL check; one for checking fprintf and closing the file on that path, and for returning the result of fclose, since buffered data is only written (and a write error only discovered) when the file is flushed or closed.
Computer Programming Fundamentals · Toronto Metropolitan University (TMU, Ryerson) · also written CPS188
Make a free, printable CPS 188 practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 300 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the TMU library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CPS 188 practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.
questionbank.dev is independent and not affiliated with Toronto Metropolitan University. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.