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

Edit | Attach | Watch | Print version | History: r8 | r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r2 - 2017-01-15 - uli
 
  • Edit
  • Attach
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