CH-230-A Assignment 6 – C Preprocessor, Bitwise Operators, Linked Lists solution

$29.99

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (4 votes)

Problem 6.1 Swapping two variables (1 point)
Presence assignment, due by 11:00 AM today Graded automatically with testcases only
Language: C
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: input
1
2
3.45
5.677
Testcase 6.1: output
After swapping:
2
1
5.677000
3.450000
Problem 6.2 Determine the least significant bit (1 point)
Due by Monday, October 18th, 23:00 Graded manually
Language: C
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: input
F
Testcase 6.2: output
The decimal representation is: 70
The least significant bit is: 0
Problem 6.3 Determine the mid-range of three values (1 point)
Due by Monday, October 18th, 23:00 Graded automatically with testcases only
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
mid range(a, b, c) = min(a, b, c) + max(a, b, c)
2
.
For example if 3, 10, 1 is the input, the mid-range of these values is
mid range(3, 10, 1) = min(3, 10, 1) + max(3, 10, 1)
2
=
1 + 10
2
=
11
2
= 5.5.
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: input
3
10
1
Testcase 6.3: output
The mid-range is: 5.500000
Problem 6.4 Conditional compilation for showing intermediate results (2 points)
Due by Monday, October 18th, 23:00 Graded manually
Language: C
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 = (x1, x2, . . . , xn)
and y = (y1, y2, . . . , yn) is calculated as
< x, y >=
Xn
i=1
xi
· yi
.
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: input
3
1
2
3
3
5
1
Testcase 6.4: output
The intermediate product values are:
3
10
3
The scalar product is: 16
Problem 6.5 Binary representation backwards (1 point)
Due by Monday, October 18th, 23:00 Graded automatically with testcases only
Language: C
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: input
2
Testcase 6.5: output
The decimal representation is: 50
The backwards binary representation is: 010011
Problem 6.6 Binary representation (1 point)
Due by Monday, October 18th, 23:00 Graded manually
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: input
2
Testcase 6.6: output
The decimal representation is: 50
The binary representation is: 00110010
Problem 6.7 set3bits() (1 point)
Due by Monday, October 18th, 23:00 Graded automatically with testcases only
Language: C
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: input
2
7
6
1
Testcase 6.7: output
The decimal representation is: 50
The binary representation is: 00110010
After setting the bits: 11110010
Problem 6.8 A linked list (1 point)
Due by Monday, October 18th, 23:00 Graded automatically with testcases only
Language: C
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: input
b
2
b
3
a
4
p
r
p
q
Testcase 6.8: output
3 2 4
2 4
Problem 6.9 An enhanced linked list (2 points)
Due by Monday, October 18th, 23:00 Graded manually
Language: C
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: input
b
2
b
3
a
4
p
r
p
i
1
5
p
i
4
11
R
p
q
Testcase 6.9: output
3 2 4
2 4
2 5 4
Invalid position!
4 5 2
How to submit your solutions
• Your source code should be properly indented and compile with gcc or g++ depending on the problem without any errors or warnings (You can use gcc -Wall -o program program.c or g++
-Wall -o program program.cpp). Insert suitable comments (not on every line . . . ) to explain
what your program does.
• Name the programs according to the suggested filenames (they should match the description of the
problem) in Grader.
Each program must include a comment on the top like the following:
/*
CH-230-A
a6 p1.[c or cpp or h]
Firstname Lastname
myemail@jacobs-university.de
*/
• You have to submit your solutions via Grader at
https://grader.eecs.jacobs-university.de.
If there are problems (but only then) you can submit the programs by sending mail to
k.lipskoch@jacobs-university.de with a subject line that begins with CH-230-A.
It is important that you do begin your subject with the coursenumber, otherwise I might have
problems to identify your submission.
• Note, that after the deadline it will not be possible to submit any solutions. It is useless to send late
solutions by mail, because they will not be accepted.
This assignment is due by Monday, October 18th, 23:00.