CMPG171: Study Unit 4

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 44

CMPG171

Software development and Structured Programming

Study Unit 4
Faculty of Natural- and Agricultural Sciences

eBook pg. 126-131, 147-151, 165-174, 187-196


Objectives:
• Understand what computer systems do, what a program is
and why we program;
• Describe the evolution of programming languages from
machine language to high-level languages;
• Understand simple program logic and the reasons for
structure;
• Describe the basic features of an algorithm;
• Describe how hardware and software collaborate in a
computer's architecture;
• Describe the steps in interpreting a program;
• Describe the three fundamental structures of structured
programming;
Understanding Computer Systems
• Computer system
• Combination of all the components required to process and store data using a
computer
• Hardware
• Equipment associated with a computer
• Software
• Computer instructions that tells the hardware what to do
• Programs
• Instructions written by programmers
• Programming
• Writing software instructions

Programming Logic and Design Ninth Edition


Understanding Computer Systems
1)
(continued -

• Application software such as word processing, spreadsheets, payroll and


inventory, even games, app
• System software such as operating systems like Windows, Linux, or UNIX,
Google Android and Apple IOS
• Computer hardware and software accomplish three major operations
• Input
• Data items such as text, numbers, images, and sound
• Processing
• Calculations and comparisons performed by the central processing unit (CPU)
Understanding Computer Systems
-2)
(continued

• Output
• Resulting information that is sent to a printer,
a monitor, or storage devices after processing
• A cloud based device is accessed through the Internet
• Programming language
• Used to write computer instructions called program code
• Writing instructions is called coding the program
• Examples
• Visual Basic, C#, C++, or Java
• Syntax
• Rules governing word usage and punctuation
• Mistakes in a language’s usage are syntax errors

5
Understanding Computer Systems
-3)
(continued

• Computer memory
• Computer’s temporary, internal storage – random access memory (RAM)
• Volatile memory – lost when the power is off
• Permanent storage devices
• Nonvolatile memory
• Compiler or interpreter
• Translates source code into machine language (binary language) statements called
object code
• Checks for syntax errors
• Program executes or runs
• Input will be accepted, some processing will occur, and results will be output
• Why programming?
• Writing programs (or programming) is a very creative and rewarding activity. You can write
programs for many reasons ranging from making your living to solving a difficult data analysis
problem to having fun to helping someone else solve a problem.

6
Evolution of Programming language

• To build programs, people use languages that are similar to human


language. The results are translated into machine code (0,1), which
computer understands.

• Programming languages fall into three broad categories:


• Machine languages
• Assembly languages
• Higher-level languages

For more information on eFundi.


Understanding Simple Program
Logic
• Programs with syntax errors cannot execute
• Logical errors
• Errors in program logic produce incorrect output
• Logic of the computer program
• Sequence of specific instructions in specific order
• Variable
• Named memory location whose value can vary
• Example:
Get a bowl
Stir
Add two eggs Don’t Do it
Add a gallon of gasoline Don’t bake a cake like this
Bake at 350 degrees for 45 minutes
Add 3 cups of flour

Programming Logic and Design, Ninth Edition


Basic features of an Algorithm

• Sequence of steps that describes each of these computational


processes is called an algorithm.

• Features of an algorithm:
• Consists of a finite number of instructions
• Each individual instruction is well defined
• Action described by the instruction can be performed effectively or be executed by a
computing agent
• Describes a process that eventually halts after arriving at a solution to a
problem
• Solves a general class of problems
• Computers can be designed to run a small set of algorithms for
performing specialized tasks
Basic features of an Algorithm

• Pseudocode
• English-like representation of the logical steps it takes to solve a problem
• Flowchart
• Pictorial representation of the logical steps it takes to solve a problem
Writing Pseudocode
• Pseudocode representation of a number-doubling problem
start
input myNumber
set myAnswer = myNumber * 2
output myAnswer
stop

Programming Logic and Design, Ninth Edition


Pseudocode Standards
• Programs begin with the word start and end with the word stop;
these two words are always aligned
• Whenever a module name is used, it is followed by a set of
parentheses
• Modules begin with the module name and end with return. The
module name and return are always aligned
• Each program statement performs one action—for example, input,
processing, or output

Programming Logic and Design, Ninth Edition


Pseudocode Standards (continued -1)

• Program statements are indented a few spaces more than the word
start or the module name
• Each program statement appears on a single line if possible. When
this is not possible, continuation lines are indented
• Program statements begin with lowercase letters
• No punctuation is used to end statements

Programming Logic and Design, Ninth Edition


Drawing Flowcharts
• Create a flowchart
• Draw geometric shapes that contain the individual statements
• Connect shapes with arrows
• Input symbol
• Indicates input operation
• Parallelogram
• Processing symbol
• Contains processing statements such as arithmetic
• Rectangle

Programming Logic and Design, Ninth Edition


Drawing Flowcharts (continued -1)

• Output symbol
• Represents output statements
• Parallelogram
• Flowlines
• Arrows that connect steps
• Terminal symbols
• Start/stop symbols
• Shaped like a racetrack
• Also called lozenges

Programming Logic and Design, Ninth Edition


Drawing Flowcharts (continued -2)

Programming Logic and Design, Ninth Edition


Repeating Instructions
• Program in Figure 1-7 only works for one number
• Not feasible to run the program over and over 10,000 times
• Not feasible to add 10,000 lines of code to a program
• Create a loop (repetition of a series of steps) instead
• Avoid an infinite loop (repeating flow of logic that never ends)

Programming Logic and Design, Ninth Edition


Repeating Instructions (continued -1)

Programming Logic and Design, Ninth Edition


Repeating Instructions (continued -2)

Programming Logic and Design, Ninth Edition


The Structure of a Modern Computer System

• A modern computer system consists of hardware and software


• Hardware: physical devices required to execute algorithms
• Software: set of these algorithms, represented as programs in particular
programming languages

© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Computer Hardware (1 of 3)

• Basic hardware components of a computer are:


• Memory
• Central processing unit (CPU)
• Set of input/output devices
• Computers can also communicate with the external world through various
ports that connect them to networks and to other devices

© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Computer Hardware (2 of 3)

• Computer memory is set up to represent and store information in electronic


form
• Stored as patterns of binary digits (1s and 0s)
• Random access memory (RAM) is also called internal or primary
• Part of a computer responsible for processing data is the central processing
unit (CPU), also called processor
• External or secondary memory can be magnetic, semiconductor, or optical

© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Computer Hardware (3 of 3)

© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Computer Software (1 of 3)

• A program stored in computer memory must be represented in binary


digits, or machine code
• A loader takes a set of machine language instructions as input and loads
them into the appropriate memory locations
• The most important example of system software is a computer’s
operating system
• Some important parts: file system, user interfaces (terminal-based, GUIs, or
touchscreen interface)
• Applications include Web browsers, word processors, spreadsheets,
database managers, graphic design packages, games, etc…

© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Computer Software (2 of 3)

• Scientists have developed high-level programming languages for


expressing algorithms
• Resemble English and allow the author to express algorithms in a form that other
people can understand
• Programmers usually start by writing high-level language statements in a
text editor
• Runs another program called a translator to convert program code into executable
code
• Translator checks for syntax errors
• If no errors are found, program can be executed by the run-time system
• Might execute program directly on the hardware or run another program called an
interpreter or virtual machine to execute the program

© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Computer Software (3 of 3)

© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Three fundamentals of Structured
Programming.
• Structured programming (sometimes known as modular programming) is a
type of procedural programming that enforces a logical structure on the
program being written to make it more efficient and easier to understand and
modify. Structured programming have three basic logic structures:

• Sequence: ordered statements or subroutines executed in sequence.


• Selection: one or a number of statements is executed depending on the state of the
program.
• Repetition: a statement or block is executed until the program reaches a certain state,
or operations have been applied to every element of a collection.
Getting Started with Python Programming

• Early 1990s: Guido van Rossum


• invented the Python programming language
• Python is a high-level, general-purpose programming language for solving
problems on modern computer systems
• Useful resources at www.python.org
Running Code in the Interactive Shell (1 of 2)

• Python is an interpreted language


• Simple Python expressions and statements can be run in the shell
• Easiest way to open a Python shell is to launch the I DLE
• To quit, select the window’s close box or press Control+D
• Shell is useful for:
• Experimenting with short expressions or statements
• Consulting the documentation

© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Running Code in the Interactive Shell (2 of 2)

© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Input, Processing, and Output (1 of 5)

• Programs usually accept inputs from a source, process them, and output
results to a destination
• In terminal-based interactive programs, these are the keyboard and terminal display
• In Python, inputs are Python expressions or statements
• Outputs are the results displayed in the shell
• Programmers can also force output of a value by using the print function
• print (<expression>)
• Example:

>>>print (“Hi there”)


Hi there

© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Input, Processing, and Output (2 of 5)

• The following example receives an input string from the user and saves it for further
processing:

>>> name = input(“Enter your name:”)


Enter your name: Ken Lambert
>>> name
‘Ken Lambert’
>>> print(name)
Ken Lambert
>>>

© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Input, Processing, and Output (3 of 5)

• The input function always builds a string from the user’s keystrokes and
returns it to the program
• Strings that represent numbers must be converted from strings to
appropriate number types
• Two type conversion functions: int (for integers) and float (for floating-point numbers)

© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Input, Processing, and Output (4 of 5)

• The next session inputs two integers and displays their sum:

>>> first = int(input(“Enter the first number: ”))


Enter the first number: 23
>>> second = int(input(“Enter the second number:”))
Enter the second number: 44
>>> print(“The sum is”, first + second)
The sum is 67

Fundamentals of Python: First programs second edition


Input, Processing, and Output (5 of 5)

Function What It Does

float(<a string of digits>) Converts a string of digits to a floating-point


value.
int(<a string of digits>) Converts a string of digits to an integer value.

input(<a string prompt>) Displays the string prompt and waits for
keyboard input. Returns the string of characters
entered by the user.
print(<expression>, Evaluates the expressions and displays them,
...,<expression>) separated by one space, in the console window.
<string 1> + <string 2> Glues the two strings together and returns the
result.

Fundamentals of Python: First programs second edition


Editing, Saving, and Running a Script (1 of 3)

• We can then run Python program files or scripts within IDLE or from the OS’s
command prompt
• Run within IDLE using menu option, F5 (Windows), or Control+F5 (Mac or Linux)
• Python program files use .py extension
• Running a script from IDLE allows you to construct some complex programs,
test them, and save them in program libraries to reuse or share with others

© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Editing, Saving, and Running a Script (2 of 3)

© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Editing, Saving, and Running a Script (3 of 3)

Fundamentals of Python: First programs second edition


Behind the Scenes: How Python Works

Fundamentals of Python: First programs second edition


Detecting and Correcting Syntax Errors (1 of 2)

• Programmers inevitably make typographical errors when editing


programs, called syntax errors
• The Python interpreter will usually detect these
• Syntax: rules for forming sentences in a language
• When Python encounters a syntax error in a program, it halts execution
with an error message
• Example:

>>> length = int(input(“Enter the length: ”))


Enter the length: 44
>>> print(lenth)
Traceback (most recent call last):
File “<pyshell#l>”, line 1, in <module>
NameError: name ‘lenth’ is not defined

Fundamentals of Python: First programs second edition


Detecting and Correcting Syntax Errors (2 of 2)

• The next statement attempts to print the value of the correctly spelled
variable:
>>> print(length)
SyntaxError: unexpected indent
• Final example, programmer attempts to add two numbers, but forgets to
include the second one:
>>> 3 +
SyntaxError: invalid syntax

Fundamentals of Python: First programs second edition


Chapter Summary (1 of 3)

• An algorithm is a sequence of steps that describes each of these


computational processes. Two types: Pseudocode and Flowchart.
• Real computing agents can be constructed out of hardware devices
• CPU, memory, and input and output devices
• Some real computers are specialized for a small set of tasks, whereas a
desktop or laptop computer is a general-purpose problem-solving
machine
• Software provides the means whereby different algorithms can be run on
a general-purpose hardware device
• Written in programming languages

Fundamentals of Python: First programs second edition


Chapter Summary (2 of 3)

• Languages such as Python are high-level


• Interpreter translates a Python program to a lower-level form that can be
executed on a real computer
• Python shell provides a command prompt for evaluating and viewing the
results of Python expressions and statements
• IDLE is an integrated development environment that allows the
programmer to save programs in files and load them into a shell for testing
• Python scripts are programs that are saved in files and run from a terminal
command prompt

Fundamentals of Python: First programs second edition


Chapter Summary (3 of 3)

• When a Python program is executed, it is translated into byte code


• Sent to PVM for further interpretation and execution
• Syntax: set of rules for forming correct expressions and statements in a
programming language

Fundamentals of Python: First programs second edition

You might also like