1a computers, problem solving - trinity college dublin€¦ · 1a computers and problem solving 1...

12
1 1a Computers and Problem Solving 1 1a Computers, Problem Solving 1E3 2 Computers and Problem Solving 2 Objectives To introduce the architecture of a computer. To introduce the notion of a programming language. To explore computational problem- solving. Read Chapter 1 of the textbook.

Upload: others

Post on 26-Jun-2020

26 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1a Computers, Problem Solving - Trinity College Dublin€¦ · 1a Computers and Problem Solving 1 1a Computers, Problem Solving!! 1E3! 2 Computers and Problem Solving 2 Objectives!!

1

1a Computers and Problem Solving 1

1a Computers, Problem Solving!

!1E3!

2 Computers and Problem Solving 2

Objectives!

n  To introduce the architecture of a computer.!

n  To introduce the notion of a programming language.!

n  To explore computational problem-solving.!

n  Read Chapter 1 of the textbook.!

Page 2: 1a Computers, Problem Solving - Trinity College Dublin€¦ · 1a Computers and Problem Solving 1 1a Computers, Problem Solving!! 1E3! 2 Computers and Problem Solving 2 Objectives!!

2

2 Computers and Problem Solving 3

Computer Hardware!

n  Central Processing Unit (CPU)!n  Main memory (MM)!

n  Aka Random Access Memory (RAM)!n  Temporary storage of data/programs!

n  Input/output devices!

n  Secondary storage!n  Store information permanently!

Page 3: 1a Computers, Problem Solving - Trinity College Dublin€¦ · 1a Computers and Problem Solving 1 1a Computers, Problem Solving!! 1E3! 2 Computers and Problem Solving 2 Objectives!!

3

2 Computers and Problem Solving 5

CPU (Central Processing Unit)!

n  CU (Control Unit):!n  Fetches and decodes instructions!

n  Controls flow of information in and out of MM!

n  Controls operation of internal CPU components!

n  PC (program counter): points to next instruction to be executed!

2 Computers and Problem Solving 6

CPU (Central Processing Unit) (continued)!

n  IR (instruction register): holds instruction currently being executed!

n  ALU (arithmetic logic unit): carries out all arithmetic and logical operations!

Page 4: 1a Computers, Problem Solving - Trinity College Dublin€¦ · 1a Computers and Problem Solving 1 1a Computers, Problem Solving!! 1E3! 2 Computers and Problem Solving 2 Objectives!!

4

2 Computers and Problem Solving 7

Main Memory!n  Directly connected to the CPU !

n  All programs must be loaded into main memory before they can be executed!

n  All data must be brought into main memory before it can be manipulated !

n  When computer power is turned off, everything in main memory is lost!

Page 5: 1a Computers, Problem Solving - Trinity College Dublin€¦ · 1a Computers and Problem Solving 1 1a Computers, Problem Solving!! 1E3! 2 Computers and Problem Solving 2 Objectives!!

5

2 Computers and Problem Solving 9

Software!n  Software: Programs that do specific tasks!

n  System programs take control of the computer, such as an operating system!

n  Application programs perform a specific task!n  Word processors!

n  Spreadsheets!

n  Games!

2 Computers and Problem Solving 10

Programs!n  CPU executes programs!

n  Sequences of instructions!n  CPU understands a small set of instructions!

n  MOVE 1002 R1!n  Move the value in memory location 1002 to register R1!

n  MULT R1 R2!n  Multiply the contents of R1 and R2!

n  And these assembly language instructions are coded in binary machine code!n  00110110 !01101101!

Page 6: 1a Computers, Problem Solving - Trinity College Dublin€¦ · 1a Computers and Problem Solving 1 1a Computers, Problem Solving!! 1E3! 2 Computers and Problem Solving 2 Objectives!!

6

2 Computers and Problem Solving 11

Programs cont.!

n  We want to be able to write “higher-level” instructions like!n  area = height * length;!

n  And get a compiler to turn it into machine code.!

n  See example in lecture.!

2 Computers and Problem Solving 12

A C++ Program!#include <iostream> using namespace std; int main() { cout << "My first C++ program." << endl; cout << "The sum of 2 and 3 = " << 5 << endl; cout << "7 + 8 = " << 7 + 8 << endl; return 0; } !!Sample Run: !My first C++ program. The sum of 2 and 3 = 5 7 + 8 = 15

Page 7: 1a Computers, Problem Solving - Trinity College Dublin€¦ · 1a Computers and Problem Solving 1 1a Computers, Problem Solving!! 1E3! 2 Computers and Problem Solving 2 Objectives!!

7

2 Computers and Problem Solving 13

Processing a Program!n  To execute a program written in a high-level

language such as C++

n Use an editor to create a source program in C++

n Use the compiler to

n Check that the program obeys the rules

n Translate into machine language (object program)

2 Computers and Problem Solving 14

Processing a Program (continued)!

n  Linker: !Combines object program with other programs to

create executable code!

n  Loader: !

n Loads executable program into main memory!

n  The last step is to execute the program!

Page 8: 1a Computers, Problem Solving - Trinity College Dublin€¦ · 1a Computers and Problem Solving 1 1a Computers, Problem Solving!! 1E3! 2 Computers and Problem Solving 2 Objectives!!

8

make myprog

./myprog

myprog.cpp Use Smultron

Type into Terminal window:

2 Computers and Problem Solving 16

This model allows a program to work with data previously stored in memory.

(We haven’t seen such a program yet.)

Page 9: 1a Computers, Problem Solving - Trinity College Dublin€¦ · 1a Computers and Problem Solving 1 1a Computers, Problem Solving!! 1E3! 2 Computers and Problem Solving 2 Objectives!!

9

2 Computers and Problem Solving 17

prog.cpp

prog

make prog

./prog

2 Computers and Problem Solving 18

Problem Solving!n  Programming is a process of problem solving!n  Problem solving techniques!

n  Analyze the problem !n  Outline the problem requirements!n  Design steps (algorithm) to solve the problem!

n  Algorithm: !n  Step-by-step problem-solving process!n  Solution achieved in finite amount of time!

Page 10: 1a Computers, Problem Solving - Trinity College Dublin€¦ · 1a Computers and Problem Solving 1 1a Computers, Problem Solving!! 1E3! 2 Computers and Problem Solving 2 Objectives!!

10

2 Computers and Problem Solving 19

Problem Solving Process!

n  Step 1 - Analyze the problem!n  Outline the problem and its requirements!n  Design steps (algorithm) to solve the problem!

n  Step 2 - Implement the algorithm!n  Implement the algorithm in code!n  Verify that the algorithm works!

n  Step 3 - Maintenance!n  Use and modify the program if the problem

domain changes!

make

Page 11: 1a Computers, Problem Solving - Trinity College Dublin€¦ · 1a Computers and Problem Solving 1 1a Computers, Problem Solving!! 1E3! 2 Computers and Problem Solving 2 Objectives!!

11

2 Computers and Problem Solving 21

Analyze the Problem!

n  Thoroughly understand the problem!n  Understand problem requirements !

n  Does program require user interaction?!n  Does program manipulate data? !n  What is the output?!

n  If the problem is complex, divide it into subproblems!n  Analyze each subproblem as above!

2 Computers and Problem Solving 22

Design an Algorithm!n  If problem was broken into subproblems!

n  Design algorithms for each subproblem!

n  Check the correctness of algorithm!

n  Can test using sample data!

n  Some mathematical analysis might be required!

Page 12: 1a Computers, Problem Solving - Trinity College Dublin€¦ · 1a Computers and Problem Solving 1 1a Computers, Problem Solving!! 1E3! 2 Computers and Problem Solving 2 Objectives!!

12

2 Computers and Problem Solving 23

Write the Code!

n  Once the algorithm is designed and correctness verified!

n  Write the equivalent code in high-level language!

n  Enter the program using text editor!

2 Computers and Problem Solving 24

Compile and Execute!

n  We’ll be compiling our programs using “make” which takes care of details.!

n  Compiler may find errors in your use of the rules (syntax) of the language !

n  The final step is to execute the program!

n  Logic errors may show up then.!