28
© Janice Regan, CMPT 128, Jan. 2007 1 CMPT 128 Introduction to Computing Science for Engineering Students Creating a program

CMPT 128 Introduction to Computing Science for Engineering Students

  • Upload
    oprah

  • View
    62

  • Download
    0

Embed Size (px)

DESCRIPTION

CMPT 128 Introduction to Computing Science for Engineering Students. Creating a program. Programs in High Level Languages. Assembler is easier to read/write than machine language but still very awkward High level languages are easier to write than assembler The compiler is more complex - PowerPoint PPT Presentation

Citation preview

Page 1: CMPT 128 Introduction to Computing Science for Engineering Students

© Janice Regan, CMPT 128, Jan. 2007 1

CMPT 128Introduction to Computing

Science for Engineering Students

Creating a program

Page 2: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 2

Programs in High Level Languages Assembler is easier to read/write than

machine language but still very awkward High level languages are easier to write

than assembler The compiler is more complex

This course uses the languages C and C++ to implement many ideas that can also be implemented in other high level languages

Page 3: CMPT 128 Introduction to Computing Science for Engineering Students

Algorithm A sequence of precise instructions that

leads to a solution

Order of instructions is important

Amount of detail must be appropriate

Janice Regan CMPT 128 2013 3

Page 4: CMPT 128 Introduction to Computing Science for Engineering Students

Program Design Problem Solving phase

Define the problem Design the algorithm to solve the problem Test the algorithm

Implementation Translate the algorithm to a computer

language like C or C++ Test the program extensively

Janice Regan CMPT 128 20134

Page 5: CMPT 128 Introduction to Computing Science for Engineering Students

Testing? To test you must

Know how to run a program Know how to debug a program Understand how to identify bugs Know how to design the tests to run

Janice Regan CMPT 128 2013 5

Page 6: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 6

Summary:Running a Computer Program

Compiler Converts a source file (containing your human

readable program in C++) to and object file (computer readable binary file)

Linker Converts object program to executable program

Compile Link/load ExecuteC languageProgram inText file(source file)

MachinelanguageProgram In binary file(object file)

Program output

Input data

Otherobject files

Page 7: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 7

Preparing your program to run

Editor, Type inyour program

Source File

Text saved on hard disk

CompilerTranslate text into

Machine Readable code

Page 8: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 8

Source files Contains the text you type into a text

editor The text is a program The program is a list of instructions written

in a special Human readable high level computer language like C, C++ or Java

The program implements the algorithm designed to solve the problem

Page 9: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 9

Creating Source files Create your source file using a text editor

like the one supplied in Microsoft C++

Do not write your code using a word processor like Microsoft Word. A word processor will save in a special format.

The compiler reads only text, not special formats.

Page 10: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 10

Source file to Object file The program can be translated, from the Human

readable language (in your source file) to a machine readable language (in your object file)

A compiler is a special piece of software used to translate from source files to object files

Each high level language (C, C++, Java …) has its own compiler (a different one for each OS)

Page 11: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 11

Perfect Code? It is highly unlikely than any of us will

always write perfect code that contains no errors

How do we find errors? Use a debugger

Are there different kinds of errors? Yes

How do we fix errors? ????

Page 12: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 12

Kinds of Errors What are the kinds of errors?

Types based on properties of the error itself syntax errors semantic errors logical errors

Types based on when the error is detected Compile errors Link errors Run Time Errors

Page 13: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 13

Syntax Errors Any language follows simple rules

how words and punctuation of different types may be combined.

In English syntax is similar to grammatical structure

The compiler can detect errors that break the simple rules defining the syntax of the computer language

When the program is compiled the compiler will tell us if there are any syntax errors

Page 14: CMPT 128 Introduction to Computing Science for Engineering Students

Examples: syntax errors connectn.cpp(57) : error C2146: syntax

error : missing ';' before identifier 'cout’

connectn.cpp(68) : error C2065: 'newt' : undeclared identifier newt has not been created

Janice Regan CMPT 128 2013 14

Page 15: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 15

Semantic Errors Semantics relates to the meaning of the words

in a sentence or a computer language command

Just like a grammatically correct English sentence can be nonsense, a syntactically correct high level computer language command can contain semantic errors

Some semantic errors may be found by the compiler, some will be found when the program is linked, some may be found at run time

Page 16: CMPT 128 Introduction to Computing Science for Engineering Students

Example: semantic error error C3861: 'setw': identifier not found

Can’t find library containing this function Without the library ‘setw’ has no meaning

Janice Regan CMPT 128 2013 16

Page 17: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 17

Logical Errors When your program completes but gives

an unexpected answer it usually means there is a error in the logic of your solution of the problem

Logic errors can also cause a program to fail part way through execution

Page 18: CMPT 128 Introduction to Computing Science for Engineering Students

Compile time errors Syntax errors

The compiler will list any syntax errors we have made

If there are syntax errors no object file is created

Some semantic errors

Janice Regan CMPT 128 2013 18

Page 19: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 19

Correcting compile time errorsEditor, Type inyour programSource File

CompilerTranslation toMachine code

Syntax or Semantic Errors, Compiler Generates error Messages. To help us find and Correct errors in the

Source File

Object File

Binary, machine readable file

Syntactically Correct

code

Code with

syntaxOr

semantic errors

Editor, CorrectSyntax Errors

Page 20: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 20

Perfect Code? If your code contains no compile time

errors is it correct? NOT NECESSARILY

How do we find the remaining errors? Tell the compiler to do more checks use another tool

Page 21: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 21

Link errors When linking the code to libraries etc.

The linker resolves references, words in your program than are defined elsewhere Finds the definitions of those words in the

libraries

Errors occur when the definitions cannot be found

Errors occur when the use of the word does not correspond to the definition

Page 22: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 22

Linking your programEditor, Enter

program

Source File

(Text)

Compile

Find Syntax and

SemanticErrors

Object File

(binary)

CorrectErrors

LinkerResolves

References among

object files

Other Object FilesLibraries …

Executable File(load module)

(binary)

Semantic Errors Reported: Finds words with no defined meaning.

Code has linker errors

Page 23: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 23

Perfect Code? If your code compiles and contains no

errors that can be found by the is it correct? NOT NECESSARILY

How do we find the remaining logic and semantic errors?

Page 24: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 24

Perfect Code? Finding Errors 3 When you run your executable program

it may not complete (may or may not generate error message)

It may complete and give the wrong answer This usually means

You have made an error translating your algorithm to the computer language

You have made an error in the design of your algorithm

Page 25: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 25

Loading/Running your programEditor, Enter

program

Source File

(Text)

Compile

Find Syntax and some

SemanticErrors

Object File

(binary)

CorrectErrors

LinkerResolves

References Other

Object FilesExecutable File

(binary)

Link Errors Reported:

LoaderCopies

ExecutableAnd Runs

Input data

output resultsCPU

Page 26: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 26

Summary:Executing a Computer Program

Compiler Converts a source file (containing your human

readable program in C++) to and object file (computer readable binary file)

Linker Converts object program to executable program

Compile Link/load ExecuteC languageProgram inText file(source file)

MachinelanguageProgram In binary file(object file)

Program output

Input data

Otherobject files

Page 27: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 27

Summary: Types of Errors Syntax errors

Errors in syntax, how words are combined and used reported by the compiler

Semantic errors Errors in the meaning of words,

Reported by the linker (linker errors) Reported by the compiler (compile time errors) Found at execution time (run-time errors)

Logic errors Errors causing incorrect results, not reported Errors causing program failure (run-time errors)

Page 28: CMPT 128 Introduction to Computing Science for Engineering Students

Janice Regan CMPT 128 2013 28

Processing a C++ Program

Debugged Executable Code

Run time errors

Editor, Enter C program

Source File

(Text)

Compile

Find Syntax and some

SemanticErrors

Object File

(binary)

CorrectErrors

LinkerResolves

References Other

Object FilesExecutable File

(binary)

Link Errors Reported: LoaderCopies

ExecutableAnd Runs

Outputcorrect results

Input data

CPU