Example: Finding the Mode

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
CS0004: Introduction to Programming Repetition – Do Loops.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions Handling.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
C++ crash course Class 8 statements, sort, flight times program.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
Lesson #5 Repetition and Loops.
Exception Handling C++.
Exceptions.
Chapter 6: Loops.
Java Programming Fifth Edition
Agenda Array accessing Automatic initialization of an array
Chapter 10 – Exception Handling
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
Lesson #5 Repetition and Loops.
Testing and Debugging.
Think What will be the output?
Week 4 – Chapter 3 Repetition.
While Loops in Python.
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Iterations Programming Condition Controlled Loops (WHILE Loop)
Chapter 14: Exception Handling
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Control Structures - Repetition
Arrays An Array is an ordered collection of variables
Introduction to Python
Topics Introduction to File Input and Output
Lesson #5 Repetition and Loops.
Exception Handling.
Exception Handling and Reading / Writing Files
TRY CATCH BLOCK By Kosala Rajapaksha.
Exception Handling.
Java Programming Loops
Fundamental Error Handling
Part B – Structured Exception Handling
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Java Programming Control Structures Part 1
Module 4 Loops.
Exception Handling.
A LESSON IN LOOPING What is a loop?
CHAPTER 6: Control Flow Tools (for and while loops)
Java Programming Loops
Lesson #5 Repetition and Loops.
Introduction to Programming
Indefinite loop variations
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
Topics Introduction to File Input and Output
Chapter 11: Exception Handling
While Loops in Python.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Example: Finding the Mode Given: A sequence of integers Goal: Determine the mod of the set The number which appears the most number of times

Example: Finding the Mode Algorithm: Generate an array of 100 random numbers from 1 to 10 Use a length 10 array to count how many times each number appears in the array Report the one that appears the most, or multiple if there are ties

exceptions Motivation: it would be nice to “monitor” the flow of a program for certain bugs Invalid input Invalid array access Type mismatches Recall the example of trying to validate a user input, ensuring that they enter a positive integer Requires slightly confusing loop logic to manage each case Use Scanner.hasNextInt(), etc.

Exceptions Example: int x; x = “1”; // “throws” a NumberFormatException int[] myArray = new int[5]; myArray[5] = 1; // “throws” a ArrayIndexOutOfBoundsException

exceptions If a program raises an exception, it crashes! … unless we “catch” it! Syntax: the “try…catch” block

Try ... Catch Syntax: try { <statements-1> } catch ( <exception-class-name> <variable-name> ) { <statements-2> } We can also chain more “catch” blocks for different types of exceptions

Try…catch Behavior: if the code inside the “try” block raises an exception that matches an exception in the “catch” block, it will immediately stop executing the “try” and start executing the “catch” block int[] myArray = new int[100]; try { myArray[100] = 1; } catch ( ArrayIndexOutOfBoundsException e ){ System.out.println(“You accessed an invalid element!”); }

Example: Input Validation Ask the user for an array of integers, validate each Print the average of the array