CSC 1051 Data Structures and Algorithms I

Similar documents
CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I

Java I/O and Control Structures

Java I/O and Control Structures Algorithms in everyday life

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Selection and Repetition Revisited

Selection and Repetition

Improved algorithm. Java code. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm

Java code. Updated program. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm. Improved algorithm

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Selection and Repetition Revisited

Control flow, conditionals, boolean expressions, block statements, nested statements. Course website:

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Selection Statements and operators

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Selection and Repetition

Algorithms and Conditionals

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Selection Statements and operators

Java Coding 3. Over & over again!

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Introduction to Arrays

Conditional Statements

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Iterators and File Input

2: Basics of Java Programming

Loops. CSE 114, Computer Science 1 Stony Brook University

Java Bootcamp - Villanova University. CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

COMP 202 Java in one week

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Building Java Programs

COMP 202. Java in one week

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Top-Down Program Development

Basics of Java Programming variables, assignment, and input

Arrays, Part 3: Multidimensional arrays

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Expression statements are formed by adding a semicolon to the end of certain types of expressions.

File I/O and Exceptions

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

CMPT 125: Lecture 4 Conditionals and Loops

Using Classes and Objects

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Example: Monte Carlo Simulation 1

Chapter 17. Iteration The while Statement

Introduction to Software Development (ISD) Week 3

CS 112 Introduction to Programming

Flow of Control of Program Statements CS 112 Introduction to Programming. Basic if Conditional Statement Basic Test: Relational Operators

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Loops / Repetition Statements

Introduction to Computer Science Unit 2. Notes

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

Lectures 3-1, 3-2. Control Structures. Control Structures, Shimon Schocken IDC Herzliya, slide 1

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

CS111: PROGRAMMING LANGUAGE II

Introduction to Computer Science Unit 2. Notes

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

Introduction to Computer Science Unit 2. Exercises

CS 112 Introduction to Programming

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Introduction to the Java Basics: Control Flow Statements

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Repetition, Looping. While Loop

Problem Solving With Loops

Claremont McKenna College Computer Science

Last Class. While loops Infinite loops Loop counters Iterations

Iteration statements - Loops

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

CS111: PROGRAMMING LANGUAGE II

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

Arrays. Eng. Mohammed Abdualal

Chapter Goals. Contents LOOPS

1.3 Conditionals and Loops

Control Structures: if and while A C S L E C T U R E 4

Object Oriented Programming. Java-Lecture 6 - Arrays

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Chapter 3. Selections

Data Representation and Applets

Midterm Examination (MTA)

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Loops. GEEN163 Introduction to Computer Programming

Controls Structure for Repetition

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Data Representation and Applets

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

Transcription:

Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus

Control flow Review Sequence of statements that are actually executed in a program Conditional and Repetition statements: enable us to alter control flow statement 1 true boolean 1 false statement 2 statement 1 statement 3 boolean 2 true statement 2 statement 4 false statement 3 This slide adapted from Doug Clark s course http://www.cs.princeton.edu/courses/archive/spring13/cos126/lectures.php

Control flow: Repetition Sequence of statements that are actually executed in a program Conditional and Repetition statements: enable us to alter control flow statement 1 true boolean 1 false statement 2 statement 1 statement 3 boolean 2 true statement 2 statement 4 false statement 3 This slide adapted from Doug Clark s course http://www.cs.princeton.edu/courses/archive/spring13/cos126/lectures.php

Example Investment problem: You put $10,000 into a bank account that earns 5% interest per year. year interest balance 0 $10,000.00 1 $500.00 $10,500.00 2 $525.00 $11,025.00 3 $551.25 $11576.25 4 $578.81 $12,155.06 How many years does it take for the account balance to be double the original? This example is adapted from Cay Horstmann s Big Java, Early Objects, 5 th edition

The while Statement while ( condition ) statement; If the condition is true, the statement is executed Then the condition is evaluated again, and if it is still true, the statement is executed again The statement is executed repeatedly until the condition becomes false condition evaluated true statement false

Example Print the even numbers from 2 to 100 Algorithm: initialize a counter to 2 while counter <= 100 print counter add 2 to counter Java? à use while statement

Try this: Write a loop to print the powers of 2 less than or equal to 2048, i.e., 2, 4, 8, 16,, 1024, 2048.

The while statement in action int count = 1; while (count <= 3) System.out.println (count);

The while statement in action count 1 Initialize count int count = 1; while (count <= 3) System.out.println (count);

The while statement in action count 1 int count = 1; count <= 3 is true while (count <= 3) System.out.println (count);

The while statement in action int count = 1; while (count <= 3) Print count System.out.println (count); Output: 1 count 1

The while statement in action int count = 1; while (count <= 3) System.out.println (count); Output: 1 count 2 Increment count

The while statement in action count 2 int count = 1; count <= 3 is true while (count <= 3) System.out.println (count); Output: 1

The while statement in action int count = 1; while (count <= 3) Print count System.out.println (count); Output: 1 2 count 2

The while statement in action int count = 1; while (count <= 3) System.out.println (count); Output: 1 2 count 3 Increment count

The while statement in action count 3 int count = 1; count <= 3 is true while (count <= 3) System.out.println (count); Output: 1 2

The while statement in action int count = 1; while (count <= 3) Print count System.out.println (count); Output: 1 2 3 count 3

The while statement in action int count = 1; while (count <= 3) System.out.println (count); Output: 1 2 3 count 4 Increment count

The while statement in action count 4 int count = 1; count <= 3 is false while (count <= 3) System.out.println (count); Output: 1 2 3

The while statement unraveled int count = 1; while (count <= 3) System.out.println(count); Output: 1 2 3 int count = 1; count TEST:(count <= 3)è true 1 System.out.println(count); TEST:(count <= 3)è true System.out.println(count); TEST:(count <= 3)è true System.out.println(count); TEST:(count <= 3)è false EXIT LOOP count 2 count 3 count 4

What s wrong with this code? int count = 1; while (count <= 10) System.out.println (count);

What s wrong with this code? int count = 1; while (count <= 10); System.out.println (count);

If the condition of a while loop is false initially, the statement is never executed System.out.println( input a number >5 ); int num = scan.nextint(); // input validation while (num <= 5) System.out.println ( type a number >5 ); num = scan.nextint(); Therefore, the body of a while loop will execute zero or more times

Example: Input validation System.out.println( Enter lifestyle code ); System.out.println ( 0=bad; 1=ok; 2=super fit ); int lifestyle = scan.nextint(); while (lifestyle < 0 lifestyle > 2) System.out.println ( Please try again ); System.out.println ( 0=bad; 1=ok; 2=super fit ); num = scan.nextint();

What if we want to do a calculation over and over again? Example: Calculating GPA for many students (how many? when do you stop?) Possible scenarios: Keep accepting new inputs (for each student) and calculating and printing corresponding GPA until user quits program (infinite loop). Same, but ask each time whether to keep going. Same, but quit if the user inputs -1 for the credits (signals end) Calculate GPA for exactly 20 students

Example Table of squares and cubes: N N 2 N 3 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125

Example Table of powers: Compute the powers of 2 and the powers of 3 and print a table like this: N 2 N 3 N 0 1 1 1 2 3 2 4 9 3 8 27 4 16 81

Nested loops Example: Investment problem repetition è the repeated action (calculating the number of years it take for investment to double) involves repetition General pattern for algorithms: A nested loop while (condition for repeating action) initialize variables while (condition for reaching goal) calculations print results

Quick Check How many times will the string "Here" be printed? count1 = 1; while (count1 <= 10) count2 = 1; while (count2 < 20) System.out.println ("Here"); count2++; count1++; Copyright 2012 Pearson Education, Inc.

Example: Factoring a positive integer // Finds prime factors of number x. import java.util.scanner; public class PrimeFactors public static void main(string[] args) Scanner scan = new Scanner(System.in); System.out.print("Enter number = "); int x = scan.nextint(); System.out.println( "Prime factors of " + x ); int factor = 2; while (factor <= x) while (x % factor == 0) System.out.println(factor); x /= factor; factor++;

Example: Computing square roots // Newton-Raphson method for computing the square root of number x. import java.util.scanner; public class Sqrt public static void main(string[] args) Scanner scan = new Scanner(System.in); double EPSILON = 0.000000001; // desired precision System.out.print("Enter number = "); double x = scan.nextdouble(); double sqrti = x; // initial estimate System.out.println( "\niteration \t sqrt approximation " ); int iteration = 1; while (Math.abs(sqrti - x/sqrti) > sqrti*epsilon) System.out.println( iteration++ + "\t\t " + sqrti); sqrti = (x/sqrti + sqrti) / 2.0; System.out.println("\n\nSquare root of " + x + " = + sqrti);

Example: Graphics (next week) UPDATE THIS SLIDE import java.awt.*; import javax.swing.japplet; public class ManyBoxes extends JApplet public void paint(graphics page) int red, green, blue; red = green = blue = 0; // black int size = 500; // largest size int count = 10; while (count > 0) size = count * 50; page.setcolor(new Color(red,green,blue)); page.fillrect(0,0,size, size); count --; green += 25;