 # LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.

## Presentation on theme: "LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered."— Presentation transcript:

LECTURE 1 CMSC 201

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

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

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

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

Basic Flowchart Symbols SymbolName Start Symbol End Symbol Input/Output Data Processing Symbol Decision Symbol Flow Control Arrows Start End

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

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

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

Back to the original example Step 3: Line # Variable Declarations hoursratepayOutput 1hours, rate, pay 2 Number of hours worked: 3 40 4 Amount paid per hour: 5 20 6 800 7 The pay is \$800

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)

Basic Computational Model CPU Code/Data Memory Input Output

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

Example: Program in C++ /************************************** C++ Implementation of the algorithm. **************************************/ #include 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)

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

Download ppt "LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered."

Similar presentations