2 Computers, Problem Solving: Objectives

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Objectives

 To introduce the architecture of a


2 Computers, computer.

Problem Solving  To introduce the notion of a programming


language.
 To explore computational problem-
solving.
1E3
 Read Chapter 1 of the textbook.

2 Computers and Problem Solving 1 2 Computers and Problem Solving 2

Learning in 1E3 Computer Hardware


 Lectures  Central Processing Unit (CPU)
 Primary source of important concepts, learning
activities, …
 Main memory (MM)
 Laboratory sessions  Aka Random Access Memory (RAM)
 Your chance to apply the concepts and practice  Temporary storage of data/programs
programming  Input/output devices
 Textbook
 Supplementary concepts
 Assumed that you read along and grasp stuff that is
 Secondary storage
not covered in lectures/labs.  Store information permanently

2 Computers and Problem Solving 3 2 Computers and Problem Solving 4

1
CPU (Central Processing
Unit)
 CU (Control Unit):
 Fetches and decodes instructions

 Controls flow of information in and out of MM

 Controls operation of internal CPU components

 PC (program counter): points to next


instruction to be executed

2 Computers and Problem Solving 6

CPU (Central Processing


Main Memory
Unit) (continued)
 Directly connected to the CPU
 IR (instruction register): holds  All programs must be loaded into main memory
instruction currently being executed before they can be executed

 ALU (arithmetic logic unit): carries out  All data must be brought into main memory
all arithmetic and logical operations before it can be manipulated

 When computer power is turned off, everything


in main memory is lost

2 Computers and Problem Solving 7 2 Computers and Problem Solving 8

2
Software
 Software: Programs that do specific tasks
 System programs take control of the computer,
such as an operating system
 Application programs perform a specific task
 Word processors
 Spreadsheets
 Games

2 Computers and Problem Solving 10

Programs Programs cont.


 CPU executes programs  We want to be able to write “higher-level”
 Sequences of instructions instructions like
 CPU understands a small set of instructions  area = height * length;
 MOVE 1002 R1
 Move the value in memory location 1002 to register R1
 And get a compiler to turn it into machine
 MULT R1 R2 code.
 Multiply the contents of R1 and R2  See example in lecture.
 And these assembly language instructions are
coded in binary machine code
 00110110 01101101

2 Computers and Problem Solving 11 2 Computers and Problem Solving 12

3
A C++ Program Processing a Program
#include <iostream>
using namespace std;
 To execute a program written in a high-level
int main() language such as C++
{
cout << "My first C++ program." << endl;  Use an editor to create a source program
cout << "The sum of 2 and 3 = " << 5 << endl; in C++
cout << "7 + 8 = " << 7 + 8 << endl;
return 0;  Use the compiler to
}
 Check that the program obeys the rules
Sample Run:
My first C++ program.  Translate into machine language
The sum of 2 and 3 = 5
7 + 8 = 15
(object program)

2 Computers and Problem Solving 13 2 Computers and Problem Solving 14

Processing a Program
(continued)
Use SubEthaEdit
 Linker: myprog.cpp
Combines object program with other programs to Type into Terminal
create executable code window:

 Loader:
make myprog
 Loads executable program into main memory

 The last step is to execute the program


./myprog

2 Computers and Problem Solving 15

4
This model
allows a prog.cpp
program to
work with
data
previously
make prog
stored in
memory.
prog
(We haven’t
seen such a ./prog
program yet.)

2 Computers and Problem Solving 17 2 Computers and Problem Solving 18

Problem Solving Problem Solving Process


 Programming is a process of problem solving  Step 1 - Analyze the problem
 Problem solving techniques  Outline the problem and its requirements
 Analyze the problem  Design steps (algorithm) to solve the problem
 Outline the problem requirements  Step 2 - Implement the algorithm
 Design steps (algorithm) to solve the problem  Implement the algorithm in code
 Verify that the algorithm works
 Algorithm:
 Step-by-step problem-solving process
 Step 3 - Maintenance
 Use and modify the program if the problem
 Solution achieved in finite amount of time
domain changes

2 Computers and Problem Solving 19 2 Computers and Problem Solving 20

5
Analyze the Problem
 Thoroughly understand the problem
 Understand problem requirements
make  Does program require user interaction?
 Does program manipulate data?
 What is the output?
 If the problem is complex, divide it into
subproblems
 Analyze each subproblem as above

2 Computers and Problem Solving 22

Design an Algorithm Write the Code


 If problem was broken into subproblems  Once the algorithm is designed and
correctness verified
 Design algorithms for each subproblem
 Write the equivalent code in high-level
 Check the correctness of algorithm language

 Can test using sample data


 Enter the program using text editor
 Some mathematical analysis might be
required

2 Computers and Problem Solving 23 2 Computers and Problem Solving 24

6
Compile and Execute Problem solving
 We’ll be compiling our programs using  We’ll work on some example problems in
“make” which takes care of details. lecture.
 Compiler may find errors in your use of the
rules (syntax) of the language

 The final step is to execute the program


 Logic errors may show up then.

2 Computers and Problem Solving 25 2 Computers and Problem Solving 26

Cm to yds, ft, inches Better-value pizza


 Design an algorithm to convert a number  Design an algorithm to determine which
of centimetres to the equivalent number of pizza supplier is better value.
yards, feet and inches.  Input is price and diameter of two pizzas.
 1 inch = 2.54 cm  Output is “first” if the first pizza’s price
per square inch is less than the second’s.

2 Computers and Problem Solving 27 2 Computers and Problem Solving 28

7
Payroll Next …
 Design an algorithm to compute net weekly pay for  Next day we’ll look at a full C++ program
an employee. and identify all the parts.
 Input is hourly rate, number of hours worked, weekly
tax credit.  This will give an overview
 Output gross pay and net pay.  Allow you to create simple similar programs.
 All hours above 40 are paid an overtime rate which is  We’ll worry about the (sometimes tedious)
50% greater than the normal hourly rate. details later.
 Pay is taxed at 20% for the first 200 euro, 40% for the  We’ll then try variations of the program.
remainder.
 Less the weekly tax credit, assuming their tax amounts to  Complete Chapter 1 before then.
that much.
2 Computers and Problem Solving 29 2 Computers and Problem Solving 30

You might also like