2023-05-31
Max. 100 points
Name:
Task | Max. | Achieved |
---|---|---|
1 | 25 | |
2 | 25 | |
3 | 25 | |
4 | 25 | |
Sum | 100 |
Statement | True | False |
---|---|---|
When recompiling a large project after a small change,
make can drastically reduce the compile time. | ||
make can use multiple processor cores
in parallel when compiling a project. | ||
make inspects a project's source code to automatically
generate each target's dependencies. | ||
cmake is a platform dependend version of
make that only works on Linux. | ||
Function pointers allow functions to be treated as first class citizen. | ||
Without function pointers, it is impossible to reserve heap memory. | ||
Function pointers are required for using callbacks in C. | ||
A function that is part of a C struct can automatically access all of the struct's data members. | ||
Command-line arguments are not used in modern programs. | ||
The C standard provides functions for parsing long command-line arguments and subcommands. |
int main(.)
(unless main is part of the snippet).
Assume that all neccessary standard library
#include
s are present.stdout
?
Write their exact output below each snippet.
char* names[] = {"Pat", "Chris", "Sue", "Steve"};
qsort(names);
char* names[] = {"Pat", "Chris", "Sue", "Steve"};
FILE* f = fopen("data.txt", "w");
for (size_t i = 0; i < sizeof(names) / sizeof(char*); ++i) {
fprintf(f, "%s\n", names[i]);
}
char name[] = "Sue";
char* n = name;
while (*n) {
printf("%c", *n);
n++;
}
char str[] = "students";
char* haystack = str;
char needle = 'x';
int occ = 0;
while (*haystack) {
if (*haystack == needle) { occ++; }
haystack++;
}
printf("%d\n", occ);
unsigned int radicand = 36;
for (unsigned int i = 0; i <= radicand; ++i) {
if (i * i == radicand) { printf("%u", i); }
}
struct
struct intarray {
size_t reserved; // available storage
size_t elements; // current number of elements
int* array;
};
Create a type IntArray
that can be used to create instances of
struct intarray
. Implement the function
void ia_erase(IntArray* ia, size_t index)
that removes the
element at index
. If index is beyond the end of the array,
nothing should be done.
index
typedef struct intarray IntArray;
void ia_erase(IntArray* ia, size_t index) {
if (index >= ia->elements) { return; }
ia->elements--;
for (size_t i = index; i < ia->elements; ++i) {
ia->array[i] = ia->array[i + 1];
}
}
int hamming(char* first, char* second)
that returns the hamming distance between the two given DNA strings.
If first
and second
do not have the same length,
return -1
.
Otherwise, return the number of differences in the two strings.
For example,
CCTAAGTAA CGTAAGTGA ^ ^has two differences and therefore your function should return 2 in this case. You may (but do not have to) use standard library functions.
int hamming(char *lhs, char *rhs) {
if (strlen(lhs) != strlen(rhs)) return -1;
int difference = 0;
for (size_t i = 0; i < strlen(lhs); ++i) {
if (lhs[i] != rhs[i]) difference += 1;
}
return difference;
}