Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal 14300 Pulau Pinang Week 2.

Similar presentations


Presentation on theme: "1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal 14300 Pulau Pinang Week 2."— Presentation transcript:

1 1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal 14300 Pulau Pinang Week 2

2 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 3 Example: Syntax Error int main () { cout << “Hello world << endl; cout << “Hello world << endl; return 0; return 0; } Syntax Error: undeclared identifier “cout” Line 4 of program 1stprog.cpp

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

5 5 Example: Semantic Error char response; cout << “Please input (y)es or (n)o: ” << endl; cout << “Please input (y)es or (n)o: ” << endl; cin >> response; cin >> response; while ( (response != ‘y’) || (response!= ‘n’) ) while ( (response != ‘y’) || (response!= ‘n’) ) { cout << “Please try again. Enter (y)es or (n)o: ” << endl; 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 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 7 Example: Dangling if-else if (condition 1) if (condition 2) if (condition 2) cout << “output: ” << endl; cout << “output: ” << endl; else else cout << “neither” << endl; cout << “neither” << endl; Correct version: if (condition 1) { if (condition 2) { if (condition 2) cout << “output: ” << endl; cout << “output: ” << endl; } else else cout << “neither” << endl; cout << “neither” << endl;

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

9 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.


Download ppt "1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal 14300 Pulau Pinang Week 2."

Similar presentations


Ads by Google