Difference: ProgrammingExercise (4 vs. 5)

Revision 52017-09-08 - uli

Line: 1 to 1
 
META TOPICPARENT name="CourseExercises"

Exercises 2: C Programming

Goal:

Line: 6 to 6
 We will program most of our sensor readout programs in the C programming language and programming a few simple C programs will help us with future exercises. As soon as programs become a bit more complex it becomes important to describe how the program must be built. This description is usually done in Makefiles in such a way that the simple command make will reconstruct the entire program compiling just the code that has been modified and linking the right files and libraries needed be the program.

Exercise -1:

Changed:
<
<
I saw that during the last exercises session you had been cheating on me doing only those exercises for which you could find a cookbook recipe but the one where you had to think yourself you skipped!
>
>
I saw that during the last exercise session you had been cheating on me doing only those exercises for which you could find a cookbook recipe but the one where you had to think yourself you skipped!
  I am talking about the calculations with octal and hex number systems. Since you will need this knowledge when we will work with chip registers, I come back to this exercise:
Line: 20 to 20
  How much is 7+5 in hexadecimal?
Changed:
<
<
What is the decimal value of 0xa3?4
>
>
What is the decimal value of 0xa3?
  Do these calculations and note down the results. I will pass by and check!

Exercise 1: The code examples from the lectures

Changed:
<
<
Create subdirectories exercises_2 in ~/exercises/problems, ~/exercises/solutions, ~/exercises/doc.
You will do this for each new exercises session, so I will not tell you any more!
>
>
Create subdirectories exercises_2 in ~/exercises/problems, ~/exercises/solutions, ~/exercises/doc.
You will do this for each new exercise session, so I will not tell you any more!
  Download the code examples from the lectures and put the file into ~/exercises/solutions (create this directory!). They are attached as an archive file (file extension: .tar.gz) to this page (see on the bottom of the page). cd to ~/exercises/solutions/codeExamples and extract the individual files with tar zxvf codeExamples.tar.gz. Check in the man page for tar what this does.

Have a look at these examples, compile them and test them. Make sure you understand every little detail. If you don't: ask!

Added:
>
>
The compile command is

gcc -o hello hello.c

if the source code is in a file called hello.c The binary output file will be called hello and you can execute it with ./hello.

 

Exercise 2: Hello World!

The first program to be written in C is traditionally the “Hello World!” program. We don’t want to change this tradition and you are therefore asked to write the program that prints “Hello World!”.
First compile it with the gcc command, then write a Makefile to build the program using the make command.

Line: 53 to 59
 
  • mult.c

  • divide.c

When trying the program you will experience a problem with multiplication. Can you figure out why? Instead of using “*” you must use “\*” and it will work.
Added:
>
>
Here we will compile the programs separately:

gcc -c calculator.c which will produce an object file calculator.o. Do the same thing with the other source files. Finally link everything to produce the executable:

gcc -o calculator calculator.o add.o subtr.o mult.o divide.o

 

Exercise 3b: Octal and Hex Calculations

Once your calculator works: Can you convert it to a calculator that calculates in decimal, octal and hex?
Have a look into the man page of sscanf and printf and check for input and output conversions.

Line: 77 to 89
 
<--/commentPlugin-->

META FILEATTACHMENT attachment="codeExamples.tar.gz" attr="" comment="" date="1504866315" name="codeExamples.tar.gz" path="codeExamples.tar.gz" size="3240" user="uli" version="1"
Added:
>
>
META FILEATTACHMENT attachment="lecture_3.odp" attr="" comment="" date="1504888100" name="lecture_3.odp" path="lecture_3.odp" size="444024" user="uli" version="1"
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback