Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "© Janice Regan, CMPT 128, Jan. 2007 0 CMPT 128 Introduction to Computing Science for Engineering Students Creating a program."— Presentation transcript:

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

2 Janice Regan CMPT 128 2013 1 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

3 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 2

4 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 2013 3

5 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 4

6 5 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 CompileLink/loadExecute C language Program in Text file (source file) Machine language Program In binary file (object file) Program output Input data Other object files

7 Janice Regan CMPT 128 2013 6 Preparing your program to run Editor, Type in your program Source File Text saved on hard disk Compiler Translate text into Machine Readable code

8 Janice Regan CMPT 128 2013 7 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

9 Janice Regan CMPT 128 2013 8 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.

10 Janice Regan CMPT 128 2013 9 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)

11 Janice Regan CMPT 128 2013 10 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? ????

12 Janice Regan CMPT 128 2013 11 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

13 Janice Regan CMPT 128 2013 12 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

14 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 13

15 Janice Regan CMPT 128 2013 14 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

16 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 15

17 Janice Regan CMPT 128 2013 16 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

18 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 17

19 Janice Regan CMPT 128 2013 18 Correcting compile time errors Editor, Type in your program Source File Compiler Translation to Machine 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 syntax Or semantic errors Editor, Correct Syntax Errors

20 Janice Regan CMPT 128 2013 19 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

21 Janice Regan CMPT 128 2013 20 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

22 Janice Regan CMPT 128 2013 21 Linking your program Editor, Enter program Source File (Text) Compile Find Syntax and SemanticErrors Object File (binary) Correct Errors Linker Resolves References among object files Other Object Files Libraries … Executable File (load module) (binary) Semantic Errors Reported: Finds words with no defined meaning. Code has linker errors

23 Janice Regan CMPT 128 2013 22 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?

24 Janice Regan CMPT 128 2013 23 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

25 Janice Regan CMPT 128 2013 24 Loading/Running your program Editor, Enter program Source File (Text) Compile Find Syntax and some SemanticErrors Object File (binary) Correct Errors Linker Resolves References Other Object Files Executable File (binary) Link Errors Reported: Loader Copies Executable And Runs Input data output results CPU

26 Janice Regan CMPT 128 2013 25 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 CompileLink/loadExecute C language Program in Text file (source file) Machine language Program In binary file (object file) Program output Input data Other object files

27 Janice Regan CMPT 128 2013 26 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)

28 Janice Regan CMPT 128 2013 27 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) Correct Errors Linker Resolves References Other Object Files Executable File (binary) Link Errors Reported: Loader Copies Executable And Runs Output correct results Input data CPU


Download ppt "© Janice Regan, CMPT 128, Jan. 2007 0 CMPT 128 Introduction to Computing Science for Engineering Students Creating a program."

Similar presentations


Ads by Google