CSC 1051 M.A. Papalaskari, Villanova University 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 – Algorithms and Data Structures I
24
Embed
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
CSC 1051 M.A. Papalaskari, Villanova University
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.
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 )
// GPA.java Author: Joyce/Papalaskari// Demonstrates the use of Scanner input and simple computation.//*************************************************************import java.util.Scanner;
public class GPA{ public static void main (String[] args) //------------------------------------------------------------ // Inputs the quality points and credits and calculates GPA. //------------------------------------------------------------ { double qp, credits, 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.
CSC 1051 M.A. Papalaskari, Villanova University
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.
CSC 1051 M.A. Papalaskari, Villanova University
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
When the type is not obvious you can add a note.
Another example:
CSC 1051 M.A. Papalaskari, Villanova University
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.)
PP 2.8 (textbook, Chapter 2 Programming projects)
Can we reverse this calculation?
CSC 1051 M.A. Papalaskari, Villanova University
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.
PP 2.9 (textbook, Chapter 2 Programming projects)
Algorithm for PP 2.9
CSC 1051 M.A. Papalaskari, Villanova University
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
// GPA.java Author: Joyce/Papalaskari// Demonstrates the use of Scanner input and simple computation.//*************************************************************import java.util.Scanner;
public class GPA{ public static void main (String[] args) //------------------------------------------------------------ // Inputs the quality points and credits and calculates GPA. //------------------------------------------------------------ { double qp, credits, gpa;
//*************************************************************// GPA_updated.java Author: Joyce/Papalaskari//// Demonstrates the use of conditional statements.//*************************************************************
import java.util.Scanner;
public class GPA_Updated{ public static void main (String[] args) //---------------------------------------------------------- // Reads the quality points and credits and calculates GPA. //---------------------------------------------------------- { double qp, credits, gpa; Scanner scan = new Scanner(System.in);