Professional Documents
Culture Documents
2 Computers, Problem Solving: Objectives
2 Computers, Problem Solving: Objectives
2 Computers, Problem Solving: Objectives
1
CPU (Central Processing
Unit)
CU (Control Unit):
Fetches and decodes instructions
ALU (arithmetic logic unit): carries out All data must be brought into main memory
all arithmetic and logical operations before it can be manipulated
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
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)
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
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.)
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
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
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