1 / 24

CSC 1051 – Algorithms and Data Structures I

CSC 1051 – Algorithms and Data Structures I. Algorithms. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051 /f13/

doane
Download Presentation

CSC 1051 – Algorithms and Data Structures I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 1051 – Algorithms and Data Structures I Algorithms 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. CSC 1051 M.A. Papalaskari, Villanova University

  2. Algorithms in everyday life Source: http://xkcd.com/627/ CSC 1051 M.A. Papalaskari, Villanova University

  3. 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: • Input qp • Input credits • gpa = qp / credits • Print gpa CSC 1051 M.A. Papalaskari, Villanova University

  4. 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.htmland Wikipedia (http://en.wikipedia.org/wiki/Mu%E1%B8%A5ammad_ibn_M%C5%ABs%C4%81_al-Khw%C4%81rizm%C4%AB ) CSC 1051 M.A. Papalaskari, Villanova University

  5. //*************************************************************//************************************************************* // GPA.java Author: Joyce/Papalaskari// Demonstrates the use of Scanner input and simple computation.//*************************************************************importjava.util.Scanner;publicclass GPA{publicstaticvoid main (String[] args)//------------------------------------------------------------// Inputs the quality points and credits and calculates GPA.//------------------------------------------------------------{doubleqp, credits, gpa; Scanner scan = new Scanner(System.in);// get inputSystem.out.print ("Enter Quality Points > ");qp = scan.nextInt();System.out.print ("Enter Credits > "); credits = scan.nextInt();// output information enteredSystem.out.println ("\nQuality Points: " + qp);System.out.println ("Credits: " + credits);// calculate and output GPAgpa = qp / credits;System.out.println ("\n\tGPA: " + gpa); }} Java Program  • variables: qp, credits, gpa • Algorithm: • Input qp • Input credits • gpa = qp / credits • Print gpa CSC 1051 M.A. Papalaskari, Villanova University

  6. Pseudocode: a way to describe what an algorithm does without writing a program. • variables: qp, credits, gpa • Algorithm: • Input qp • Input credits • gpa = qp / credits • Print gpa CSC 1051 M.A. Papalaskari, Villanova University

  7. 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: • Input qp • Input credits • gpa = qp / credits • Print gpa CSC 1051 M.A. Papalaskari, Villanova University

  8. 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(use floating point) • Algorithm: • Input qp • Input credits • gpa = qp / credits (Note: use floating point division) • Print gpa When the type is not obvious you can add a note. CSC 1051 M.A. Papalaskari, Villanova University

  9. 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.) CSC 1051 M.A. Papalaskari, Villanova University

  10. 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. CSC 1051 M.A. Papalaskari, Villanova University

  11. Algorithm for PP 2.9 CSC 1051 M.A. Papalaskari, Villanova University

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

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

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

  15. 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 CSC 1051 M.A. Papalaskari, Villanova University

  16. //*************************************************************//************************************************************* // GPA.java Author: Joyce/Papalaskari// Demonstrates the use of Scanner input and simple computation.//*************************************************************importjava.util.Scanner;publicclass GPA{publicstaticvoid main (String[] args)//------------------------------------------------------------// Inputs the quality points and credits and calculates GPA.//------------------------------------------------------------{doubleqp, credits, gpa; Scanner scan = new Scanner(System.in);// get inputSystem.out.print ("Enter Quality Points > ");qp = scan.nextInt();System.out.print ("Enter Credits > "); credits = scan.nextInt();// output information enteredSystem.out.println ("\nQuality Points: " + qp);System.out.println ("Credits: " + credits);// calculate and output GPAgpa = qp / credits;System.out.println ("\n\tGPA: " + gpa); }} Previous Example Java Program  • variables: qp, credits, gpa • Algorithm: • Input qp • Input credits • gpa = qp / credits • Print gpa What if credits = 0 ???? CSC 1051 M.A. Papalaskari, Villanova University

  17. Updated Algorithm • variables: qp, credits, gpa • Algorithm: • Input qp • Input credits • if credits = 0 • Print “No gpa yet” • else • gpa = qp / credits • Print gpa • Printgpagoodbye message CSC 1051 M.A. Papalaskari, Villanova University

  18. variables: qp, credits, gpa • Algorithm: • Input qp • Input credits • if credits = 0 • Print “No gpa yet” • else • gpa = qp / credits • Print gpa • Printgpagoodbye message Java code if (credits == 0) System.out.println (“\n\tGPA: None"); else { gpa= qp / credits; System.out.println (“\n\tGPA: " + gpa); } CSC 1051 M.A. Papalaskari, Villanova University

  19. //*************************************************************// GPA_updated.java Author: Joyce/Papalaskari//// Demonstrates the use of conditional statements.//*************************************************************importjava.util.Scanner;publicclassGPA_Updated{publicstaticvoid main (String[] args)//----------------------------------------------------------// Reads the quality points and credits and calculates GPA.//---------------------------------------------------------- {doubleqp, credits, gpa; Scanner scan = new Scanner(System.in);// get inputSystem.out.print ("Enter Quality Points > ");qp = scan.nextInt();System.out.print ("Enter Credits > "); credits = scan.nextInt();// output information enteredSystem.out.println ("\nQuality Points: " + qp);System.out.println ("Credits: " + credits);// calculate and output GPA, if possibleif (credits == 0)System.out.println ("\n\tGPA: None");else {gpa = qp / credits;System.out.println ("\n\tGPA: " + gpa); }// Print goodbye messageSystem.out.println ("Goodbye and thank you for using my GPA calculator."); }} Updated program  • variables: qp, credits, gpa • Algorithm: • Input qp • Input credits • if credits = 0 • Print “No gpa yet” • else • gpa = qp / credits • Print gpa • Printgoodbye message CSC 1051 M.A. Papalaskari, Villanova University

  20. Conditional statementsalter the linear flow of control. They use boolean expressions to determine what to do next.Example: if (credits == 0) System.out.println("GPA: None"); else {gpa = qp / credits;System.out.println ("\n\tGPA: " + gpa); } A boolean expression CSC 1051 M.A. Papalaskari, Villanova University

  21. Control flow • Sequence of statements that are actually executed in a program • Conditional and Repetition statements: enable us to alter control flow true statement 1 false statement 1 statement 2 boolean 1 boolean 2 true statement 3 statement 2 false statement 4 statement 3 This slide dapted from Doug Clark’s course http://www.cs.princeton.edu/courses/archive/spring13/cos126/lectures.php CSC 1051 M.A. Papalaskari, Villanova University

  22. Java relational operators • relational operators can be used with numeric types and producebooleanresults: ==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 (=) CSC 1051 M.A. Papalaskari, Villanova University

  23. Boolean Expressions • The reserved wordstrueandfalseare the only valid values for a boolean type • Example booleandeclarations: booleanaboveAgeLimit = false; booleanusePlural = hours > 1; A boolean expression using a relational operator CSC 1051 M.A. Papalaskari, Villanova University

  24. Example • An ifstatement with its boolean condition: if(hours !=1) System.out.print("s"); • Another way, using a boolean variable: booleanusePlural = hours !=1; if (usePlural) System.out.print("s"); • See alsoAge.java CSC 1051 M.A. Papalaskari, Villanova University

More Related