Vous êtes sur la page 1sur 37

Imperative Programming

A 5 ECTS course for for first year students

http://www.control.aau.dk/~jdn/edu/courses/13-2/imperativ

http://sict.moodle.aau.dk

Based on C

Imperativ Programming/JDN - Lecture 1

Goal
Learn how to

program with an imperativ language. C

Do it in real life on your PC

Install of SW like compilers

Get things up and running

Use it in P1 project if possible


Get an programming skill platform for the next languages
to come to you (java, C++,C#,Python,Php,matlab,...)

Imperativ Programming/JDN - Lecture 1

Goal II

Imperativ Programming/JDN - Lecture 1

Rough layout of course

Basic C and
programming
Embedded
programming on
Arduino

just buy one


And maybe buy one
you self (200 DKK)
http://arduino.cc

Imperativ Programming/JDN - Lecture 1

Basic C part

GUI based:

Arduino environment

(codeblocks C for mac,windows,linux)

Net based http://idone.com

The war of operating systems

Mac, Windows XP/V/7, Linux, embedded

We try to cover it all

Please help your neighbour

Imperativ Programming/JDN - Lecture 1

Embedded part

Arduino based

AVR micro controller

1/2/4/8 kByte of RAM

Up to 128 kByte of flash for program code

IO

Up to 14 channels 10 bit analog input

Up to 7 or so analog output (by PWM)

Digital in and out a lot

Serial (rs232),i2c,..

Use nearly no current (use a battery)

Cheap

Imperativ Programming/JDN - Lecture 1

Arduino's

Imperativ Programming/JDN - Lecture 1

Imperativ Programming/JDN - Lecture 1

It is your choice

To be here

We are here to help (you)

Imperativ Programming/JDN - Lecture 1

TODAY

Arduino SW install party

Network based coding (http://idone.com or eq)

Imperativ Programming/JDN - Lecture 1

10

Den gang far var ung ...

Var der rigtige kompjutere til

Imperativ Programming/JDN - Lecture 1

11

Imperativ Programming/JDN - Lecture 1

12

Characteristics

Difficult to program

Difficult to run programs on

High salaries

Died after the PC arrived ?

64 kByte Ram

4.77 Mhz processor (today 1000 times more)

1 or 2 640 kByte floppy drive

Imperativ Programming/JDN - Lecture 1

13

The return of the main frame ?!

Imperativ Programming/JDN - Lecture 1

14

Very simple model

OS

GUI IO

NET

IO

HW
Imperativ Programming/JDN - Lecture 1

15

From code to executable

Editor write program in language (C,...)

Compile it to binary for a specific architecture (PC,ARM,...)

Link it with libraries for specific platform

windows,linux,arduino,...

Different platforms different facilities/possibilities

Save executable

In filesystem (on disk)

In flash memory

Loadable via net

...

Imperativ Programming/JDN - Lecture 1

16

Let's do it

/* A simple Program */
#include <stdio.h>
int main(void)
{
printf(Hello World\n);
}

Imperativ Programming/JDN - Lecture 1

17

Imperativ Programming/JDN - Lecture 1

18

Imperativ Programming/JDN - Lecture 1

19

SW Development method

Specify problem requirements

Analyze the problem

Design the algorithm to solve the problem

Implement the algorithm

Test and verify the completed program

Maintain ... and update the program

Very simplified ...

Imperativ Programming/JDN - Lecture 1

20

Imperativ Programming/JDN - Lecture 1

21

Fig 2.1 ... on the comming slides

C is en general a very small language


No fancy graphics, GUI, network,...
All extra is by purpose on a specific platform
And is supplied in libraries/part of Operating Sys

Imperativ Programming/JDN - Lecture 1

22

Only simple types:


Integers :
16/32/64 bit wide dep of architecture
Floating point:
IEEE given
character/byte
8 bit
void (nothing)
...
Imperativ Programming/JDN - Lecture 1

23

/* include statements for extending language*/


#include <stdio.h> /* standard input/output stuff */
/* C can only + - * /
/* the rest comes as library / extensions */
Sin, cos, atan, printf, file I/O, GUI (!),...

Imperativ Programming/JDN - Lecture 1

24

double float_1;
int int_1, int_2; /* upper case is not equal lower case */
int main(void)
{
int_1 = 3;
int_2 = int_1 *3 44;
float_1 = int_1;
}
/* p.. of cake */

Imperativ Programming/JDN - Lecture 1

25

Beware about conversions


/* declaration part */
int i_1, i_2, i_3;
double float_1;
i_1 = 3;
i_2 = 4;
i_3 = i_1 / i_2;
float_1 = i_1 / i_2; /* ??? */
Imperativ Programming/JDN - Lecture 1

26

int int1, int2;


...
int2 = 12785;
Int1 = int2 ; /* an assigment is a copy !!! */

Imperativ Programming/JDN - Lecture 1

27

stdio.h

Need something one the computer screen ...

Output like print hello ...

Input like

read_an_integer_from_keyboard

Imperativ Programming/JDN - Lecture 1

28

stdio.h - output

printf (hello);

printf (hello\n new line);

printf(hello I have %i legs , 2 );

printf( hello I have %&i legs , int_1) ;

%i : integer

%f : floating point

%c : character

+ variants : %3i Integer written out as xxx

Imperativ Programming/JDN - Lecture 1

29

printf ( %i %f %c, 3, 3.2 , 3 );

int i,j,k;
i = 3 ; j = 4; k = 4444;
printf(%i%i%i,

i,j,k);

printf(%i %i %i, i,j,k);

Imperativ Programming/JDN - Lecture 1

/* writes out 344444 */


/* writes out 3 4 4444 */

30

stdio.h - input

read from keyboard


No error handling today

I ask for an integer you type dumpkopf ...

Imperativ Programming/JDN - Lecture 1

31

stdio.h - scanf

int a_number;

scanf ( %i , & a_number);


/* read & as location or address */
/* i for int, f for float c for character as printf*/

Imperativ Programming/JDN - Lecture 1

32

int i1,i2,i3;
scanf(%i %i %i, & i1, &i2, &i3 ) ;

Imperativ Programming/JDN - Lecture 1

33

Operator Hierarchy

See inside first page in book and page 96..

Level x

+ -

Level x-1

* /

...

2 + 5 * 4 + 5 = ???

Imperativ Programming/JDN - Lecture 1

27 I guess

34

(())(())

Use ( ) to clarify:
((2*3) +5) / (3+7)
You can never use to many only to few ...

Imperativ Programming/JDN - Lecture 1

35

Imperativ Programming/JDN - Lecture 1

36

Imperativ Programming/JDN - Lecture 1

37

Vous aimerez peut-être aussi