• No results found

An Overview of Programming Languages, Problem Solving Techniques and Paradigms

N/A
N/A
Protected

Academic year: 2020

Share "An Overview of Programming Languages, Problem Solving Techniques and Paradigms"

Copied!
39
0
0

Loading.... (view fulltext now)

Full text

(1)

LECTURE 4 : Week 4

An Overview of Programming

Languages, Problem Solving

(2)

C

S

C

-1

10

om

pu

tin

g

F

un

d

am

en

ta

ls

2

Programming Languages

Various programming languages enable

people to tell computers what to do

(3)

C

S

C

-1

10

C

om

pu

tin

g

F

un

d

am

en

ta

ls

3

The Language Translation Process

How are Programs Understood by the Computer?

Program written in programming

language

(source code)

Translator program  Assembler  Compiler  Interpreter

Program written in machine language (object code)

(4)

C

S

C

-1

10

om

pu

tin

g

F

un

d

am

en

ta

ls

4

Programming Languages

Machine Language

(first generation of programming languages)

The computer’s ‘native language’

Composed of binary digits (0s, 1s)

The only language that computers understand

Assembly Language

(second generation of programming languages)

One-to-one correspondence to machine language

Somewhat more user-friendly than machine language

(mnemonic rather than binary digits)

(5)

C

S

C

-1

10

C

om

pu

tin

g

F

un

d

am

en

ta

ls

5

Programming Languages

Procedural Languages (High Level Languages)

(third generation languages)

One instruction translates into many machine language

instructions

Programs describe the computer’s processing

step-by-step

Closer to natural language; uses common words rather

than abbreviated mnemonics

Examples:

C, Fortran, QuickBasic

Compiler

- translates the entire program into machine

language

(6)

CSC-110 Computing Fundamentals 6

Assembly language

Can be written as follows:

LOAD rate

MULT hour

STOR wages

Programming Languages

Represent the equation wages = rate · hours to calculate the weekly wages

Machine language

100100 stands for load

100110 stands for multiplication

100010 stands for store Can be written as follows:

100100 010001 100110 010010 100010 010011

High level language

(7)

C

S

C

-1

10

C

om

pu

tin

g

F

un

d

am

en

ta

ls

7

Programming Languages

Nonprocedural Language

(fourth generation languages)

Allows the user to specify the desired result without

having to specify the detailed procedures needed for

achieving the result

Example:

data base query language - SQL

Can be used by non technical users

Natural Language Programming Languages

(fifth generation (intelligent) languages)

Translates natural languages into a structured,

machine-readable form

(8)

C

S

C

-1

10

om

pu

tin

g

F

un

d

am

en

ta

ls

8

(9)

C

S

C

-1

10

C

om

pu

tin

g

F

un

d

am

en

ta

ls

9

Current Programming Languages

 Visual Programming Languages

 Used within a graphical environment  Example: Visual Basic and Visual C++  Popular to non technical users

 Hypertext Markup Language (HTML)

 standard language used in World Wide Web

 contains text, images, and other types of information such as data files, audio, video, and executable computer programs

 Componentware

 Software components that may be assembled by developer as needed

(10)

C

S

C

-1

10

om

pu

tin

g

F

un

d

am

en

ta

ls

10

Current Programming Languages

Extensible Markup Language (XML)

Improved on web document functionality

Object-Oriented Programming Languages (OOP)

based on objects – packaging data and the

instructions about what to do with that data

together

Examples: Java, C++

(11)

CSC-110 Computing Fundamentals 11

Processing A High Level Language Program

 Steps to execute a program written in C++:

1. Use an editor to create a program. (source program)

2. Compiler translate the program in to an equivalent machine language. (object program)

3. The programs that you write in a high-level language are developed using a software development kit (SDK), which contains many programs that are useful in creating your program. This prewritten code resides in a place called the

library.

4. The next step is to load the executable program into the main memory for execution and a program called loader accomplishes this.

(12)

C

S

C

-1

10

om

pu

tin

g

F

un

d

am

en

ta

ls

12

Step in Program Development

 Programming can be defined as the development of a solution to an identified problem, and the setting up of a related series of instructions that will produce the desired results.

Define the Problem

 To help with initial analysis, the problem should be divided into three separate components:

 the inputs

 the outputs

 the processing steps to produce the required outputs

Outline the Solution

 This initial outline is usually a rough draft of the solution and may include:

 The major processing steps involved

 The major subtasks (if any)

 The user interface (if any)

 The major control structures (e.g. repetition loops)

 The major variables and record structures

(13)

C

S

C

-1

10

C

om

pu

tin

g

F

un

d

am

en

ta

ls

13

Step in Program Development

Develop the Outline into an Algorithm

 The solution outline developed in Step 2 is expanded into an algorithm: a set of precise steps that describe exactly the tasks to be performed and the order in which they are to be carried out.

Test the Algorithm for Correctness

 Most importance in the development of a program

 To identify major logic errors early, so that they may be easily corrected.

Code the Algorithm into a Specific Programming Language

 Only after all design considerations have been met

Run the program on the computer

 This step uses a program compiler and programmer-designed test data to machine test the code for syntax errors and logic errors.

Document and Maintain the program

 It is an ongoing task from the initial definition of the problem to the final test result.

 Documentation involves both external and internal documentation that may have been coded in the program.

(14)

C

S

C

-1

10

om

pu

tin

g

F

un

d

am

en

ta

ls

14

Programming With The Problem

Analysis – Coding – Execution Cycle

Problem solving process

First step

 Define the problem.

 Outline the solution.

 Design an algorithm.

 Test the algorithm for correctness.

Second step

 Implement the algorithm in programming language, such as C++.

 Run the program on the computer

Last step

(15)

C

S

C

-1

10

C

om

pu

tin

g

F

un

d

am

en

ta

ls

15

Procedural vs. Object-Oriented

Programming

Procedural programming

 is based on a structured, top-down approach to writing effective programs.

concentrates on ‘what’ a program has to do and involves identifying and organizing the ‘processes’ in the program solution

 Decomposed into separate tasks or functions and includes

top-down development broken down problem into more detailed steps

modular design  grouping task together because they all perform the same function; connected to top-down development

Object-Oriented Programming

 OOP is also based on decomposing the problem.

(16)

CSC-110 Computing Fundamentals 16

Procedural vs. Object-Oriented Programming

Procedural programming

(17)

C

S

C

-1

10

C

om

pu

tin

g

F

un

d

am

en

ta

ls

17

Top-Down Development

If we look at a problem as a whole, it may seem

impossible to solve because it is so complex.

Examples:

 writing a tax computation program  Writing a library system

Writing a Bank System

Complex problems can be solved using top-down design,

also known as stepwise refinement, where

 We break the problem into parts

 Then break the parts into smaller parts

(18)

C

S

C

-1

10

om

pu

tin

g

F

un

d

am

en

ta

ls

18

Advantages of Top-Down

Development

 Breaking the problem into parts helps us to clarify what needs to be done.

 At each step of refinement, the new parts become less complicated and, therefore, easier to figure out.

 Parts of the solution may turn out to be reusable.

 Breaking the problem into parts allows more than one person to work on the solution.

Problem:

(19)

C

S

C

-1

10

C

om

pu

tin

g

F

un

d

am

en

ta

ls

19

The Top Level

Get the customer list from a file.

Sort the list according to zip code.

Make a new file of only the customers with the zip code

43100 from the sorted customer list.

Print an envelope for each of these customers.

Main

(20)

C

S

C

-1

10

om

pu

tin

g

F

un

d

am

en

ta

ls

20

Another Level?

 Should any of these steps be broken down further? Possibly!

 How do I know? Ask yourself whether or not you could easily write the algorithm for the step. If not, break it down again.

 When you are comfortable with the breakdown, write the pseudocode for each of the steps (modules) in the hierarchy.

 Typically, each module will be coded as a separate function.

Another Example:

A Top-down analysis of a simple cooking task

Cook Breakfast

Bring out cook ware

Get Coffee Pot

Get

Frying pan Get fork

Get Serving dishes Take out

(21)

C

S

C

-1

10

C

om

pu

tin

g

F

un

d

am

en

ta

ls

21

What is an Algorithm?

A step-by-step problem solving procedure, especially an

establish, recursive computational procedure for solving a

problem in a finite number of steps.

There are many models supporting the development of

the code:

Pseudocode

Structure Diagram

and finally in C++

Flowcharts

An algorithm has to be clear

have a finite length stop in finite time

(22)

CSC-110 Computing Fundamentals 22

Input Processing Output

Names for our cells

5, 10 15

1) Declare variables input_1

input_2 sum

2) Assign values input_1 = 5

input_2 = 10

3) Process

sum = input_1 + input_2

The computer (and so C) provides basic arithmetic operations. If the operation

you want to use is not provided, you have to compose it.

(23)

CSC-110 Computing Fundamentals 23

PROGRAM Add Two Numbers READ two numbers ADD the numbers WRITE the sum END PROGRAM Version 1:

PROGRAM Add Two Numbers READ First

READ Second

Sum = First + Second WRITE Sum

END PROGRAM Version 2:

Pseudocode

Write a program calculating the sum of two numbers

(24)

CSC-110 Computing Fundamentals 24

Version 1: PROGRAM

Add Two Numbers

Version 2: READ

Two Numbers

ADD

Two Numbers

WRITE The Sum

PROGRAM

Add Two Numbers

READ

Two Numbers

ADD

Two Numbers

WRITE The Sum

Sum = Input_1 +

Input_2

Take note:

We develop software iteratively (meaning version by version), but the code itself

is broken in top-down manner!!

Structure Diagram

 Write a program calculating the sum of two numbers

READ Input_1

(25)

CSC-110 Computing Fundamentals 25

START

STOP READ First READ Second

Sum = First + Second

WRITE Sum

Flowcharts

Write a program calculating the sum of two numbers

Flowcharting symbols

Input/Output (used for all I/O operations) Processing (used for all arithmetic and data transfer operations).

Terminal (used to indicate the beginning and end of a program or module).

Decision (used to test for a condition).

Connector (used to indicate the point at which a transfer of control operation occurs).

Predefined (used to indicate the name process of a module to be executed).

(26)

CSC-110 Computing Fundamentals 26

Pseudocode are more closely to the programming language

and for the advanced programmer.

Structure Diagrams are helpful to break the algorithm into more manageable pieces.

Flowcharts show the workflow of the algorithm and stress on structured programming.

Every model has its advantages and disadvantages. But all try to help you to structure your code in a top-down style and this is the way you should implement your algorithm.

Modular Design

 Write a program calculating the sum of two numbers

Modular Design

We don’t break according to the sequence

in which the parts are following, but rather in its frequency of use and

(27)

CSC-110 Computing Fundamentals 27

/* Addition of 2 numbers */

#include <iostream> using namespace std;

int main() {

int first, second, sum;

cin>>first; cin>>second;

sum = first + second; cout<<sum<<endl;

return 0; }

/* Addition of 2 numbers */

#include <iostream> using namespace std;

int main() {

int first, second, sum;

cin>>first; cin>>second;

sum = first + second; cout<<sum<<endl;

return 0; }

Where do you find now in the code the steps we have developed?

1. Declaration of the structure

2. Reading the input 3. Processing the data 4. Writing the output

(28)

C

S

C

-1

10

om

pu

tin

g

F

un

d

am

en

ta

ls

28

Programming Guideline

The

following

rules

are

intended

to

avoid

some

mistakes students normally do.

Even,

if

you

have

not

(29)

C

S

C

-1

10

C

om

pu

tin

g

F

un

d

am

en

ta

ls

29

Programming Guideline

Rule 1: Think about your strategy twice! Choose a strategy possibly coming out with less program lines.

Rule 2: You cannot program, what you cannot do with paper and pencil.

It is worth to rethink your strategy. - Do I really have chosen the best? - Can I reformulate my problem to make it more simple?

Even after having an algorithm ready in mind.

Play with your data. Play to be the computer. You are only allowed to perform actions a computer can. Can you solve your problem this way?

(30)

C

S

C

-1

10

om

pu

tin

g

F

un

d

am

en

ta

ls

30

Programming Guideline

Rule 3: The compiler must be easy and in short time accessible.

Rule 4: (Data) Structure before functionality!

First declare all the variables you will need. (Later you will use more advanced structures like

objects, trees, graphs,..)

But in all cases: The data structure you need has to be ready first.

You can still use old DOS-shells, if you want. But open at least two windows, one for editing, one for compiling the

code. Prepare your programming

(31)

C S C -1 10 C om pu tin g F un d am en ta ls 31

Programming Guideline

Rule 5: Name your variables appropriate and format your code in a way, that the structure is easily detectable.

Rule 6: Write code using a divide and conquer approach. (Top-down

)

while (counter < 100) {

if (no > 1) {

... }

}

while (counter < 100){ if (no > 1)

{ ... } }

Cycle Test Express PROGRAM Add Two Numbers

(32)

C S C -1 10 om pu tin g F un d am en ta ls 32

Programming Guideline

Rule 7: Compile every three lines. Write code in such a way, that it compiles and executes at every moment of the code development!

Rule 8: Do your best in writing the documentation, learn from your code. Make sure you do not do the same mistakes again.

Cycle Test

Express

Whatever we do, our code always compiles and executes!!

Compile very often! Detect a mistake as soon as possible!

Comment clearly

/* Addition of 2 numbers */

#include <iostream> using namespace std;

int main() {

/* The input numbers */ int first, second;

/* The output number */ int sum;

....

/* Addition of 2 numbers */

#include <iostream> using namespace std;

int main() {

/* The input numbers */ int first, second;

/* The output number */ int sum;

(33)

C

S

C

-1

10

C

om

pu

tin

g

F

un

d

am

en

ta

ls

33

 Why are there hundreds of programming languages in use today?

 Some programming languages are specifically designed for use in certain applications.

 Different programming languages follow different approaches to solving programming problems

 A programming paradigm is an approach to solving programming problems.

 A programming paradigm may consist of many programming languages.

Common programming paradigms:

 Imperative or Procedural Programming  Object-Oriented Programming

 Functional Programming  Logic Programming

(34)

C

S

C

-1

10

om

pu

tin

g

F

un

d

am

en

ta

ls

34

 In this paradigm, a program is a series of statements containing

variables.

 Program execution involves changing the memory contents of the

computer continuously.

 Example of imperative languages are: C, FORTRAN, Pascal, COBOL

etc

Advantages

 low memory utilization  relatively efficient

 the most common form of programming in use today.

Programming Paradigms: Imperative

Disadvantages

difficulty of reasoning about programs

difficulty of parallelization.

(35)

C

S

C

-1

10

C

om

pu

tin

g

F

un

d

am

en

ta

ls

35

 A program in this paradigm consists of objects which communicate with each other by sending messages

 Example object oriented languages include: Java, C#, Smalltalk, etc

Advantages

Conceptual simplicity

Models computation better

Increased productivity.

Programming Paradigms: Object-Oriented

Disadvantages

Can have a steep learning curve, initially

(36)

C

S

C

-1

10

om

pu

tin

g

F

un

d

am

en

ta

ls

36

 A program in this paradigm consists of functions and uses functions in a similar way as used in mathematics

 Program execution involves functions calling each other and

returning results. There are no variables in functional languages.

 Example functional languages include: ML, MirandaTM, Haskell

 Advantages

 Small and clean syntax

 Better support for reasoning about programs

They allow functions to be treated as any other data values.

 They support programming at a relatively higher level than the

imperative languages

Programming Paradigms: Functional

(37)

C

S

C

-1

10

C

om

pu

tin

g

F

un

d

am

en

ta

ls

37

 A program in the logic paradigm consists of a set of predicates and rules of inference.

Predicates are statements of fact like the statement that says: water is wet. Rules of inference are statements like: If X is human then X is mortal.

 The predicates and the rules of inference are used to prove

statements that the programmer supplies.  Example: Prolog

Advantages

Good support for reasoning about programs  Can lead to concise solutions to problems   Disadvantages

Slow execution

Limited view of the world

 That means the system does not know about facts that are not

its predicates and rules of inference.

Difficulties in understanding and debugging large programs

(38)

C

S

C

-1

10

om

pu

tin

g

F

un

d

am

en

ta

ls

38

 Which of these paradigms is the best?

The most accurate answer is that there is no best paradigm.

 No single paradigm will fit all problems well.

Human beings use a combination of the models represented by these

paradigms.

 Languages with features from different paradigms are often too complex.

 So, the search of the ultimate programming language continues!

(39)

C

S

C

-1

10

C

om

pu

tin

g

F

un

d

am

en

ta

ls

39

1. List two advantages and two disadvantages of low-level languages.

2. Explain the similarities and differences between an assembly language and a machine language.

3. Mention the programming paradigm to which each of the following languages belongs: Visual Basic, Java, C#, Haskell, Lisp, Prolog, Pascal.

4. Which programming paradigms give better support for reasoning about programs?

5. Which programming paradigms give better support for doing I/O?

References

Related documents

(a) Struggle by the people (b) Invasion by foreign countries (c) End of colonialism (d) People's desire for freedom Ans. Which of the following statement is true

Nuestro trabajo, de todas formas, detecta este uso de una forma muy individualizada: en algunos hablantes (pocos) el ap prehodiernal aorístico es una forma nor- mal para la

As we have defined, anti-surveillance is described as the actions or manoeuvres that a person carries out in order to get a reaction, and therefore confirm that

&#34;Borch,&#34; replied Geralt, white head turning away from his horse, looking into the bright eyes of the stranger, &#34;I'd like you to know so that there be no

[r]

第一章  绪论  3   

Write a JavaScript code to ask user to enter at least two numbers and output the sum and average of these two numbers in alert box using a function... Write a script code to

To use cited patents in common, as clustering attributes, can be an alternative process to create new groups in subgroups level of classification systems, where