1 / 9

EMT 101 – Engineering Programming

Week 2. EMT 101 – Engineering Programming. Dr. Farzad Ismail. School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal 14300 Pulau Pinang. Bugs in code. There are three types of bugs (errors)

terah
Download Presentation

EMT 101 – Engineering Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 2 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering UniversitiSains Malaysia NibongTebal 14300 Pulau Pinang

  2. Bugs in code • There are three types of bugs (errors) • Syntax errors – violation of the grammatical rules of the programming language • A compiler would detect syntax errors. • Semantic errors – violation of the ‘meaning’ or ‘action’ of the code – compiler does NOT detect and the code can run • Algorithm errors – most difficult to be detected

  3. Example: Syntax Error • int main () { cout << “Hello world << endl; return 0; } Syntax Error: undeclared identifier “cout” Line 4 of program 1stprog.cpp

  4. Example: Syntax Error include <math> • int main () { int num; float value; double bigNum; bignum = num + value; } Can you detect the error?

  5. Example: Semantic Error • char response; cout << “Please input (y)es or (n)o: ” << endl; cin >> response; while ( (response != ‘y’) || (response!= ‘n’) ) { cout << “Please try again. Enter (y)es or (n)o: ” << endl; } The expression for while is always true regardless of what input you enter!

  6. Discussion on semantic error example(Boolean Algebra) • If user enters ‘y’, the first part of the expression is false but the second part is true -> overall true due to OR. • If user enters ‘n’, the first part is true but second part is false -> true • The program would keep on asking to try again regardless (infinite loop!) • Corrected by (response != ‘y’) && (response!= ‘n’)

  7. Example: Dangling if-else • if (condition 1) if (condition 2) cout << “output: ” << endl; else cout << “neither” << endl; Correct version: if (condition 1) { if (condition 2) cout << “output: ” << endl; } else cout << “neither” << endl;

  8. Example: Algorithm Error • Find TKE double TKE = 0.0; • for (inti=0; i < n, i++) { KE[i] = u[i]*v[i] + v[i]*u[i]; TKE += KE[i]; }

  9. Strategies to detect errors (bugs) • Trace by hand – trace each step of program execution by pen and paper to recreate the output -> ok for small programs but tedious for large complicated ones! • Program tracing – print out statements inserted in to programs at key locations to track the changing of variables to see how program is progressing, step-by-step. Can also used function calls and flags! • System interactive debugger – a software package that allows you to run your program in a step-by-step mode, automatically keeping track of all variables in code as it is executed.

More Related