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

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

Algorithms and Conditionals

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

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

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

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

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

Java I/O and Control Structures

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

Java I/O and Control Structures Algorithms in everyday life

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

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I

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

Conditional Statements

CSC 1051 Data Structures and Algorithms I

Selection and Repetition Revisited

2: Basics of Java Programming

Selection Statements and operators

Selection and Repetition

Selection and Repetition Revisited

Selection Statements and operators

Selection and Repetition

Introduction to Arrays

Lecture 3: Variables and assignment

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

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

Iterators and File Input

Basics of Java Programming variables, assignment, and input

Introduction to Computer Science Unit 2. Exercises

Arrays, Part 3: Multidimensional arrays

Using Classes and Objects

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

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

CSE 8A Lecture 12. Reading for next class: : Sounds! Today s topics: Logical Operators:! && Start PSA 6: Chromakey!

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

Java Coding 3. Over & over again!

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

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

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

CS111: PROGRAMMING LANGUAGE II

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

File I/O and Exceptions

COMP 202 Java in one week

COMP 202. Java in one week

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

Fundamentals of Programming Data Types & Methods

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

Data Representation Classes, and the Java API

Arrays - Review. Initializer Lists. The for-each Loop. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

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

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Chapter 3. Selections

Oct Decision Structures cont d

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

H212 Introduction to Software Systems Honors

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

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

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

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

Name: Checked: Preparation: Response time experiment accessing the system clock

Lab 6. Name: Checked:

Applets and the Graphics class

Controls Structure for Repetition

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Data Representation and Applets

Name: Checked: Preparation: Response time experiment accessing the system clock

Programming with Java

Supplementary Test 1

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

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

COMP 202 Java in one week

Tutorial 03. Exercise 1: CSC111 Computer Programming I

AP COMPUTER SCIENCE A

Practice Midterm 1 Answer Key

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

COMP-202 Unit 4: Programming with Iterations

Loops. CSE 114, Computer Science 1 Stony Brook University

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

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

Arrays. Eng. Mohammed Abdualal

Mr. Monroe s Guide to Mastering Java Syntax

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

CS111: PROGRAMMING LANGUAGE II

Using Classes and Objects

CompSci 125 Lecture 11

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

Question 1 [20 points]

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

Bjarne Stroustrup. creator of C++

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

Introduction to Software Development (ISD) David Weston and Igor Razgon

1. An operation in which an overall value is computed incrementally, often using a loop.

Top-Down Program Development

CSC1590 Java Assignment 2-0. User Defined Classes

download instant at

Transcription:

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

Algorithms in everyday life Source: http://xkcd.com/627/

Algorithm Example Statement of GPA problem: Write a program that inputs the credits and quality points earned and outputs the gpa. variables: qp, credits, gpa Algorithm: 1. Input qp 2. Input credits 3. gpa = qp / credits 4. Print gpa

Algorithms An algorithm is a specific set of instructions for carrying out a procedure or solving a problem, usually with the requirement that the procedure terminate at some point. Specific algorithms sometimes also go by the name method, procedure, or technique. The word "algorithm" is a distortion of al-khwārizmī, a Persian mathematician who wrote an influential treatise about algebraic methods. Sources: http://mathworld.wolfram.com/algorithm.html and Wikipedia ( http://en.wikipedia.org/wiki/mu%e1%b8%a5ammad_ibn_m%c5%abs%c4%81_al-khw%c4%81rizm%c4%ab )

Java Program è // GPA.java Author: Joyce/Papalaskari // Demonstrates the use of Scanner input and s //********************************************* import java.util.scanner; public class GPA { public static void main (String[] args) //------------------------------------------ // Inputs the quality points and credits an //------------------------------------------ { double qp, credits, gpa; variables: qp, credits, gpa Algorithm: 1. Input qp 2. Input credits 3. gpa = qp / credits 4. Print gpa } } Scanner scan = new Scanner(System.in); // get input System.out.print ("Enter Quality Points > qp = scan.nextint(); System.out.print ("Enter Credits > "); credits = scan.nextint(); // output information entered System.out.println ("\nquality Points: " System.out.println ("Credits: " + // calculate and output GPA gpa = qp / credits; System.out.println ("\n\tgpa: " + gpa);

Pseudocode: a way to describe what an algorithm does without writing a program. variables: qp, credits, gpa Algorithm: 1. Input qp 2. Input credits 3. gpa = qp / credits 4. Print gpa

Writing an algorithm in pseudocode List the variables used. List the steps for solving the problem, in order. Try to be brief and unambiguous; use Java expressions only when it is simpler to specify a step in java than in English. variables: qp, credits, gpa Algorithm: 1. Input qp 2. Input credits 3. gpa = qp / credits 4. Print gpa

Writing an algorithm in pseudocode List the variables used. List the steps for solving the problem, in order. Try to be brief and unambiguous; use Java expressions only when it is simpler to specify a step in java than in English. When the type is not obvious you can add a note. variables: qp, credits, gpa (use floating point) Algorithm: 1. Input qp 2. Input credits 3. gpa = qp / credits (Note: use floating point division) 4. Print gpa

Another example: PP 2.8 (textbook, Chapter 2 Programming projects) Write an application that reads values representing a time duration in hours, minutes, and seconds and then prints the equivalent total number of seconds. (For example, 1 hour, 28 minutes, and 42 seconds is equivalent to 5322 seconds.)

Can we reverse this calculation? PP 2.9 (textbook, Chapter 2 Programming projects) Create a version of the previous project that reverses the computation. That is, read a value representing a number of seconds, then print the equivalent amount of time as a combination of hours, minutes, and seconds. (For example, 9999 seconds is equivalent to 2 hours, 46 minutes, and 39 seconds.) The next 3 slides will help us visualize this problem.

Algorithm for PP 2.9

How many of each can you pack in the black box?

How many of each can you pack in the black box?

How many of each can you pack in the black box?

Topic Thread 2.1 Character Strings 2.2 Variables, Assignment 2.3 Data Types, in particular int, double 2.4 Expressions (simple) 2.6 Interactive Programs 5.1 Boolean Expressions 5.2 The if Statement 5.5 The while Statement

Previous Example Java Program è // GPA.java Author: Joyce/Papalaskari // Demonstrates the use of Scanner input and s //********************************************* import java.util.scanner; variables: qp, credits, gpa Algorithm: public class GPA { public static void main (String[] args) //------------------------------------------ // Inputs the quality points and credits an //------------------------------------------ { double qp, credits, gpa; Scanner scan = new Scanner(System.in); 1. Input qp 2. Input credits 3. gpa = qp / credits 4. Print gpa // get input System.out.print ("Enter Quality Points > qp = scan.nextint(); System.out.print ("Enter Credits > "); credits = scan.nextint(); // output information entered System.out.println ("\nquality Points: " System.out.println ("Credits: " + What if credits = 0???? } } // calculate and output GPA gpa = qp / credits; System.out.println ("\n\tgpa: " + gpa);

variables: qp, credits, gpa Algorithm: Updated Algorithm 1. Input qp 2. Input credits 3. if credits = 0 Print No gpa yet else gpa = qp / credits Print gpa 4. Print gpa goodbye message

variables: qp, credits, gpa Algorithm: 1. Input qp 2. Input credits 3. if credits = 0 Print No gpa yet else gpa = qp / credits Print gpa 4. Print gpa goodbye message if (credits == 0) else { } Java code System.out.println ( \n\tgpa: gpa = qp / credits; None"); System.out.println ( \n\tgpa: " + gpa);

Updated program è variables: qp, credits, gpa Algorithm: 1. Input qp 2. Input credits 3. if credits = 0 Print No gpa yet else gpa = qp / credits Print gpa 4. Print goodbye message import java.util.scanner; public class GPA_Updated { public static void main (String[] args) //-------------------------------------------------- // Reads the quality points and credits and calcula //-------------------------------------------------- { double qp, credits, gpa; Scanner scan = new Scanner(System.in); } } // get input System.out.print ("Enter Quality Points > "); qp = scan.nextint(); System.out.print ("Enter Credits > "); credits = scan.nextint(); // output information entered System.out.println ("\nquality Points: " + qp); System.out.println ("Credits: " + credits) // calculate and output GPA, if possible if (credits == 0) System.out.println ("\n\tgpa: None"); else { gpa = qp / credits; System.out.println ("\n\tgpa: " + gpa); } // Print goodbye message System.out.println ("Goodbye and thank you for us

Conditional statements alter the linear flow of control. They use boolean expressions to determine what to do next. Example: A boolean expression if (credits == 0) System.out.println ("GPA: None"); else { } gpa = qp / credits; System.out.println ("\n\tgpa: " + gpa);

Control flow 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 dapted from Doug Clark s course http://www.cs.princeton.edu/courses/archive/spring13/cos126/lectures.php

Java relational operators relational operators can be used with numeric types and produce boolean results: == equal to!= not equal to < less than > greater than <= less than or equal to >= greater than or equal to Note the difference between the equality operator (==) and the assignment operator (=)

Boolean Expressions The reserved words true and false are the only valid values for a boolean type Example boolean declarations: boolean aboveagelimit = false; boolean useplural = hours > 1; A boolean expression using a relational operator

Example An if statement with its boolean condition: if (hours!= 1) System.out.print("s"); Another way, using a boolean variable: boolean useplural = hours!= 1; if (useplural) See also Age.java System.out.print("s");