2023-06-14
Max. 100 points
Name:
Task | Max. | Achieved |
---|---|---|
1 | 25 | |
2 | 25 | |
3 | 25 | |
4 | 25 | |
Sum | 100 |
Statement | True | False |
---|---|---|
cmake can generate
Makefiles . | ||
One can only use cmake if the chosen
IDE supports it. | ||
cmake is platform independent. | ||
With dynamic memory it is possible to grow and shrink dynamic data structures. | ||
The size of the stack can be determined with
ulimit -s . | ||
Using memory from the heap is faster than from the stack. |
Statement | True | False |
---|---|---|
Text files have the advantage of being human readable. | ||
There are multiple standards for storing data in text files. | ||
Binary files can easily be read with any text editor. | ||
Storing data in binary files generally requires more memory than structured text files. |
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.
typedef struct { double x; double y; } Point;
Point test = {.25, .75};
printf("[%.2lf %.2lf]\n", test.x, test.y);
int a = 10;
int* p = &a;
int** pp = &p;
**pp /= **pp;
printf("%d\n", a);
char s[] = "Hello";
char* p = s;
while (*p) {
*p = toupper(*p);
++p;
}
puts(s);
unsigned int radicand = 36;
for (unsigned int i = 0; i <= radicand; --i) {
if (i * i == radicand) { printf("%u", i); }
}
size_t size = 8192 * 1024; // 8 MB
int* array = (int*) malloc(size);
for (size_t i = 0; i < size / sizeof(int); ++i) {
array[i] = i * i; // initialize the array ...
}
printf("Survived?\n");
free(array); // avoid memory leak
const int CAN_NOT_ATTACK = 0;
const int CAN_ATTACK = 1;
const int INVALID_POSITION = 2;
typedef struct {
uint8_t row;
uint8_t column;
} Position;
Given the position of two queens on a chess board (8 times 8 squares) as
0 indexed row and column of type Position
, determine whether
the two queens have invalid positions, can attack each other or cannot
attach each other. To do so, implement a function
int can_attack(Position queen_1, Position queen_2)
that
returns the appropriate constant as defined above.CAN_ATTACK
in case rows or columns
are shared and the setting is validCAN_ATTACK
in case the diagonal is
shared and the setting is validCAN_NOT_ATTACK
in case
the setting is valid but an attack is impossiblebool is_valid(Position pos) {
if (pos.row >= 8 || pos.column >= 8) return false;
return true;
}
int can_attack(Position queen_1, Position queen_2) {
if (!(is_valid(queen_1) && is_valid(queen_2))) return INVALID_POSITION;
if (queen_1.row == queen_2.row && queen_1.column == queen_2.column)
return INVALID_POSITION;
if (queen_1.row == queen_2.row || queen_1.column == queen_2.column)
return CAN_ATTACK;
int8_t d_row = queen_1.row - queen_2.row;
int8_t d_column = queen_1.column - queen_2.column;
if (abs(d_row) == abs(d_column)) return CAN_ATTACK;
return CAN_NOT_ATTACK;
}
int binary_str_to_int(const char* str)
that takes a C string representing a binary number (eg. "100101")
and returns the base 10 numeric representation of that string.
In case of "100101", the result would be
int binary_str_to_int(const char* str) {
int digits = 0;
int result = 0;
while (str[digits]) { digits++; }
for (int i = digits - 1; i >= 0; --i) {
if (str[digits - i - 1] == '1') {
result += 1 << i;
}
}
return result;
}