1 / 15

Lecture 1

Lecture 1. CMSC 201. Overview. Goal: Problem solving and a lgorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered steps to accomplish a task Algorithm Representation - Pseudocode or flowchart. Program Development.

varsha
Download Presentation

Lecture 1

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. Lecture 1 CMSC 201

  2. Overview • Goal: Problem solving and algorithm development. Learn to program in Python. • Algorithm - a set of unambiguous and ordered steps to accomplish a task • Algorithm Representation - Pseudocode or flowchart

  3. Program Development Step 1: Understand the problem (input, process, output) Step 2: Represent algorithm in pseudocode or flowchart Step 3: Desk check the algorithm Step 4: Implement in a programming language (We will use Python, other options include C, C++, Java, etc.) Step 5: Test/Debug your program

  4. Example • Develop an algorithm that will calculate an hourly employee’s weekly pay • When developing algorithms, we have 3 control structures available to us • Sequence (i.e. one line after the other) • Decision making (e.g. using if/else constructs) • Looping (e.g. using while loops) Step 1 : Understand the problem • Input : pay rate and number of hours • Process: pay = rate * hours • Output: pay

  5. Example - Pseudocode • Pseudocode - looks like code, but not a real language Step 2: 1. Variables: hours, rate, pay 2. Display “Number of hours worked: ” 3. Get hours 4. Display “Amount paid per hour: ” 5. Get rate 6. pay = hours * rate 7. Display “The pay is $” , pay • Notice the importance of order and lack of ambiguity

  6. Basic Flowchart Symbols Start End

  7. Example - Flowchart Start Display “Amount paid per hour: ” Display “Number of hours worked: ” Get rate Get hours pay = hours * rate Display “The pay is $”, pay End

  8. Flowcharts – Decision Making example If num > 0 display “Positive” Else (that means 0 or negative) display “Not Positive” num >0? True False Display “positive” Display “Not positive”

  9. Flowcharts – Looping example Keep prompting the user for the number of books as long as the input is non-positive Get books books >= 0? False Display ”Error, enter number of books read: “ Get books True

  10. Back to the original example Step 3:

  11. Stored-Program Computer Step 4: So far, everything has been on paper. How do we get it to run on a computer? 1. Design a computer specific to solving this one problem (Not a good idea, although historically early computers were designed to just solve specific problems) 2. Use a stored-program computer (A more general approach where data/instructions are in memory and the CPU processes the code)

  12. Basic Computational Model Memory Code/Data CPU Input Output

  13. Computer Program • Algorithm development should be independent of the final computer language used to implement the algorithm as an executable program • Algorithm development is the hard part • Example Algorithm Python Program

  14. Example: Program in C++ /************************************** C++ Implementation of the algorithm. **************************************/ #include <iostream> using namespace std; int main() { //declare the variables double hours, rate, pay; //Get user input cout<< "Number of hours worked: "; cin >> hours; cout<< "Amount paid per hour: "; cin >> rate; //Calculate the pay pay = hours * rate ; //Display the result cout << "The pay is $” << pay <<endl; return 0; } Step 4: Let’s write the program in Python (We will learn the Python programming language this semester)

  15. Python Interpreter • Python Interpreter – runs your python code • Can use it in the interactive modeor script mode • Your code is compiled into byte code (.pyc file) • Python Virtual Machine (PVM) runs the byte code

More Related