Chapter 1: Introduction
A SHORT INTRODUCTION TO HARDWARE,
SOFTWARE, AND ALGORITHM DEVELOPMENT
Chapter Goals
- In this chapter you will earn:
- About computer hardware, software and programming
- How to write and execute your first Python program
- How to diagnose and fix programming errors
- How to use pseudocode to describe an algorithm
3/12/24
Our First Definition
Algorithm:
- An algorithm is a step by step description of how to solve a problem
3/12/24
Computer Programs
- A computer program tells a computer the sequence of steps needed to
complete a specific task
- The program consists of a very large number of primitive (simple)
instructions
- Computers can carry out a wide range of tasks because they can
execute different programs
- Each program is designed to direct the computer to work on a specific task
Programming:
- The act of designing, implementing, and testing computer programs
3/12/24
Hardware and Software
THE BUILDING BLOCKS THAT MAKE UP A
COMPUTER
3/12/24
Hardware Components
- Hardware consists of the physical elements in a computer system.
- Some very visible examples are the monitor, the mouse, external storage,
and the keyboard.
- The central processing unit (CPU) performs program control and data
processing
- Storage devices include memory (RAM) and secondary storage
- Hard disk
- Flash drives
- CD/DVD drives
- Input / output devices allow the user to interact with the computer
- Mouse, keyboard, printer, screen ...
3/12/24
Simple View of a Computer's Components
Printer
Mouse/Trackpad
Ports
Disk
controller
Secondary storage
Keyboard
CPU
Monitor
Microphone
Memory
Speakers
Network
controller
Internet
3/12/24
The CPU
- The CPU has two components, the control unit and the arithmetic
logic unit
- The control unit directs operation of the processor.
- All computer resources are managed by the control unit.
- It controls communication and co-ordination between input/output devices.
- It reads and interprets instructions and determines the sequence for
processing the data.
- It provides timing and control signals
- The arithmetic logic unit contains the circuitry to perform
calculations and do comparisons.
- It is the workhorse portion of the computer and its job is to do precisely
what the control unit tells it to do.
3/12/24
Storage Types
- There are two types of storage:
- Primary Storage
- Secondary Storage
- Primary storage is composed of memory chips: electronic circuits that
can store data as long as it is provided electric power
- Secondary storage provides a slower, less expensive storage that is
persistent: the data persists without electric power
- Computers store both data and programs
- The data and programs are located in secondary storage and loaded into
memory when the program is executed
3/12/24
Memory
- A simple way to envision primary memory is a table of cells all the
same size, one byte, and each containing a unique address beginning
with 0.
- The "typical" computer has a main memory ranging from 4 gigabytes (GB),
to 32 GB.
- How big is a gigabyte?
- A byte is 8 bits.
- A kilobyte, KB, is 1024 bytes, or "about 1 thousand bytes."
- A megabyte, MB, is 1,048,576 bytes, or "about 1 million bytes."
- A gigabyte, GB, is 1,073,741,824 bytes or "about 1 billion bytes."
3/12/24
Executing a Program
- Program instructions and data (such as text, numbers, audio, or video)
are stored in digital format
- When a program is started, it is brought into memory, where the CPU
can read it.
- The CPU runs the program one instruction at a time.
- The program may react to user input.
- The instructions and user input guide the program execution
- The CPU reads data (including user input), modifies it, and writes it back to
memory, the screen, or secondary storage.
3/12/24
Software Overview
- Software is typically realized as an application program
- Microsoft Word is an example of software
- Computer Games are software
- Operating systems and device drivers are also software
- Software
- Software is a sequence of instructions and decisions implemented in some
language and translated to a form that can be executed or run on the
computer.
- Computers execute very basic instructions in rapid succession
- The basic instructions can be grouped together to perform complex tasks
- Programming is the act of designing and implementing computer
programs
3/12/24
Algorithms
3/12/24
Introduction to Algorithms
- If you want a computer to perform a task, you start by writing an
algorithm
- An Algorithm is:
- a sequence (the order mattering) of actions to take to accomplish the given
task
- An algorithm is like a recipe; it is a set of instructions written in a sequence
that achieves a goal
- For complex problems software developers write an algorithm before
they attempt to write a computer program
- For this class we will ALWAYS write an algorithm for each project
- Developing algorithms is a fundamental problem-solving skill
- It has uses in many fields outside of Computer Science
3/12/24
Algorithm: Formal Definition
An algorithm describes a sequence of steps that is:
1. Unambiguous
a. No "assumptions" are required to execute the algorithm
b. The algorithm uses precise instructions
2. Executable
a. The algorithm can be carried out in practice
3. Terminating
a. The algorithm will eventually come to an end, or halt
3/12/24
Problem Solving: Algorithm Design
- Algorithms are simply plans
- Detailed plans that describe the steps to solve a specific problem
- You already know quite a few
- Calculate the area of a circle
- Find the length of the hypotenuse of a triangle
- Some problems are more complex and require more steps
- Calculate PI to 100 decimal places
- Calculate the trajectory of a missile
3/12/24
Simple Example: Selecting a Car
Problem Statement: Car Selection
- You have the choice of buying two cars.
- One is more fuel efficient than the other, but also more expensive.
- You know the price and fuel efficiency (in miles per gallon, mpg) of
both cars.
- You plan to keep the car for ten years.
- Which car is the better deal?
3/12/24
Developing the Car Selection Algorithm
Determine the inputs and outputs for Car Selection
From the problem statement we know:
· Car 1: Purchase price, Fuel Efficiency
· Car 2: Purchase price, Fuel Efficiency
· Price per gallon = $4.00
· Annual miles driven= 15,000
· Length of time = 10 years
For each car we need to calculate:
· Annual fuel consumed for each car
· Annual fuel cost for each car
· Operating cost for each car
· Total cost of each Car
. Then we select the car with the lowest total cost
3/12/24
Translating the Car Selection Algorithm to pseudocode
- Break down the problem into smaller tasks
- 'Calculate total cost' for each car
- To calculate the total cost for each year we need to calculate the operating cost
- The operating cost depends on the annual fuel cost
- The annual fuel cost is the price per gallon * the annual fuel consumed
- The annual fuel consumed is the annual miles drive / fuel efficiency
- Describe each subtask as pseudocode
- total cost = purchase price + operating cost
3/12/24
The Car Selection Pseudocode
For each Car, compute the total cost
Annual fuel consumed = annual miles driven / fuel efficiency
Annual fuel cost = price per gallon * annual fuel consumed
Operating cost = Length of time * annual fuel cost
Total cost = purchase price + operating cost
If total cost1 < total cost2
Chose Car1
Else
Choose Car2
3/12/24
Bank Account Example
Problem Statement: Bank Account
- Problem Statement:
- You put $10,000 into a bank account that earns 5 percent interest per year.
How many years does it take for the account balance to be double the
original?
- How would you solve it?
- Manual method
- Make a table
- Add lines until done
- Use a spreadsheet!
- Write a formula
- Per line, based on line above
year
balance
0
10000
1
10000.00 x 1.05 = 10500.00
2
10500.00 x 1.05 = 11025.00
3
11025.00 x 1.05 = 11576.25
4
11576.25 × 1.05 = 12155.06
3/12/24
Develop the Bank Account Algorithm Steps
- You put $10,000 into a bank account that earns 5 percent interest per year.
How many years does it take for the account balance to be double the
original?
- Break it into steps
- Start with a year value of 0 and a balance of $10,000
year
balance
0
10000
- Repeat the following while the balance is less than $20,000
- Add 1 to the year value
- Multiply the balance by 1.05
- (5% increase)
year
balance
0
10000
1
10500
- Report the final year value as the answer
14
19799.32
15
20789.28
3/12/24
Translate Bank Account to Pseudocode
- Pseudocode
- Half-way between natural language and a programming language
- Modified Steps
- Set the year value of 0
- Set the balance to $10,000
- While the balance is less than $20,000
- Add 1 to the year value
- Multiply the balance by 1.05
- Report the final year value as the answer
- The pseudocode is easily translated into Python
3/12/24
Python Language
TIM
The Python Language
- In the early 1990' s, Guido van Rossum designed what would become
the Python programming language
- Van Rossum was dissatisfied with the languages available
- They were optimized to write large programs that executed quickly
- He needed a language that could not only be used to create programs
quickly but also make them easy to modify
- It was designed to have a much simpler and cleaner syntax than other
popular languages such as Java, C and C++ (making it easier to learn)
- Python is interpreted, making it easier to develop and test short programs
- Python programs are executed by the Python interpreter
- The interpreter reads your program and executes it
https://www.python.org/downloads/
3/12/24
Programming Environments
- There are several ways of creating a computer program
- Using python console
- Using an Integrated Development Environment (IDE)
- Using a text editor
- You should use the method you are most comfortable with.
- I'll use the Sublime Text Editor for all my in-class examples
S
https://www.sublimetext.com
- We'll run examples with the python console from Sublime Text Editor
3/12/24
Sublime Text Components
- The source code editor can help programming by:
- Listing line numbers of code
- Color lines of code (comments, text ... )
- Auto-indent source code
$
C:\Users\DeathAxe\Desktop\hello_world.py - Sublime Text
X
File Edit Selection Find View Goto Tools Project Preferences Help
hello_world.py
×
1
2
3
def main():
print("hello World")
4
5
if
==
main ":
6
name
main()
7
Line 1, Column 1
Spaces: 4
Python
3/12/24