280 likes | 410 Views
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
E N D
CMPT 128Introduction 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 • This course uses the languages C and C++ to implement many ideas that can also be implemented in other high level languages
Algorithm • A sequence of precise instructions that leads to a solution • Order of instructions is important • Amount of detail must be appropriate
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
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
Summary:Running a Computer Program Input data • 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 Machine language Program In binary file (object file) Compile Link/load Execute C language Program in Text file (source file) Program output Other object files
Preparing your program to run Editor, Type in your program Source File Text saved on hard disk Compiler Translate text into Machine Readable code
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
Creating Source files • Create your source fileusing 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.
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)
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? ????
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
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
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
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
Example: semantic error • error C3861: 'setw': identifier not found • Can’t find library containing this function
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
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
Correcting compile time errors Editor, Type in your program Editor, Correct Syntax Errors Source File Code with syntax Or semantic errors Syntax or Semantic Errors, Compiler Generates error Messages. To help us find and Correct errors in the Source File Compiler Translation to Machine code Syntactically Correct code Object File Binary, machine readable file
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
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
Linking your program Editor, Enter program Source File (Text) Correct Errors Other Object Files Libraries … Compile Find Syntax and SemanticErrors Linker Resolves References among object files Object File (binary) Code has linker errors Semantic Errors Reported: Finds words with no defined meaning. Executable File (load module) (binary)
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?
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
Loading/Running your program Editor, Enter program Source File (Text) Correct Errors Compile Find Syntax and some SemanticErrors Object File (binary) output results CPU Input data Link Errors Reported: Loader Copies Executable And Runs Linker Resolves References Executable File (binary) Other Object Files
Summary:Executing a Computer Program Input data • 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 Machine language Program In binary file (object file) Compile Link/load Execute C language Program in Text file (source file) Program output Other object files
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)
Processing a C++ Program Editor, Enter C program Source File (Text) Correct Errors Input data Output correct results Compile Find Syntax and some SemanticErrors Object File (binary) CPU Run time errors Link Errors Reported: Loader Copies Executable And Runs Linker Resolves References Executable File (binary) Other Object Files Debugged Executable Code