Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

Similar documents
Design and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

Pre- Defined Func-ons in C++ Review for Midterm #1

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Flow Control in C++ Condi&onals & Loops CS 16: Solving Problems with Computers I Lecture #4

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science, Winter 2018 Lecture #9

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

Chapter 3. More Flow of Control

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science Lecture #7

Flow Control in C++ 1 CS 16: Solving Problems with Computers I Lecture #4

More on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

Unit 7. 'while' Loops

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Introduc)on to Arrays in C++ Review for Midterm #2 CS 16: Solving Problems with Computers I Lecture #12

Welcome to Solving Problems with Computers I

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

Call- by- Reference Func0ons Procedural Abstrac0ons Numerical Conversions CS 16: Solving Problems with Computers I Lecture #9

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

File Input/Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #10

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Dynamic Arrays Makefiles and Mul3ple File Compiles

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

ques4ons? Midterm Projects, etc. Path- Based Sta4c Analysis Sta4c analysis we know Example 11/20/12

Today in CS161. Lecture #7. Learn about. Rewrite our First Program. Create new Graphics Demos. If and else statements. Using if and else statements

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8

Introduc)on to C++ CS 16: Solving Problems with Computers I Lecture #2

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15

Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

CSci 1113 Midterm 1. Name: Student ID:

File I/O CS 16: Solving Problems with Computers I Lecture #9

Structures and Classes CS 16: Solving Problems with Computers I Lecture #15

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

MIPS Assembly: More about Memory and Instructions CS 64: Computer Organization and Design Logic Lecture #7

File I/O and String Manipula3ons CS 16: Solving Problems with Computers I Lecture #11

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Increment and the While. Class 15

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

LAB 4.1 Relational Operators and the if Statement

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 5. Aykut Erdem, Erkut Erdem, Fuat Akal

Lecture 13: Abstract Data Types / Stacks

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Model-Based Testing. (DIT848 / DAT261) Spring Lecture 3 White Box Testing - Coverage

Review for the Final Exam CS 8: Introduction to Computer Science, Winter 2018 Lecture #15

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

COMP 11 Class 17 Outline

Welcome to Computer Organization and Design Logic CS 64: Computer Organization and Design Logic Lecture #1 Winter 2018

More on File I/O Strings in C++ CS 16: Solving Problems with Computers I Lecture #10

Introduction to Programming

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

File I/O in Python CS 8: Introduction to Computer Science Lecture #11

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

5. Control Statements

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

Agenda. Address vs. Value Consider memory to be a single huge array. Review. Pointer Syntax. Pointers 9/9/12

MIPS Instruc,ons CS 64: Computer Organiza,on and Design Logic Lecture #8

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

if Statement Numeric Rela5onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

Welcome to Computer Organization and Design Logic

CIS 130 Exam #2 Review Suggestions

if Statement Numeric Rela7onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

Python Lists 2 CS 8: Introduction to Computer Science Lecture #9

More Course Overview: Models, Tests, Bugs, and Symbols

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Python, Part 2 CS 8: Introduction to Computer Science Lecture #4

CS 106B, Lecture 1 Introduction to C++

Computer Science II Lecture 1 Introduction and Background

MIPS Assembly: Practice Questions for Midterm 1 Saving to and Loading from Memory CS 64: Computer Organization and Design Logic Lecture #6

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

COMP Flow of Control: Branching 2. Yi Hong May 19, 2015

Finding Pi: Applications of Loops, Random Numbers, Booleans CS 8: Introduction to Computer Science, Winter 2018 Lecture #6

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Python Lists and Dictionaries CS 8: Introduction to Computer Science, Winter 2019 Lecture #13

Test Results Schedule Miscellanea Control Structs. Add l Oper s Break Hands on Q & A Conclusion References Files

Transcription:

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8 Ziad Matni Dept. of Computer Science, UCSB

Outline Midterm# 1 Grades Review of key concepts Loop design help Ch. 3.3, 3.4 in the textbook Debugging your code Ch. 5.4, 5.5 in the textbook 5/2/17 Matni, CS16, Sp17 2

Announcements Midterm is graded! Grades online Homework #7 due today Lab #3 was due Monday, 5/1 Lab #4 due today Homework #8 is out Don t forget your TAs and Instructor s office hours!! J 5/2/17 Matni, CS16, Sp17 3

Midterm #1 Results 25 20 Grade Distribu.on for Midterm #1 CS 16, Sp 17 (Matni) 19 19 25 21 Average 86.1 Median 89 Std. Dev. 10.1 Min 53 Max 102 15 10 5 0 6 6 3 2 < 65 65-69.9 70-74.9 75-79.9 80-84.9 85-89.5 90-94.9 > 95 5/2/17 Matni, CS16, Sp17 4

Programming and submit.cs: The Devil is in the Details 5/2/17 Matni, CS16, Sp17 5

A Review of Basic Concepts If- Else vs. Switch- Case If- Else condi.onal branches: Great for variable condi.ons that give you a Boolean Can use any data type Can do more complex branching Switch statement branches: Great for fixed data values that give you a return value i.e. Menu- style Cannot do Boolean on the case! Cannot use anything other than int, char or enum 5/2/17 Matni, CS16, Sp17 6

A Review of Basic Concepts If- Else Condi6onals if (AmountDesc == Not a lot ) { cout << This is a small amount ; p += (amount 50); r = calcinterest(p); } else { cout << This may be enough ; p += amount; r = calcinterest(p 50); } 1. Note the syntax 2. Why is this type of condi.onal NOT applicable to switch/case? 3. Note the coding style 5/2/17 Matni, CS16, Sp17 7

A Review of Basic Concepts If- Else Condi6onals if ( (amt > 0) && (amt <= 10) ) cout << This is between 1 and 10\n ; Why is it ok NOT to have { } here??? else if ( (amt > 10) && (amt <= 20) ) cout << This is between 11 and 20\n ; else { cout << This is outside the range\n ; cout << Enter another number: ; } cin >> num; Why is it NECESSARY to have { } here??? 1. Note the syntax 2. Why is this type of condi.onal NOT applicable to switch/case? 3. Note the coding style 5/2/17 Matni, CS16, Sp17 8

A Review of Basic Concepts Switch- Case Condi6onals int num; cout << "Gimme a number! "; cin >> num; 1. Note the syntax 2. Why does this type of condi.onal apply well to switch/case? 3. Note the coding style switch (num) { case 1: Which one cout << "One\n"; is wrong? break; case 2: { cout << "Two\n"; break; } case 3: cout << "Three\n"; break; default: cout << "Neither One, Two, nor Three\n"; } // end switch 5/2/17 Matni, CS16, Sp17 9

A Review of Basic Concepts while loops int num(1); Is the logic correct in the code? while (num!= 0) { cout << Give me a number, or zero to quit: ; cin >> num; if (num!= 0) cout << Number x 10 = << num * 10 << endl; } else cout << Quitting!\n ; 1. Note the syntax 2. Why does this type of loop apply well to while loops? 3. Note the coding style 5/2/17 Matni, CS16, Sp17 10

int num = 25; A Review of Basic Concepts for loops for (int j = 1; j <= 10; j++) { } cout << Loop run no. << j << endl; if ((num 2*j) < 10) cout << Condition 1 exists\n ; else cout <<...nothing... ; Is the logic correct in the code? 1. Note the syntax 2. Why does this type of loop apply well to for loops? 3. Note the coding style 5/2/17 Matni, CS16, Sp17 11

In- Class Exercise A Review of Basic Concepts for loops int num1, num2, flag2(1), flag3(1); cout << "Enter start, end numbers: "; cin >> num1 >> num2; for (int j = num1; j <= num2; j++) { cout << "The number " << j << endl; What does this code do? 1. Note the syntax 2. Why does this type of loop apply well to for loops? 3. Why is it beher to use if/then here vs. switch? 4. Note the coding style if (j % 2 == 0) cout << "Divisible by 2\n"; else flag2 = 0; if (j % 3 == 0) cout << "Divisible by 3\n"; else flag3 = 0; if (!(flag2 flag3)) cout << "Not divisible by either 2 or 3\n"; Why does this Boolean expression work? } flag2 = 1; flag3 = 1; cout << "- - - - - " << endl; 5/2/17 Matni, CS16, Sp17 12

5/2/17 Matni, CS16, Sp17 13

Designing Loops What do I need to know? What am I doing inside the loop? What are my ini.alizing statements? What are the condi.ons for ending the loop? 5/2/17 Matni, CS16, Sp17 14

Exit on Flag Condi_on Loops can be ended when a par_cular flag condi_on exists Applies to while and do- while loops Flag: A variable that changes value to indicate that some event has taken place Examples of exit on a flag condi_on for input List ended with a sen_nel value Running out of input 5/2/17 Matni, CS16, Sp17 15

Exit on Flag Example Consider this loop to iden_fy a student with a grade of 90 or beaer and think of how it s logically limited. int n = 1; //student ID number grade = compute_grade(n); // compute_grade() is a function while (grade < 90) { grade = compute_grade(n); cout << "Student number " << n << " has a score of " << grade << endl; n++; } 5/2/17 Matni, CS16, Sp17 16

The Problem The loop on the previous slide might not stop at the end of the list of students if no student has a grade of 90 or higher It is a good idea to use a second flag to ensure that there are s_ll students to consider The code on the following slide shows a beaer solu_on 5/2/17 Matni, CS16, Sp17 17

Exit on Flag Example int n = 1; //student ID number grade = compute_grade(n); // compute_grade() is a function while ((grade < 90) && ( n < number_of_students)) { grade = compute_grade(n); cout << "Student number " << n << " has a score of " << grade << endl; n++; } 5/2/17 Matni, CS16, Sp17 18

Debugging Loops Common errors involving loops include: Off- by- one errors in which the loop executes one too many or one too few _mes Infinite loops usually result from a mistake in the Boolean expression that controls the loop 5/2/17 Matni, CS16, Sp17 19

Fixing Off By One Errors Check your comparison: should it be < or <=? Check that the ini_aliza_on uses the correct value Does the loop handle the zero itera_ons case? 5/2/17 Matni, CS16, Sp17 20

Fixing Infinite Loops Check the direc_on of inequali_es: < or >? Test for < or > rather than equality (==) 5/2/17 Matni, CS16, Sp17 21

More Loop Debugging Tips: Tracing Be sure that the mistake is really in the loop Trace the variable to observe how it changes Tracing a variable is watching its value change during execu_on. Best way to do this is to insert cout statements to have the program show you the variable at every itera_on of the loop. 5/2/17 Matni, CS16, Sp17 22

Debugging Example The following code is supposed to conclude with the variable product equal to the product of the numbers 2 through 5 i.e. 2 x 3 x 4 x 5, which, of course, is 120. What could go wrong?! J int next = 2, product = 1; while (next < 5) { next++; product = product * next; } DEMO! Using variable tracing 5/2/17 Matni, CS16, Sp17 23

Loop Tes_ng Guidelines Every _me a program is changed, it should be retested Changing one part may require a change to another Every loop should at least be tested using input to cause: Zero itera_ons of the loop body One itera_on of the loop body One less than the maximum number of itera_ons The maximum number of itera_ons 5/2/17 Matni, CS16, Sp17 24

Star_ng Over Some_mes it is more efficient to throw out a buggy program and start over! The new program will be easier to read The new program is less likely to be as buggy You may develop a working program faster than if you work to repair the bad code The lessons learned in the buggy code will help you design a beaer program faster 5/2/17 Matni, CS16, Sp17 25

5/2/17 Matni, CS16, Sp17 26

Tes_ng and Debugging Func.ons Each func_on should be tested as a separate unit Tes_ng individual func_ons facilitates finding mistakes Driver Programs allow tes_ng of individual func_ons Once a func_on is tested, it can be used in the driver program to test other func_ons

Example of a Driver Test Program 5/2/17 Matni, CS16, Fa16 28

To Dos Homework #8 for Thursday Lab #5 5/2/17 Matni, CS16, Sp17 29

5/2/17 Matni, CS16, Sp17 30