410 likes | 449 Views
Programming Fundamentals 1 st lecture. Content. Steps of solving problems – steps of writing a computer program Languages used when programming The alogirithm The specificaion Languages describing and algorithm – structogram Coding a program – programming tool. Steps of solving problems.
E N D
Content • Steps of solving problems–steps of writing a computer program • Languages used when programming • The alogirithm • The specificaion • Languages describing and algorithm– structogram • Coding a program– programming tool
Steps of solving problems Example: build a house What can you see from the process? What is behind? Assess the needs (aspects: size of family, their idea, money) Design (plan, building material needs, engineer) Organize (schedule, contractor…) Building operations (supply material, building / contractor…) Put in practice (have a look – how fancy, try out – how good) Move in, live in (make corrections, detect problems)
Steps of creating a computer program Specify (from what?, what?) specification Design (with what?, how?) data and algorithm desc. Coding (how (computer)?) program source coderepresentation + implementation Testing (any bugs?) error list (diagnosis) Search for bugs (where is the bug?) bug location Correction (how is it correct?) correct program Quality assurance (Q&A), efficiency (can we make better?, how?) good program Documenting (how does it work? etc…) usable program Usage, maintenance (still works?) durable program
Language tiers Living language = English Specification Algorithm description Programming language Computer language (machine code) Converge the languages (EnglishComputer)
The Algorithm Usage of drink dispensing machine: Choose the drink! Insert 1€! Press the proper button! Wait until the drink runs out! Get the dring! Drink it!
The Algorithm Executable (interpreter exists) Can be executed step by step The steps themselves are also algorithms Exactly defined, with given order of steps The description is finite, however the execution time can be infinite
The Algorithm Usage of drink dispensing machine: Choose the drink! Insert 1€! Press the proper button! Repeatlook at the glass!Until the drink runs out! Get the dring! Drink it! New element: Repetition based on a condition
The Algorithm Usage of drink dispensing machine: Choose the drink! If you have 1€ then Insert 1€!otherwise Insert 5 x20 cents! … New element: choice from two options, (can also be non-deterministic)
The Algorithm Insert 5 x 20 cents: Repeat 5 times: Insert one 20 cents! New element: repeat given times
The Algorithm Structural elements of an algorithm: Sequence (execute step by step) Fork (choice from 2 or more activities based on condition) Cycle (repeat given times, or until a condition turns true)
Specification Input data (identifier, domain set, unit) What we know about the input (precondition) Results (identifier, domain, …) The rule how to calculate the result (post condition) Requirements against the solution Restrictions Definitions of the applied notions
Specification Specification has to be Exact, full Short, compact, formalized Expressive, understandable Specification tools Text description Mathematical formulas
Example: triangle(specification) Problem: Is it true that given 3 numbers represent the sidelengths of a right angle triangle? Specification: Input: x,y,z:Real Output: possible:Logical Precondition: x>0 and y>0 and z>0 Post condition: possible=(x2+y2=z2) Comment: we suppose z is the length of hypotenuse
Example: triangle(algorithm) Algorithm: Comment: Later we will not include In and Out in our algorithms
Example: triangle(algorithm) Anotheralgorithm(without In and Out): We can introducehelper (internal, own) variables.
Example: quadratic equation(specification) Problem: Let’s specify one root of a quadratic equation! The equation is: ax2+bx+c=0 Questions: What is the solution? – output What does it mean: „being a solution”? – post condition Does there a solution exist? – precondition Are we sure there is only one solution? – output/post condition
Example: quadratic equation(specification) Specification1: Input: a,b,c:Real Output: x:Real Precondition: – Post condition1: ax2+bx+c=0 Remark: this post condition does not provide us with information how to create the algorithm. No worries, let’s try again!
Example: quadratic equation(specification) Specification2: Input: a,b,c:Real Output: x:Real Precondition: a0What if we allowed? Post condition2: Open questions: Is there always a solution? Is there only one solution?
Example: quadratic equation(specification) Extend the output: Output: x:Real,exists:Boolean Post condition: exists=(b24*a*c) andexists Open question: Is there only one solution? – homework
Example: quadratic equation(specification) Algorithm: I N True way False way
Example: quadratic equation(specification) Algorithm in another representation: ProgramQuadraticEquation: d:=b2-4*a*c exists:=d≥0 Ifexiststhen Program end.
Languages for algorithms Text description Describe by sentences Pseudo code Describe with drawing Flow chart Structogram
Structogram(and pseudo code) Sequence: Fork (2way): Fork (more): Statement1 Statement2 IfConditionthenStatements1else Statements2End if ForkIn case Conition1:Statements1In case Conition2:Statements2 … …OtherwiseStatements otherwiseEnd fork
Structogram(and pseudo code) Loops: How to draw structogram: Text editor / spreadsheet Specific tools (e.g. NSD) Loop whileConditionStatementsEnd loop LoopStatementsUntilConditionEnd loop Loopi=from 1 to nStatementsEnd loop
Coding(programming tool) Framework (tool): Code::Blocks Download: www.codeblocks.org Installation: easy
At first startup: Choose compiler Coding(programming tool)
Steps of usage: Create a project , the type determines the platform of you want to deploy to.Create a new project sablon (template) választása: Console application Coding(programming tool)
Steps of usage: workspace of project on the disk Coding(programming tool) project name project root folder
Further steps of usage: workspace of project on the disk Coding(programming tool) Project name Project root folder projektfájl-név Project file name with full path
Further steps of usage: Choose compiler Finalize Coding(programming tool) compiler development version? debug dirs final version? final version dirs
Our environment: on the disk: in framework: Coding (programming tool) browse program
Our environment: on disk: in framework: Coding(programming tool)
Compiling our first program Coding(programming tool) Szlávi-Zsakó: Programozási alapismeretek 1.
The output of compilation: on the disk: Codeing(programming tool) Szlávi-Zsakó: Programozási alapismeretek 1.
The output of compilation: on the disk: Coding(programming tool)
Our first program: the content ofmain.cpp : Coding(programming tool) #include <iostream> usingnamespace std; intmain() { cout << "Hello world!" << endl; return 0; }
The project source file: The content offirstProg.cbp: Coding(programming tool) (mily meglepő!)
Run the exe in console: „compilation” – run (the last compiled) – compile & run – the console looks like this: Coding (programming tool) execution time The output of the program returned value Start theexe directly from file system!What do you experience? Why?