Starting from:

$25

CH-230-A-Assignment 6 Solved

Programming in C and C++
Course: CH-230-A

Assignment 6 - C Preprocessor, Bitwise Operators, Linked Lists

•    The problems of this assignment must be solved in C or C++ (instruction in each problem).

•    Your programs should have the input and output formatting according to the testcaseslisted after the problems.

Problem 6.1 Swapping two variables                                                                     
 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                                                     
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                                          

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: input

3
Testcase 6.3: output

The 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 = (x1,x2,...,xn) and y = (y1,y2,...,yn) 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: input

3

1

2

3

3
Testcase 6.4: output

The intermediate product values are:

3

10

3

The 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: input

2
Testcase 6.5: output

The decimal representation is: 50

The 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: input

2
Testcase 6.6: output

The decimal representation is: 50

The binary representation is: 00110010
Problem 6.7 set3bits()
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

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

Testcase 6.9: input

b 2 b 3 a
Testcase 6.9: output

3 2 4

2 4

2 5 4

Invalid position!

4 5 2
 4 p r p i 1 5 p i 4

11 R p q


More products