Intro. to Computer S..

advertisement
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.
2.
3.
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
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
Symbol
Start
Name
Start Symbol
End Symbol
End
Input/Output
Data Processing Symbol
Decision Symbol
Flow Control Arrows
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
Flowcharts – Decision Making example
If num > 0 display “Positive”
Else (that means 0 or negative) display “Not Positive”
True
False
num >0?
Display
“positive”
Display “Not
positive”
Flowcharts – Looping example
Keep prompting the user for the number of
books as long as the input is non-positive
Get books
books
>= 0?
True
False
Display
”Error, enter
number of
books read: “
Get books
Back to the original example
Step 3:
Line #
Variable
hours
rate
pay
Output
Declarations
1
2
3
4
5
6
7
hours, rate, pay
Number of hours
worked:
40
Amount paid per
hour:
20
800
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
Memory
CPU
Code/Data
Output
Input
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 <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)
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