Problem 6.1 Swapping two variables (
Write a macro and a program for swapping the contents of two variables. The macro should have three parameters: the two variables and the corresponding data type.
Your program should read two integers and two doubles from the standard input. Then you should print on the standard output the contents of the four variables after swapping (doubles with a floating point precision of 6).
You can assume that the input will be valid. Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.
Testcase 6.1: input123.455.677 | Testcase 6.1: outputAfter swapping:215.677000 |
3.450000
Problem 6.2 Determine the least significant bit
Write a macro and a program for determining the least significant bit (the first bit from the right in the binary representation) of an unsigned char read from the standard input.
Your program should read an unsigned char from the standard input and print the decimal representation of the unsigned char as well as its least significant bit (which is either 1 or 0) on the standard output using only bitwise operators and without explicitly converting to binary representation.
You can assume that the input will be valid. To pass the testcases your output has to be identical with the provided ones.
Testcase 6.2: inputF | Testcase 6.2: outputThe decimal representation is: 70 |
The least significant bit is: 0
Problem 6.3 Determine the mid-range of three values
Write multiple macros and a program for determining the mid-range of three values. The midrange of three variables a, b, and c is calculated as
.
For example if 3, 10, 1 is the input, the mid-range of these values is
.
Your program should read three integers from the standard input. For calculating the mid-range of these values only macros should be used. The mid-range should be printed on the standard output with a floating point precision of 6.
You can assume that the input will be valid. Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.
Testcase 6.3: input3 | Testcase 6.3: outputThe mid-range is: 5.500000 |
10
1
Problem 6.4 Conditional compilation for showing intermediate results
Write a program which computes the scalar product of two n-dimensional integer vectors and uses conditional compilation for showing/not showing intermediate results (products of the corresponding components). The scalar product of two n-dimensional vectors x = (x_{1},x_{2},…,x_{n}) and y = (y_{1},y_{2},…,y_{n}) is calculated as
.
For example the scalar product of the vector x = (1,2,3) with the vector y = (3,5,1) is
< x,y >= 1 · 3 + 2 · 5 + 3 · 1 = 3 + 10 + 3 = 16.
The intermediate results which are to be shown or not are 3, 10 and 3.
Your program should read from the standard input the dimension of the vector (in the previous example 3) along with the components of two integer vectors. The output consists of the intermediate results and the value of the scalar product of the two vector if the directive INTERMEDIATE is defined. If INTERMEDIATE is not defined then only the scalar product of the two vectors should be printed on the standard output.
You can assume that the input will be valid. To pass the testcases your output has to be identical with the provided ones.
Testcase 6.4: input31233 | Testcase 6.4: outputThe intermediate product values are:3103The scalar product is: 16 |
5
1
Problem 6.5 Binary representation backwards
Write a program using bit masks and bitwise operators for printing the binary representation of an unsigned char backwards. For example the character ’2’ is encoded as 50 in decimal representation which is in binary representation 110010. Therefore, the backwards binary representation is 010011.
Your program should read an unsigned char from the standard input and print on the standard output the backwards binary representation of the read character without explicitly converting the decimal value to binary or using an array to store the bits.
You can assume that the input will be valid. Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.
Testcase 6.5: input2 | Testcase 6.5: outputThe decimal representation is: 50The backwards binary representation is: 010011 |
Problem 6.6 Binary representation | |
Language: C
Write a program using bit masks and bitwise operators for printing the binary representation of an unsigned char without storing the bits in an array or explicitly converting to binary. For example the character ’2’ is encoded as 50 in decimal representation which is in binary representation on 8 bits 00110010.
Your program should read an unsigned char from the standard input and print on the standard output the binary representation of the read character.
You can assume that the input will be valid. To pass the testcases your output has to be identical with the provided ones.
Testcase 6.6: input2 | Testcase 6.6: outputThe decimal representation is: 50The binary representation is: 00110010 |
Problem 6.7 set3bits() |
Write a program for setting three bits of an unsigned char to 1. The function set3bits should have four parameters: the unsigned char to be changed and the three bits which are to be set to 1. For example the character ’2’ is encoded as 50 in decimal representation which is in binary representation on 8 bits 00110010. If set3bits() with bits 7, 6 and 1 to be set to 1 is called then the output on the standard output should be 11110010. Print the result on the standard output from the main() function.
You can assume that the input will be valid. Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.
Testcase 6.7: input2761 | Testcase 6.7: outputThe decimal representation is: 50The binary representation is: 00110010After setting the bits: 11110010 |
Problem 6.8 A linked list |
Using the example from the slides (Tutorial 6, pages 28 − 35), write a program that uses a linked list. Your program should wait for input from the keyboard. Entering from the keyboard an ’a’ will just add the following number (read as next from the keyboard) to the end of the list, while a ’b’ inserts at the beginning of the list. The character ’r’ will remove the first element from the list, a ’p’ will print the list while a ’q’ will free the memory used by the list and quit the execution of the program.
Use a switch-case statement to decide which action to take.
You can assume that the input will be valid regarding the structure. To pass the testcases your output has to be identical with the provided ones.
Testcase 6.8: inputTestcase 6.8: output
b3 2 4
22 4 b 3 a 4 p r p q
Problem 6.9 An enhanced linked list (
Extend your program for Problem 6.8 by writing a function for inserting a new element into the list at a given position and a function for reversing the order of the elements in the list. Your program should wait for input from the keyboard. An ’i’ followed by two numbers (the position and the number to be inserted) should insert the second the number at position of the first number (the first element in the list has position 0). You can assume that the input does not contain any logical errors (e.g., ’i’ is always followed by two numbers, and ’b’ and ’a’ are followed by one number). However, if the position for inserting is negative or is greater than the number of elements in the list then print on the standard output “Invalid position!”. An ’R’ should reverse the order of the elements in the list without allocating new nodes or using a doubly linked list (i.e., only with the use of pointers). Use a switch-case statement to decide which action to take.
You can assume that the input will be valid regarding the structure. To pass the testcases your output has to be identical with the provided ones.
Testcase 6.9: inputb 2 b 3 a | Testcase 6.9: output3 2 42 42 5 4Invalid position!4 5 2 |
4 p r p i 1 5 p i 4
11 R p q
Reviews
There are no reviews yet.