Difference: ProgrammingExercise (1 vs. 8)

Revision 82017-09-18 - uli

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

Exercises 2: C Programming

Goal:

Line: 31 to 31
  Have a look at these examples, compile them and test them. Make sure you understand every little detail. If you don't: ask!
Added:
>
>
Type at least a few of them yourself using the text editor.
 The compile command is

gcc -o hello hello.c

Line: 38 to 40
 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!

Changed:
<
<
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.
>
>
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!”. Create a directory hello in ~/exercises/solutions/exercise_2 and create the source file hello.c in there.
Compile it with the gcc command and run the program to test it (./hello). What does the dot do?
 

Exercise 3: A calculator

We want to produce a calculator program capable of calculating the 4 basic calculations:

Line: 88 to 90
 
<--/commentPlugin-->
Changed:
<
<
META FILEATTACHMENT attachment="codeExamples.tar.gz" attr="" comment="" date="1504866315" name="codeExamples.tar.gz" path="codeExamples.tar.gz" size="3240" user="uli" version="1"
META FILEATTACHMENT attachment="exercises_2.odt" attr="" comment="" date="1505471107" name="exercises_2.odt" path="exercises_2.odt" size="21017" user="uli" version="1"
>
>
META FILEATTACHMENT attachment="exercises_2.odt" attr="" comment="" date="1505762906" name="exercises_2.odt" path="exercises_2.odt" size="21198" user="uli" version="1"
META FILEATTACHMENT attachment="newCodeExamples.tar.gz" attr="" comment="" date="1505765608" name="newCodeExamples.tar.gz" path="newCodeExamples.tar.gz" size="13591" user="uli" version="1"

Revision 72017-09-15 - uli

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

Exercises 2: C Programming

Goal:

Line: 27 to 27
  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!
Changed:
<
<
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.
>
>
Download the code examples from the lectures and put the file into ~/exercises/solutions/exercise_2 (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/exercise_2 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!
Line: 89 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"
Changed:
<
<
META FILEATTACHMENT attachment="exercises_2.odt" attr="" comment="" date="1504897614" name="exercises_2.odt" path="exercises_2.odt" size="21224" user="uli" version="1"
>
>
META FILEATTACHMENT attachment="exercises_2.odt" attr="" comment="" date="1505471107" name="exercises_2.odt" path="exercises_2.odt" size="21017" user="uli" version="1"

Revision 62017-09-08 - uli

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

Exercises 2: C Programming

Goal:

Line: 89 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"
Changed:
<
<
META FILEATTACHMENT attachment="lecture_3.odp" attr="" comment="" date="1504888100" name="lecture_3.odp" path="lecture_3.odp" size="444024" user="uli" version="1"
>
>
META FILEATTACHMENT attachment="exercises_2.odt" attr="" comment="" date="1504897614" name="exercises_2.odt" path="exercises_2.odt" size="21224" user="uli" version="1"

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"

Revision 42017-09-08 - uli

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

Exercises 2: C Programming

Goal:

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.

Changed:
<
<

Exercise 1: Hello World!

>
>

Exercise -1:

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

What is the octal value of 101 001 110 ?

How much is 7+5 when calculated in octal?

Convert the octal value of 345 to decimal

Write down the binary representation of the hex value 0x83ab

How much is 7+5 in hexadecimal?

What is the decimal value of 0xa3?4

Do these calculations and note down the results. I will pass by and check!

Exercise 1: The code examples from the lectures

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!

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!

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.
Changed:
<
<

Exercise 2: A calculator

>
>

Exercise 3: A calculator

  We want to produce a calculator program capable of calculating the 4 basic calculations:
  • addition

Line: 26 to 52
 
  • subtr.c

  • mult.c

  • divide.c

Changed:
<
<
and the Makefile to build the complete program.

Exercise 3: Calculate a sine function

>
>
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.

Exercise 3b: Octal and Hex Calculations

 
Changed:
<
<
One of the devices we will use during the sensor exercises is a digital to analogue converter (DAC), allowing us to generate an arbitrary wave form. This is a 12 bit device and we want to generate a sine wave for it. In order to do this, calculate the values to be sent to the DAC in such a way, that the min/max values of the sine wave are adapted to the 12 bits of the DAC.
>
>
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.
 
Changed:
<
<
Generate 100 values for the sin(x) argument x running from 0 to 2π and write them to a file called sine.dat.
>
>
Can this also work on negative numbers?
 
Changed:
<
<
Read sine.dat with LibreOffice and generate a plot.
>
>
Now you can check if your hand calculations were correct!

Exercise 4: Calculate a sine function

 
Changed:
<
<
Generate a plot with gnuplot.
>
>
You will probably not make it to this exercise in a single session. We can therefore postpone this one for the next lab session.

One of the devices we will use during the sensor exercises is a digital to analogue converter (DAC), allowing us to generate an arbitrary wave form. This is a 12 bit device and we want to generate a sine wave for it. In order to do this, calculate the values to be sent to the DAC in such a way, that the min/max values of the sine wave are adapted to the 12 bits of the DAC.

Generate 100 values for the sin(x) argument x running from 0 to 2π and write them to a file called sine.dat. You will have to add the math library when linking (add -lm argument when calling gcc).

Read sine.dat with libreoffice and generate a plot. Generate a plot with gnuplot.

  -- Uli Raich - 2017-01-03

Comments

<--/commentPlugin-->
Added:
>
>
META FILEATTACHMENT attachment="codeExamples.tar.gz" attr="" comment="" date="1504866315" name="codeExamples.tar.gz" path="codeExamples.tar.gz" size="3240" user="uli" version="1"

Revision 32017-08-12 - uli

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

Exercises 2: C programming

>
>

Exercises 2: C Programming

 

Goal:

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.

Revision 22017-01-15 - uli

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

Exercises 2: C programming

Line: 34 to 34
  Generate 100 values for the sin(x) argument x running from 0 to 2π and write them to a file called sine.dat.
Changed:
<
<
Read sine.dat with LibreOffice and generate a plot.
>
>
Read sine.dat with LibreOffice and generate a plot.
  Generate a plot with gnuplot.

Revision 12017-01-03 - uli

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

Exercises 2: C programming

Goal:

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

Exercise 2: A calculator

We want to produce a calculator program capable of calculating the 4 basic calculations:

  • addition

  • subtraction

  • multiplication

  • division

The program takes 3 arguments: the first operand followed by the operator followed by the second operand. The user should type calculate 3.4 + 5.8 and the program should print:

3.4 + 5.8 = 9.2

Addition, subtraction, multiplication and division should be performed by functions implemented in separate files, compiled separately and linked to the main calculator program.

Write the 5 C files:

  • calculator.c

  • add.c

  • subtr.c

  • mult.c

  • divide.c

and the Makefile to build the complete program.

Exercise 3: Calculate a sine function

One of the devices we will use during the sensor exercises is a digital to analogue converter (DAC), allowing us to generate an arbitrary wave form. This is a 12 bit device and we want to generate a sine wave for it. In order to do this, calculate the values to be sent to the DAC in such a way, that the min/max values of the sine wave are adapted to the 12 bits of the DAC.

Generate 100 values for the sin(x) argument x running from 0 to 2π and write them to a file called sine.dat.

Read sine.dat with LibreOffice and generate a plot.

Generate a plot with gnuplot.

-- Uli Raich - 2017-01-03

Comments

<--/commentPlugin-->
 
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