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/ 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 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 )
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 Pseudocode: describe steps of algorithm in simple, unambiguous language 3. gpa = qp / credits 4. Print gpa
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 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);
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 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) 3. gpa = qp / credits (Note: use floating point division) 4. Print gpa
Example: Algorithm for computing total seconds 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. Eg: 1 hour, 28 min, 42 sec is equivalent to 5322 seconds.)
Example: Algorithm for computing hours, minutes, seconds from totalsec 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. Eg: 9999 seconds is equivalent to 2 hours, 46 min, 39 sec
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 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); 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 Updated Algorithm 3. if credits equals 0 Print No gpa yet else gpa = qp / credits Print gpa 4. Print gpa goodbye message
variables: qp, credits, gpa 3. if credits equals 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 3. if credits equals 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