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

Slides:



Advertisements
Similar presentations
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Advertisements

LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 3.
CMPUT 101 Lab # 5 October 22, :00 – 17:00.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 Lecture 15 Chapter 6 Looping Dale/Weems/Headington.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Computer Science 1620 Programming & Problem Solving.
The If/Else Statement, Boolean Flags, and Menus Page 180
CS 117 Section 2 + KNET Computer accounts – ed to KNET students –Change password Homework 1 Lab Tutors –In lab for next 2 weeks –Will help you with.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Debugging Logic Errors CPS120 Introduction to Computer Science Lecture 6.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
CH Programming An introduction to programming concepts.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 1.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 9.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 4.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 9.
Looping and Counting Lecture 3 Hartmut Kaiser
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
Syntax and Semantics, and the Program Development Process ROBERT REAVES.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
1 Original Source : and Problem and Problem Solving.ppt.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Control Structures RepetitionorIterationorLooping Part I.
Debugging Logic Errors CPS120 Introduction to Computer Science.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 6.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language:
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Chapter 6 Looping. 2 l A loop is a repetition control structure. l it causes a single statement or block to be executed repeatedly What is a loop?
Software Engineering Algorithms, Compilers, & Lifecycle.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Basic concepts of C++ Presented by Prof. Satyajit De
EMT 101 – Engineering Programming
Chapter 2 Assignment and Interactive Input
Repetition Statements
Counting Loops.
Repetition Control Structure
Fundamental Programming
Repetition Statements (Loops) - 2
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Presentation transcript:

1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 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 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 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 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 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) 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 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 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.