Procedural Programming - ti.uni-due.de · 03.05.2017 2 ProceduralProgramming Principles of the...
Transcript of Procedural Programming - ti.uni-due.de · 03.05.2017 2 ProceduralProgramming Principles of the...
03.05.2017
1
Procedural Programming
Exercise 1 (SS 2017) ■ 02.05.2017
Procedural Programming
Procedural Programming?
• consists of 6 Lectures, 6 Exercises each with 1 home exercise 8 Practical Trainings
• has no exam
• is passed when you got 96 points from 114(18 from home exercises, 96 from the lab)
ti.uni‐due.de Joachim Zumbrägel
03.05.2017
2
Procedural Programming
Principles of the exercises
ti.uni‐due.de Joachim Zumbrägel
• We have 6 exercise dates
• We discuss topics of the lecture
• We discuss exercises and their solutions
• You can get points for home exercises (3 points per solved exercise, all together 18)
• Home exercises are announced at the end of the exercise and must be solved within 1 week
Procedural Programming
Principles of the practical training
ti.uni‐due.de Joachim Zumbrägel
• We have 8 different labs
• Each labs consists of - homework - handout (3 points), checked
during the lab- a test before the lab (3 points)- the practical training itself (6 points)
• => 12 points per lab
• => 96 points for all labs
03.05.2017
3
Procedural Programming
Schedule
ti.uni‐due.de Joachim Zumbrägel
Date Le. (2h) Ex. (2h) P (2h) Topic of the practical training18.04.2017 L125.04.2017 L202.05.2017 E109.05.2017 E216.05.2017 L323.05.2017 L4 P1 Variables, Data Types, Statements, Hello World30.05.2017 E3 P2 Boolean expressions, Conditional Executions Nested06.06.2017 Pfingstferien13.06.2017 L5 P3 Finite and Infinite Loops, Break and Continue20.06.2017 L6 P4 Functions: Parameters and Arguments, Void 27.06.2017 E4 P5 Arrays04.07.2017 E5 P6 Addressing: Direct and Indirect Addressing to Pointers11.07.2017 E6 P7 Strings and Files18.07.2017 P8 Math Functions, Linked Lists, and Binary Trees25.07.2017 RT Repetition Date for P1‐P8, used for missed labs
Procedural Programming
Practical training time and location
ti.uni‐due.de Joachim Zumbrägel
DatesTuesday: 12:00 – 13:30, 14:00 – 15:30
Wednesday: 15:45 – 17:15
Rooms:BA 028, BB 219
03.05.2017
4
Procedural 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
Procedural 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
03.05.2017
5
Procedural 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
Procedural 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
03.05.2017
6
Procedural 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
Procedural Programming
Flow chart symbols
ti.uni‐due.de Joachim Zumbrägel
03.05.2017
7
Procedural 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)
Procedural Programming
• 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
Kiss principle
ti.uni‐due.de Joachim Zumbrägel
03.05.2017
8
Procedural 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
Procedural 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
03.05.2017
9
Procedural 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
Procedural Programming
Program for exercise 1
/* PROG: NrOfAdders
CREATED:07.05.2013 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 must be smaller than:%.0f",nrOfSubCircuits);
return 0;
}
ti.uni‐due.de Joachim Zumbrägel
03.05.2017
10
Procedural 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
Procedural 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
What means after the 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. ⇒Remember: # indicates preprocessing
ti.uni‐due.de Joachim Zumbrägel
03.05.2017
11
Procedural 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
Procedural 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
03.05.2017
12
Procedural 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
Procedural 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
03.05.2017
13
Procedural 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
Procedural 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
03.05.2017
14
Procedural 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
Procedural 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
03.05.2017
15
Procedural 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
Procedural 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
RESTATE THE PROBLEM => New drawing of the circuit: A FA is seen as an device with 2 inputs and two outputs, ai,bi is seen as 1 input.
Device n
sn cn
an, bn
Device 3
s2c2
a2,b2
Device2
s1c1
a1,b1
Device 1
c0 s0
a0,b0
…..
0 INPUT starts t=0
(t1=2ns=1*2ns)
(t2=4ns=2*2ns)
(t3=6ns=3*2ns)
(tn = n*2ns)