Computers
and
Programming
Definition of a Computer
A machine that given instructions and can
manipulate data by itself.
Basic Computer Needs :
o Need to perform calculations faster and more
accurately
o Need to control processes consistently
o Need to handle larger and larger amounts of
data
Computer System
A computer system contains two parts
hardware: actual pieces of equipment
keyboards, screens, components inside the boxes,
printers, modems, etc.
software: instructions that direct hardware
operating systems, compilers, applications
Hardware
Access to data
Read: fetching information from somewhere
Write: putting information somewhere
Common hardware components
Central Processing Unit (CPU)
Main Memory
Secondary Storage
Input & Output (I/O)
CPU
CPU is the heart of any computer
Three functions
control: CPU takes on instruction at a time and
follows it to direct the rest of the system
arithmetic operations: addition, subtraction,
multiplication, division
logical operations: comparisons
Main Memory
Main memory is a temporary, working
storage area
Stores two types of things
current set of instructions the CPU is following
data these instructions manipulate
Made up locations with unique addresses
Addresses allow random access of data
Very fast, but expensive and volatile
Secondary Storage
Permanent storage device
Disks, hard and floppy, are common examples
Relatively cheap, but slow (can be hundreds
of times slower than Main Memory)
Accessed in blocks (many characters at a
time)
I/O Devices
Provide a way for humans to communicate
with computers
Common input devices
Keyboard, mouse, scanner, etc.
Common output devices
Monitor, printer
Software
Legal definition of computer software as used
in the U.S Copyright Act of 1980
“a set of statements or instructions to be used
directly or indirectly in a computer in order to
bring about a certain result.”
A set of instructions is commonly called a
program
Software Categories
Software
Software
System
System Application
Application
Software
Software Software
Software
Operating Device Productivity Business Entertainment Education
Operating Device Utilities Productivity Business Entertainment Education
Systems Drivers Utilities Software Software Software Software
Systems Drivers Software Software Software Software
MS-DOS OS/2 Windows Macintosh UNIX
MS-DOS OS/2 Windows Macintosh UNIX
System Software
System software provides services for all of
the users of the computer system
System software has two main objectives:
Deliver hardware’s resources in a relatively
simple manner
Provide compatibility between different
hardware configurations (portability)
Operating System
Usually works
behind the scenes Applications
Applications
Controls I/O,
allocates system
resources, manages
storage space, Operating
OperatingSystem
System
maintains security,
and detects
equipment failures
Hardware
Hardware
Application Software
The software most users are familiar with.
Word processors, spreadsheets, databases,
games, web browsers, etc.
All of the programs you write for this class
will be application software
Data Storage Units
Binary Digit (Bit)
A single symbol in a binary number
The binary number 11001110 has eight bits
Byte
A group of bits (usually eight)
Smallest unit of data usable
Word
Bits a computer can process simultaneously
Computer Languages
CPUs don’t understand natural languages
nor many programming languages
Each computer has some built-in language
to which other languages must be
translated
Three levels of languages:
Machine Language
Assembly Language
High-level Languages
Machine Language
Built-in language of the computer
Native language of the CPU
Expressed in binary form
An instruction might be 0111010110110011
Lowest level of computer languages
You can write highly efficient code
Very difficult to use
Assembly Language
Second level of languages
An assembler translates assembly-
language instructions into machine
language
Really just symbolic machine languages
0111010110110011 becomes ADD
Uses CPU’s instruction set
Code can be very efficient
Takes a long time to write programs
High-Level Languages
Meant to be easy for humans to use
Similar to natural languages
One HLL instructions translates into many
machine-language instructions
Strict vocabulary and syntax essential for the
computer to understand
Not as efficient as low-level languages
Software Development
Write
Write Source
Source
Source
SourceCode
Code File
File
Object
Object
Compile
Compile File
File
Errors
Executable
Executable
Link
Link File
File
Errors
Test
Test
Errors
Modular Design
Large projects can be very difficult to
envision and implement
Breaking these into smaller tasks is called
modular design
Start with a single broad statement and work
in layers towards detailed descriptions of
small parts of the problem
This process is called top-down design
Modular Design
Student
Student
Portal
Portal
Finance
FinanceServices
Services eLibrary
eLibrary E-mail
E-mail LMS
LMS Examination
Examination
CIW306
CIW306 CKM313
CKM313 CCP203
CCP203
Modular Design Advantages
Separate tasks means that different people
(or computers) can work at the same time.
Modules are often reusable.
It is often easier to maintain many small
modules than one large product.
Terminologies
Program – set of instructions.
Programming – process of writing
instructions in understandable language by
computer and programmers.
Programming Language – set of
instructions that can be used to construct a
program.
Programming Language
FORTRAN, BASIC, COBOL, PASCAL, C,
etc
So many? – there are vast differences in
types of input data, calculations needed,
and output required by applications.
FORTRAN – scientific & engineering purposes.
COBOL – business applications
BASIC, PASCAL, C – general programming
languages.
Algorithms
Algorithm – step-by-step sequence of
instructions that describes how to perform a
computation.
Eg: Assume that a program must calculate
the average of three numbers.
Algorithm (processing steps) for the above
problem can be described into:- pseudocode
and flowcharts.
Pseudocode – English like phrases.
Flowcharts – pictorial representation, use
the standard defined symbols.
Pseudocode
No standard rules for constructing
pseudocode – any short English phrase may
be used.
Solution for the problem in pseudocode:
Input the three numbers into the computer
Calculate the average by adding the numbers
and dividing the sum by three
Display the average
Flowchart
SYMBOL NAME DESCRIPTION
Start
Terminal Indicates the beginning
or end of an algorithm
Indicates an input or
Input Input/Output output operation
three
values
Indicates computation or
Process data manipulation
Calculate Connects the flowcharts
the Flow lines symbols and indicates the
average logic flow
Indicates a decision point
Decision in an algorithm
Display
the
average
Indicates an entry to or
Connector exit from another part of
the flowchart
End
Program Translation
Coding an algorithm
Translate the
Select an algorithm algorithm into C
Requirements
(step-by-step language
procedure) (coding)
Source programs must be translated
C language Machine
source Translation language
program program program
Programming Process
Writing a program that performs the given
task is not always enough.
Good programmers should keep in mind
several objectives when writing code.
Often programmers have to make sacrifices
in one objective to reach acceptable levels in
another.
Programming Objectives
Execution efficiency
Will the program execute in a reasonable time?
Programming efficiency
How long will you spend creating the program?
Maintainability
How easy is it to change the program later?
Reusability
Can the code be used elsewhere easily?
Introduction
The ideal in programming – to efficiently
produce readable, error-free programs that
work correctly and can be modified or
changed with minimum testing required for
reverification.
Compile-Time and Run-Time Errors
Detection of errors:
1. Before a program is compiled
2. While the program is being compiled
3. While it is being run
4. After the program has been executed and the
output is being examined
5. Not at all
Compile-Time and Run-Time Errors
Compile-time Errors :- Errors detected by
compiler.
Run-time Errors :– Errors occur while
program is running.
Desk checking :– Method for detecting
errors before a program is compiled.
Program Verification and Testing :–
Method for detecting errors after a program
has been executed.
Syntax and Logic Errors
A syntax error – an error in the structure or
spelling of a statement.
Logic errors – erroneous, unexpected, or
unintentional errors resulting from a flaw in
the program’s logic.
Logic errors more difficult to detect
compared to syntax errors.
Syntax and Logic Errors
Generally, logic errors are include:
No output – caused of omission printf()
statement, or a sequence of statements that
inadvertently bypasses a printf() function call
Unappealing or misaligned output – caused
of printf() statements.
Incorrect numerical results – caused of
incorrect values being assigned, incorrect
arithmetic operations, omission of statements,
improper sequence of statements, or a roundoff
error.
Testing and Debugging
In theory, a comprehensive set of test runs
will reveal all logic errors and ensure that a
program will work correctly for any and all
combinations of input and computed data.
In practice, this requires checking all
possible combinations of statements
execution (for a simple program).
Testing and Debugging
Remember in testing phase:
Although a single test can reveal the presence
of an error, it does not verify the absence of
one.
The fact that one test reveals no errors does
not indicate that there are no errors.
Testing and Debugging
Debugging – the process of isolating,
correcting, and verifying the correction of
a program.
Techniques of debugging;
Desk checking before write and compile
programs – check pseudocode/flowchart
Program tracing
Diagnostic using printf() function calls to
display selected variables or all input data.
Using debugger
Use your ability to debug the errors