Intro. to Computer S..

gorgeousvassalΛογισμικό & κατασκευή λογ/κού

7 Νοε 2013 (πριν από 4 χρόνια και 5 μέρες)

63 εμφανίσεις

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

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

Symbol

Name



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”


n
um

>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

hours

rate

pay

Output

1

hours,
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 <
iostream
>

using namespace
std
;



int

main()

{


//declare the variables


double hours, r
ate
, pay;




//Get user input


cout

<< "
Number
of hours worked:
";


cin

>> hours;




cout

<< "
Amount
paid per hour:
";


cin

>> r
ate
;




//Calculate the pay


pay = hours * r
ate
;




//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