Top Banner
Programming Fundamentals 1 st lecture
40

Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content Steps of solving problems – steps of writing a computer.

Apr 01, 2015

Download

Documents

Eden Goolsby
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

Programming Fundamentals

1st lecture

Page 2: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

22/42/4223.04.11.23.04.11.

Content

Steps of solving problems –steps of writing a computer

programLanguages used when programming

The alogirithmThe specificaionLanguages describing and algorithm –

structogram Coding a program –

programming tool

Page 3: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

33/42/4223.04.11.23.04.11.

Steps of solving problems

Example: build a house What can you see from the process? What is behind?1. Assess the needs (aspects: size of family,

their idea, money)2. Design (plan, building material needs,

engineer)3. Organize (schedule, contractor…)4. Building operations (supply material,

building / contractor…)5. Put in practice (have a look – how fancy, try

out – how good)6. Move in, live in (make corrections, detect

problems)

Page 4: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

44/42/4223.04.11.23.04.11.

Steps of creating a computer program

1.Specify (from what?, what?) specification2.Design (with what?, how?) data and

algorithm desc.3.Coding (how (computer)?) program source

code representation + implementation4.Testing (any bugs?) error list (diagnosis)5.Search for bugs (where is the bug?) bug

location6.Correction (how is it correct?) correct

program7.Quality assurance (Q&A), efficiency (can

we make better?, how?) good program8.Documenting (how does it work? etc…)

usable program9.Usage, maintenance (still works?) durable

program

Page 5: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

55/42/4223.04.11.23.04.11.

Language tiers

Living language = English

Specification

Algorithm description

Programming language

Computer language (machine code)

Converge the languages (English Computer)

Page 6: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

66/42/4223.04.11.23.04.11.

The Algorithm

Usage of drink dispensing machine:

1. Choose the drink!2. Insert 1€!3. Press the proper button!4. Wait until the drink runs out!5. Get the dring!6. Drink it!

Page 7: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

77/42/4223.04.11.23.04.11.

The Algorithm

Executable (interpreter exists)Can be executed step by stepThe steps themselves are also algorithms

Exactly defined, with given order of steps

The description is finite, however the execution time can be infinite

Page 8: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

88/42/4223.04.11.23.04.11.

The Algorithm

Usage of drink dispensing machine:

1. Choose the drink!2. Insert 1€!3. Press the proper button!4. Repeat

look at the glass!Until the drink runs out!

5. Get the dring!6. Drink it!

New element: Repetition based on a condition

Page 9: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

99/42/4223.04.11.23.04.11.

The Algorithm

Usage of drink dispensing machine:1. Choose the drink!2. If you have 1€

then Insert 1€!otherwise Insert 5 x 20

cents!3. …

New element: choice from two options, (can also be non-deterministic)

Page 10: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

1010/42/4223.04.11.23.04.11.

The Algorithm

Insert 5 x 20 cents:1. Repeat 5 times:

Insert one 20 cents!

New element: repeat given times

Page 11: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

1111/42/4223.04.11.23.04.11.

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)

Page 12: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

1212/42/4223.04.11.23.04.11.

Specification

1. Input data (identifier, domain set, unit)

2. What we know about the input (precondition)

3. Results (identifier, domain, …)4. The rule how to calculate the

result (post condition)5. Requirements against the

solution6. Restrictions7. Definitions of the applied notions

Page 13: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

1313/42/4223.04.11.23.04.11.

Specification

Specification has to be1. Exact, full2. Short, compact, formalized3. Expressive, understandableSpecification tools1. Text description2. Mathematical formulas

Page 14: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

1414/42/4223.04.11.23.04.11.

Example: triangle(specification)

Problem:Is it true that given 3 numbers represent the side lengths of a right angle triangle?

Specification:Input: x,y,z:RealRealOutput: possible:LogicalLogicalPrecondition: x>0 and y>0 and z>0Post condition: possible=(x2+y2=z2)

Comment: we suppose z is the length of hypotenuse

Page 15: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

1515/42/4223.04.11.23.04.11.

Example: triangle(algorithm)

Algorithm:

Comment: Later we will not include In and Out in our algorithms

In: x,y,z [x>0 and y>0 and z>0]possible:=(x2+y2=z2)

Out: possible

Page 16: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

1616/42/4223.04.11.23.04.11.

Example: triangle(algorithm)

Another algorithm (without In and Out):

We can introduce helper (internal, own) variables.

xx:=x2

yy:=y2

zz:=z2

possible:=(xx+yy=zz)

Page 17: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

1717/42/4223.04.11.23.04.11.

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

Page 18: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

1818/42/4223.04.11.23.04.11.

Example: quadratic equation

(specification)

Specification1:Input: a,b,c:RealRealOutput: x:RealRealPrecondition: – 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!

Page 19: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

1919/42/4223.04.11.23.04.11.

Example: quadratic equation

(specification)

Specification2:Input: a,b,c:RealRealOutput: x:RealRealPrecondition: a0

What if we allowed?

Post condition2:

Open questions: Is there always a solution? Is there only one solution?

a2ca4bb

x2

Page 20: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

2020/42/4223.04.11.23.04.11.

Example: quadratic equation

(specification)

Extend the output:Output: x:RealReal,, exists:BooleanBooleanPost condition: exists=(b24*a*c) and

exists

Open question: Is there only one solution? –

homework

a2ca4bb

x2

Page 21: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

2121/42/4223.04.11.23.04.11.

Example: quadratic equation

(specification)

d:=b2-4*a*c

exists:=d0

exists?

a2db

:x

True wayTrue way

False wayFalse way

II NN

Algorithm:

Page 22: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

2222/42/4223.04.11.23.04.11.

Example: quadratic equation

(specification)

Algorithm in another representation:

Program QuadraticEquation:

d:=b2-4*a*c

exists:=d≥0

If exists then

Program end.

a2db

:x

Page 23: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

2323/42/4223.04.11.23.04.11.

Languages for algorithms

Text description Describe by sentences Pseudo code

Describe with drawing Flow chart Structogram

Page 24: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

2525/42/4223.04.11.23.04.11.

Structogram(and pseudo code)

Sequence:

Fork (2way):

Fork (more):

Statement1Statement1Statement2Statement2

IfIf ConditionCondition thenthen Statements1Statements1elseelse Statements2Statements2End ifEnd if

ForkFork In case In case Conition1:Statements1Conition1:Statements1 In case In case Conition2:Statements2 Conition2:Statements2 … … … … OtherwiseOtherwise Statements otherwiseStatements otherwiseEnd forkEnd fork

Statement1

Statement2

Condition

Statements1 Statements2

Condition1 Condition2 … Otherwise

Statements1 Statements2 … Statements

Page 25: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

2626/42/4223.04.11.23.04.11.

Structogram(and pseudo code)

Loops:

How to draw structogram: Text editor / spreadsheet Specific tools (e.g. NSD)

Loop whileLoop while ConditionCondition StatementsStatementsEnd loopEnd loop

LoopLoop StatementsStatementsUntilUntil ConditionConditionEnd loopEnd loop

LoopLoop ii==from 1 to nfrom 1 to n StatementsStatementsEnd loopEnd loop

Condition

Statements

Condition

Statements

i=1..n

Statements

Page 26: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

2727/42/4223.04.11.23.04.11.

Coding(programming tool)

Framework (tool):Code::Blocks

Download:www.codeblocks.org

Installation:easy

Page 27: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

2828/42/4223.04.11.23.04.11.

At first startup:Choose compiler

Coding(programming tool)

Page 28: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

2929/42/4223.04.11.23.04.11.

Steps of usage:1. Create a project , the type

determines the platform of you want to deploy to.Create a new project

2. sablon (template) választása: Console application

Coding(programming tool)

Page 29: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

3030/42/4223.04.11.23.04.11.

Steps of usage: workspace of project on the

diskproject name

project root folder

Coding(programming tool)

Page 30: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

3131/42/4223.04.11.23.04.11.

Further steps of usage: workspace of project on the

diskProject name

Project root folder

projektfájl-név

Project file name with full

path

Coding(programming tool)

Page 31: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

3232/42/4223.04.11.23.04.11.

Further steps of usage: Choose compiler Finalizecompiler

development version?

debug dirs

final version?

final version dirs

Coding(programming tool)

Page 32: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

3333/42/4223.04.11.23.04.11.

Our environment: on the disk:

in framework:

browse program

Coding(programming tool)

Page 33: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

3434/42/4223.04.11.23.04.11.

Our environment: on disk:

in framework:

Coding(programming tool)

Page 34: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

Szlávi-Zsakó: Programozási alapismeretek 1.Szlávi-Zsakó: Programozási alapismeretek 1. 3535/42/4223.04.11.23.04.11.

Compiling our first program

Coding(programming tool)

Page 35: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

Szlávi-Zsakó: Programozási alapismeretek 1.Szlávi-Zsakó: Programozási alapismeretek 1. 3636/42/4223.04.11.23.04.11.

The output of compilation: on the disk:

Codeing(programming tool)

Page 36: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

3737/42/4223.04.11.23.04.11.

The output of compilation: on the disk:

Coding(programming tool)

Page 37: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

3838/42/4223.04.11.23.04.11.

Our first program: the content of main.cpp :

#include <#include <iostreamiostream>>

usingusing namespacenamespace std; std;

intint main()main(){{ cout <<cout << "Hello world!" "Hello world!" << endl;<< endl; returnreturn 0 0;;}}

Coding(programming tool)

Page 38: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

3939/42/4223.04.11.23.04.11.

The project source file: The content of firstProg.cbp :

(mily meglepő!)(mily meglepő!)

Coding(programming tool)

Page 39: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

ELTEELTE

4040/42/4223.04.11.23.04.11.

Run the exe in console: „compilation” – run (the last compiled) – compile & run – the console looks like this:

Start theStart the exe exe directly from file directly from file system!system!

What do you experience? What do you experience? Why?Why?

returned value

execution time

The output of

the program

Coding (programming tool)

Page 40: Programming Fundamentals 1 st lecture. ELTE 2/42 2014. 12. 11.2014. 12. 11.2014. 12. 11. Content  Steps of solving problems – steps of writing a computer.

Programming Fundamentals

End of 1st lecture