procedural programming & fundamentals of programming · lab exercises • distributed at the...

24
25.04.2018 1 Procedural Programming & Fundamentals of Programming Exercise 1 (SS 2018) 24.04.2018 Procedural Programming & Fundamentals of Programming Principles of the exercises ti.unidue.de Joachim Zumbrägel We have 4 exercise dates We discuss topics of the lecture We discuss exercises and their solutions

Upload: others

Post on 18-Jan-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

1

Procedural Programming &Fundamentals of ProgrammingExercise 1 (SS 2018) ■ 24.04.2018

Procedural Programming & Fundamentals of Programming

Principles of the exercises

ti.uni‐due.de Joachim Zumbrägel

• We have 4 exercise dates

• We discuss topics of the lecture

• We discuss exercises and their solutions

Page 2: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

2

Procedural Programming & Fundamentals of Programming

Principles of the practical trainings

ti.uni‐due.de Joachim Zumbrägel

• A lab takes 90 minutes

• Each of the eight lab consists of

– homework tasks in the handout, checked during the lab

– test before the lab (PP only)

– the practical training itself

Procedural Programming & Fundamentals of Programming

Schedule

ti.uni‐due.de Joachim Zumbrägel

Date Lecture(3h)

Exercise(3h)

Practical Training for PP:  Tu., We. (2h)Practical Training for FOP: Mo., Tu., We. (2h)

10.04.18L1

17.04.18L2

24.04.18 E1

08.05.18L3 P1 Variables, Data Types, Statements, Hello World

15.05.18 E2 P2  Boolean expressions, Conditional Executions, 

29.05.18 E3 P3 Finite and Infinite Loops, Break and Continue

05.06.18L4 P4 Functions: Parameters and Arguments, Void 

12.06.18 E4 P5 Arrays

19.06.18 P6 Addressing: Direct and Indirect, Pointers 

26.06.18 P7 Strings and Files

03.07.18 P8 Math Functions, Linked Lists, and Binary Trees

10.07.18 NT Repetition Date for P1‐P8, used for missed labs

17.07.18

Page 3: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

3

Procedural Programming & Fundamentals of Programming

Practical training time and location

ti.uni‐due.de Joachim Zumbrägel

Procedural Programming• Tuesday 12:00-13:30, 14:00-15:30

• Wednesday 15:45-17:15

Fundamentals of Programming • Monday 08:15-09:45, 10:00-11:30, 17:30-19:00

• Tuesday 08:00-12:00

• Wednesday 17:30-19:00

Rooms: BA 028, BB 219

Procedural Programming & Fundamentals of Programming

Lab programming environment

Restrictions:• Simple as possible (syntax high lighting)• Available under Linux and Microsoft

Result:• Compiler

gcc

• Editor Jedit

ti.uni‐due.de Joachim Zumbrägel

Page 4: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

4

Procedural Programming & Fundamentals of Programming

Programming environment for you

Under linuxGcc: sudo apt-get install gccJedit: sudo apt-get install jedit

Under WindowsGcc: Download and install mingw (http://mingw.org)

Extend the Environment variable „PATH“ with the path where gcc is installed

Jedit: Download and install jedit (http://jedit.org)

That‘s all

ti.uni‐due.de Joachim Zumbrägel

Procedural Programming & Fundamentals of Programming

Lab tests

• Duration 5 minutes• Multiple Choice• 5 Questions (about the current lab respectively

about the corresponding handout)• Passed with 2 right answers• Example: What are the right symbols to

enclose a block in C? a) (); b) [ ]; c) {}; d) “ “;

ti.uni‐due.de Joachim Zumbrägel

Page 5: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

5

Procedural Programming & Fundamentals of Programming

Lab exercises

• Distributed at the beginning of the practical part of the lab

• Some simple tasks• Example (first lab):

Write a program asking the user to input two integer numbers and then calculates the sum of those two numbers.

ti.uni‐due.de Joachim Zumbrägel

Procedural Programming & Fundamentals of Programming

What will I learn in the 1. Seminar/Exercise

ti.uni‐due.de Joachim Zumbrägel

• Exercises– Flowchart, – Pseudocode– C-Code

• C-Programming– Introduction from source to executable file– C-Basics (main und return)– Operators– Basic data types– Inputs/Outputs– If and switch

Page 6: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

6

Procedural Programming & Fundamentals of Programming

Flow chart symbols

ti.uni‐due.de Joachim Zumbrägel

Procedural Programming & Fundamentals of Programming

Welcome

ti.uni‐due.de Joachim Zumbrägel

Start Excercise

Say „hello and quiet please“

If to loud

Continue with excercise ….

Show KISS

Hear Response

Stop Excercise

Check loudness

no

yes(unfortunately)

Page 7: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

7

Procedural Programming & Fundamentals of Programming

Kiss principle

ti.uni‐due.de Joachim Zumbrägel

• Keep it simple [and] stupid• Keep it simple and short• Keep it simple and smart• Keep it simple and straightforward• Keep it simple and small• Keep it simple and safe

Procedural Programming & Fundamentals of Programming

Solving Strategies

• Restate the problem• Divide the problem• Start with what you know• Reduce the problem• Look for analogies• Experiment• Don’t get frustrated

ti.uni‐due.de Joachim Zumbrägel

Page 8: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

8

Procedural Programming & Fundamentals of Programming

Excercise 1 (Task)

The following Ripple-Carry-Adder is constructed using a cascade of multiple full adders (FA). The Ripple-Carry-Adder should be operated on 20 MHZinput frequency, in which all input signals are changing at the same time. Each full adder has a propagation delay of 2ns.

Considering the limitation of input frequency and propagation delay, how many full adders in the cascade can produce the correct outputs? (Hint: Correct output means that the sum of the input vectors (a,b) should at least be delivered correctly at the output before the input changes next time).

ti.uni‐due.de Joachim Zumbrägel

Procedural Programming & Fundamentals of Programming

Exercise 1 (Questions/Task)

1. What do I know and what do I have to know to solve the problem?

2. Calculate the max. number of full adders (n) for the given circuit

3. Create now a program which generalizes the approach so that we can calculate n for a given input frequency and a given propagation delay– Build up the formula to solve this problem – Think about INPUT and OUTPUT

(measurement unit, dimension) – Draw a flow chart– Write the program

ti.uni‐due.de Joachim Zumbrägel

Page 9: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

9

Procedural Programming & Fundamentals of Programming

Program for exercise 1

/* PROG: NrOfAdders

CREATED:24.04.2018 by JOACHIM ZUMBRÄGEL */

#include <stdio.h>

int main()

{

float frequency, propagationDelay, nrOfSubCircuits;

printf("Frequency(in MHZ):\n");

scanf("%f", &frequency);

printf("Propagation delay (in ns):\n");

scanf("%f", &propagationDelay);

nrOfSubCircuits = 1000/frequency/propagationDelay;

printf("\nNr of Sub-Circuits <= %.0f",nrOfSubCircuits);

return 0;

}

ti.uni‐due.de Joachim Zumbrägel

Procedural Programming & Fundamentals of Programming

From program to executable

ti.uni‐due.de Joachim Zumbrägel

source file<filename>.c

Other object file(libraries)

object file(machine code)

Header files<headerfile>.h

executable file<filename>.exe

editor

compiler

linker

preprocessed file(text replacement)

preprocessor

Page 10: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

10

Procedural Programming & Fundamentals of Programming

Preprocessor

# indicates that the so called preprocessor comes into a play.

#include<stdlib.h> //includes the stdlib.h

#define MAX_ADDERS 10 //Definition of

after preprocessing ⇒the term „#include<stdlib.h>“ is replaced with

the contents of the file <stdlib.h> and ⇒the term MAX_ADDERS is replaced with 10.

ti.uni‐due.de Joachim Zumbrägel

Procedural Programming & Fundamentals of Programming

Main function and its return value

Every C-Programm has its main function:int main(){::return <integervalue>;

}

The returned integer value is catched by the operating system and can be used for further processing. 0 stands for success.

ti.uni‐due.de Joachim Zumbrägel

Page 11: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

11

Procedural Programming & Fundamentals of Programming

Comments

/* */ to enclose more than one line/* AUTHOR: J. Zumbrägel

CREATED: 01.05.2017

LAST UPDATED: 02.05.2017*/

// if you want to comment one line// Use comments to explain

ti.uni‐due.de Joachim Zumbrägel

Procedural Programming & Fundamentals of Programming

Simple data types

• Four basic arithmetic type identifiers in C: char, int (Integer types)float, double (floating point types)

• There are optional specifiers:signed, unsigned, short, long

• The actual size of integer types varies by implementation, but rules are existing like:char<=short<=int<=long<=long long

• Size can be evaluated by sizeof(<data type>)

ti.uni‐due.de Joachim Zumbrägel

Page 12: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

12

Procedural Programming & Fundamentals of Programming

Variables

• Before variables can be used in a program, all variables must be defined:- int x;- unsigned short int // or unsigned short- float y;

• Variables can and should be initialized:- unsigned long int ulCounter=0;

• You can define “constant” variables which will be remain unchanged after initialization.- const int maxNrOfItems = 10;

ti.uni‐due.de Joachim Zumbrägel

Procedural Programming & Fundamentals of Programming

Input and Output

• Inputscanf(format string, address of variable(s))int age;scanf(„%d“,&age) //reads in age„%d“ is the format string&age is the address of the variable age

• Outputprintf (format string,variable(s))int age;printf(„You are %d“,age) //prints age=> You are 20

ti.uni‐due.de Joachim Zumbrägel

Page 13: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

13

Procedural Programming & Fundamentals of Programming

Format string

Format strings allow the usage of data type placeholders in strings. The placeholder will be later on replaced with values of variables:

- “%d” // Value of type integer- “%f” // Value of type float- “%c” // Value of type Character

Hint: A % in a string always points to the start of a format specifier. If you want to output the sign % you have to write %% in the format string.

ti.uni‐due.de Joachim Zumbrägel

Procedural Programming & Fundamentals of Programming

Escape sequences

Escape sequences, otherwise known as escape codes, are special type of characters that are used inside the string literals. Examples:\n => newline\t => tab\” = double quote (“)• printf(“Hello, \“interested students\“!“);=> Hello, “interested students“!

• printf(“Hello,\nare you fine?“)=> Hello,

are you fine?

ti.uni‐due.de Joachim Zumbrägel

Page 14: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

14

Procedural Programming & Fundamentals of Programming

Operators (not complete)

C supports a lot of operators. They are symbols used within an expression to specify the manipulations to be performed while evaluating that expression. C operators are:• arithmetic: +, -, *, /, %• assignment: =• augmented assignment: +=, -=, *=, /=,%=

a+=value; //equivalent to a=a+value;

To be continued:

ti.uni‐due.de Joachim Zumbrägel

Procedural Programming & Fundamentals of Programming

Programming style

• Use spaces in source code (easier to read)• Use BlockCodes• Descriptive names for variables and use

CamelCase. Example: (int nrOfAdders)• Initialize your variables• Use constant or defines for fixed values• Use comments • Avoid long lines and complex statements

ti.uni‐due.de Joachim Zumbrägel

Page 15: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

15

Procedural Programming & Fundamentals of Programming

Exercise 2

• Write a program which converts a decimal number (0-9) into a nibble (half byte) .The output should be printed in the form:nibble = 0101Don‘t use if statements for the solution.Hint: make use of the operators “/” and “%”

Example : int n; n = 9;n/2 => 4n%2 => 1

ti.uni‐due.de Joachim Zumbrägel

Procedural Programming & Fundamentals of Programming

Relational Operators

Operator Meaning

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

== Equal to

!= Not equal to

ti.uni‐due.de Joachim Zumbrägel

Page 16: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

16

Procedural Programming & Fundamentals of Programming

Relational Expression

• Consists of operands and a relational operator between them.

• The result is a boolean value, represented as either true (1) or false (0).

• The result of a relational expression can beassigned to a variable.

Example: logResult=(x < y)

//logResult is set to 1(true) if x smaller y

ti.uni‐due.de Joachim Zumbrägel

Procedural Programming & Fundamentals of Programming

Logical Operators

• The logical operators allow logical combination of relational expressions

• The && Operator realizes Logical AND• The || Operator realizes Logical OR• The ! Operator realizes Logical NOTExample: logResult=((x && y) || (!z))

// logResult is set to 1 (true) // if x AND y are non-zero OR if z is equal 0

ti.uni‐due.de Joachim Zumbrägel

Page 17: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

17

Procedural Programming & Fundamentals of Programming

if - else statement

The if statement executes a code only when the condition is true. The else part is optional. If-else can be nested!if (condition){

statement(s)_1;//executed when condition true (1)

}

else

{statement(s)_2;//executed when condition false (0)

}

ti.uni‐due.de Joachim Zumbrägel

Procedural Programming & Fundamentals of Programming

if vs. if-else

ti.uni‐due.de Joachim Zumbrägel

statement(s)

condition?

true

false

if

statement(s)_1

condition?

true

false

statement(s)_2

if-else

Page 18: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

18

Procedural Programming & Fundamentals of Programming

The conditional expression

A conditional expression is a „short“ if else!(expression)?(execute if true):(execute if false)

Example:conResult=(x>y)? x : y;

//is equal to

if (x>y)conResult=x;

elseconResult=y;

ti.uni‐due.de Joachim Zumbrägel

Procedural Programming & Fundamentals of Programming

Switch (1)

The switch statement allows the selection of statements from several available choices.switch (expression of type integer){

case (choice 1): // executed if expression == choice 1

case (choice 2): // executed if expression == choice 2

:case (choice n):

// executed if expression == choice n default:

// default execution }

ti.uni‐due.de Joachim Zumbrägel

Page 19: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

19

Procedural Programming & Fundamentals of Programming

Switch (2)

Take care: When a case is true, all following statements are executed regardless of the case choices.

=> Use break to leave the switch.

switch (expression of type integer){

case (choice 1): statement(s); break; //leave the switch block

case (choice 2): :

}ti.uni‐due.de Joachim Zumbrägel

Procedural Programming & Fundamentals of Programming

Exercise 3

Write a program which simulates the behaviourof an AND Gate and an OR Gate. The gates have two inputs. The program works as follows:

By entering a number the user can choose which gate he wants to simulate. 1 for the AND Gate and 2 if he wants two simulate an OR Gate. Then the user has to enter the two input values. After that the program shows the result of the simulation.

ti.uni‐due.de Joachim Zumbrägel

Page 20: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

25.04.2018

20

Procedural Programming & Fundamentals of Programming

Material

BOOK:• C - Einführung und professionelle Anwendung.

IT-Studienausgabe- Ulla Kirch Prinz, Peter Prinz- ISBN: 978-3826617669

WWW• http://de.wikibooks.org/wiki/C-Programmierung

ti.uni‐due.de Joachim Zumbrägel

Page 21: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

Exercise 1 (Flow Chart)

Start

Input frequency (ín MHZ) Input propDelay (in ns)

n = 1000/f/pd

Output n

End

Page 22: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

Exercise 1 (program)

/* PROG: NrOfAdders

CREATED:24.04.2018 by JOACHIM ZUMBRÄGEL */

#include <stdio.h>

int main()

{

float frequency; float propDelay, float nrOfSubCircuits; printf("Frequency(in MHZ):\n"); scanf("%f", &frequency); printf("Propagation delay (in ns):\n"); scanf("%f",&propagationDelay); nrOfSubCircuits = 1000/frequency/propDelay; printf("\nNr of Sub-Circuits <= %.0f", nrOfSubCircuits); return 0;

}

Page 23: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

Exercise 2 /* Program converts a decimal value * from 1-9 into a nibble * AUTHOR: Joachim Zumbrägel * DATE CREATED:2018-04-24 */ #include <stdio.h> #include <stdlib.h> int main() { unsigned int n=0, b0=0, b1=0, b2=0, b3=0; printf("Convert n(0-9) into a nibble:\n”); printf(“Input n please:"); scanf("%u",&n); b0 = n%2; n = n/2; b1 = n%2; n = n/2; b2 = n%2; n = n/2; b3 = n%2; printf("Nibble is:%d%d%d%d\n", b3,b2,b1,b0); return EXIT_SUCCESS;

}

Page 24: Procedural Programming & Fundamentals of Programming · Lab exercises • Distributed at the beginning of the practical part of the lab • Some simple tasks • Example (first lab):

Exercise 3 #include <stdio.h> #define SIM_AND 1 #define SIM_OR 2 int main() { int select,a,b; printf("Select (1=AND), (2=OR)"); scanf("%d",&select); switch (select) { case SIM_AND: printf("Enter a (0 or 1):"); fflush(stdin); scanf("%d",&a); printf("Enter b (0 or 1):"); fflush(stdin); scanf("%d",&b); printf("result (AND):%d\n", (a && b)); break; case SIM_OR: printf("Enter a (0 or 1):"); fflush(stdin); scanf("%d",&a); printf("Enter b (0 or 1):"); fflush(stdin); scanf("%d",&b); printf("result (OR):%d\n", (a || b)); break; default: printf("Wrong input\n"); } return 0;