# Difference: Exercise2Solutions (1 vs. 4)

#### Revision 42018-12-19 - UliRaich

Line: 1 to 1

 META TOPICPARENT name="Solutions"

# Solutions to the programming exercises

### Hello World

Line: 27 to 27

• We can figure out the file size using fseek and then dynamically (on the heap) allocate exactly the amount of memory needed to contain the full file. Again characters are printed.
• We can read the file and convert the numbers from ASCII to binary and store the numbers in a fixed size array. Then the numbers are printed
• We can read the file and test how many numbers can be converted such that the integer array containing the numbers can also be dynamically allocated.
Changed:
<
<
I do have an example for each of these solutions but before uploading them I would like to see your solution.
>
>
Here are possible solutions:

### Exercise 2.4: A Calculator

The calculations are extremely simple and they are implemented each in its own individual C file, Here is the example for addition:

Line: 57 to 59
However: For certain applications (e.g. real time clock) BCD (Binary Coded Decimal) is used. In this coding a hex digit only has values up to 9. The number 28 would then not be converted into 0x1C but into 0x28. We could in this case convert a BCD coded number into a binary coded number.
Changed:
<
<
I presume that the first version is wanted but for the moment I leave it up to you to provide the solution.
>
>
I presume that the first version is wanted for which this could be a solution:
--
Line: 75 to 79

>
>
 META FILEATTACHMENT attachment="fileRead.tar.gz" attr="" comment="" date="1545213239" name="fileRead.tar.gz" path="fileRead.tar.gz" size="46584" user="UliRaich" version="1" attachment="conv.tar.gz" attr="" comment="" date="1545213386" name="conv.tar.gz" path="conv.tar.gz" size="752" user="UliRaich" version="1"

#### Revision 32018-10-08 - UliRaich

Line: 1 to 1

 META TOPICPARENT name="Solutions"

# Solutions to the programming exercises

### Hello World

Line: 25 to 25

• We can try to read the file line by line with the library call getline (check it out with man!) using a fixed size line buffer. This reads a character string which we can print out with printf using the "%s" string format. This solution does not necessarily use pointers.
• We can read the file in a single go into a statically allocated (on the stack) file buffer, which must be big enough to hold the full file. The fread library call returns the number of characters (not the number of integers!) and we can print the characters one by one.
• We can figure out the file size using fseek and then dynamically (on the heap) allocate exactly the amount of memory needed to contain the full file. Again characters are printed.
Changed:
<
<
• We can read the file and convert the numbers from ASCII to binary and store the numbers in a fixed size array. The the numbers are printed
>
>
• We can read the file and convert the numbers from ASCII to binary and store the numbers in a fixed size array. Then the numbers are printed

• We can read the file and test how many numbers can be converted such that the integer array containing the numbers can also be dynamically allocated.
I do have an example for each of these solutions but before uploading them I would like to see your solution.

### Exercise 2.4: A Calculator

Line: 42 to 42 Changed:
<
<
First we make sure that exactly 3 parameters are given (argc == 4, because it counts the executable name as the first parameter). Then we convert the operands from string (argv and argv) to double. The operator is thee first character (character number 0) of the second argument: argv.
>
>
First we make sure that exactly 3 parameters are given (argc == 4, because it counts the executable name as the first parameter). Then we convert the operands from string (argv and argv) to double. The operator is the first character (character number 0) of the second argument: argv.
Now we can call the function implementing the operation and print the results. The full program is here:

#### Revision 22018-10-05 - UliRaich

Line: 1 to 1

 META TOPICPARENT name="Solutions"
Changed:
<
<

>
>

# Solutions to the programming exercises

### Hello World

Changed:
<
<
Article text.
>
>
When learning C almost everybody comes across a program called "Hello World". This is the most simple C program you can possibly write. It has the advantage though that once it works you know that your infrastructure: compiler, linker, C-library etc. are working properly.

Changed:
<
<
--
>
>
Here is the program: and this is what you see when you compile and run it ### Exercise 2.3 Pointers

Frankly speaking, going from "Hello World" to a file reading program with pointers is a huge jump. It would be better to continue with exercise 2.4 first before trying to attack this one. An additional difficulty is that a priori we do not know the length of the file.

Looking at the file exercise.txt with file and cat we see that it is an ASCII file with integer numbers (more precisely their ASCII representation) There are different ways to try and solve the problem:

• We can try to read the file line by line with the library call getline (check it out with man!) using a fixed size line buffer. This reads a character string which we can print out with printf using the "%s" string format. This solution does not necessarily use pointers.
• We can read the file in a single go into a statically allocated (on the stack) file buffer, which must be big enough to hold the full file. The fread library call returns the number of characters (not the number of integers!) and we can print the characters one by one.
• We can figure out the file size using fseek and then dynamically (on the heap) allocate exactly the amount of memory needed to contain the full file. Again characters are printed.
• We can read the file and convert the numbers from ASCII to binary and store the numbers in a fixed size array. The the numbers are printed
• We can read the file and test how many numbers can be converted such that the integer array containing the numbers can also be dynamically allocated.
I do have an example for each of these solutions but before uploading them I would like to see your solution.

### Exercise 2.4: A Calculator

The calculations are extremely simple and they are implemented each in its own individual C file, Here is the example for addition: and here is the corresponding include file: A little bit more complex is the reading of the operators and the operand. These are read as command line arguments and they are therefore strings and must be converted into binary representation before we can do any calculations with them: First we make sure that exactly 3 parameters are given (argc == 4, because it counts the executable name as the first parameter). Then we convert the operands from string (argv and argv) to double. The operator is thee first character (character number 0) of the second argument: argv.

Now we can call the function implementing the operation and print the results. The full program is here:

### Exercise 2.5 Conversion function

The conversion of the calculator to an octal or hex one is included in the solution for exercise 2.4

The conversion function is still a different story:

The calculator uses ASCII strings for its input and output. If the conversion function is to be in relation with the calculator then the conversion must be from a text string containing a decimal number to a text string containing a hex or binary number).

However: For certain applications (e.g. real time clock) BCD (Binary Coded Decimal) is used. In this coding a hex digit only has values up to 9. The number 28 would then not be converted into 0x1C but into 0x28. We could in this case convert a BCD coded number into a binary coded number.

I presume that the first version is wanted but for the moment I leave it up to you to provide the solution.

>
>
I was waiting for the solutions but did not see anything come up. So... I started to write at least partial solutions

`<--/commentPlugin-->`
\ No newline at end of file
>
>
 META FILEATTACHMENT attachment="helloResult.png" attr="" comment="" date="1538746387" name="helloResult.png" path="helloResult.png" size="9243" user="UliRaich" version="1" attachment="helloWorld.png" attr="" comment="" date="1538746387" name="helloWorld.png" path="helloWorld.png" size="16483" user="UliRaich" version="1" attachment="ex2numbers.png" attr="" comment="" date="1538747355" name="ex2numbers.png" path="ex2numbers.png" size="14888" user="UliRaich" version="1" attachment="exerciseFile.png" attr="" comment="" date="1538747724" name="exerciseFile.png" path="exerciseFile.png" size="32666" user="UliRaich" version="1" attachment="calculator.tar.gz" attr="" comment="" date="1538748727" name="calculator.tar.gz" path="calculator.tar.gz" size="1317" user="UliRaich" version="1" attachment="add.png" attr="" comment="" date="1538749108" name="add.png" path="add.png" size="8115" user="UliRaich" version="1" attachment="calc.png" attr="" comment="" date="1538749110" name="calc.png" path="calc.png" size="143024" user="UliRaich" version="1" attachment="addIncl.png" attr="" comment="" date="1538749357" name="addIncl.png" path="addIncl.png" size="5743" user="UliRaich" version="1"

Line: 1 to 1
>
>
 META TOPICPARENT name="Solutions"

# Title

Article text.

`<--/commentPlugin-->`