CSC425 Topic 1 Introduction
CSC425 Topic 1 Introduction
CSC425 Topic 1 Introduction
Programming (CSC425)
•
Basic Concept
Computer program
From Hanly [2001], it is “list of instructions that
direct the computer to transform information from
one form to another.”
Another name for software
Is a set of detailed, step-by-step instructions
that directs the computer to do what you want
it to do.
Programs are written in a programming
language.
Basic Concept
Computer program
Turn data (input) into useful information
(output).
Program uses variables to store data
Storage
Basic Concept
Programming Language
A set of rules, words and symbols are used
to write a computer program.
Provides a way of telling the computer what
operations to perform.
Programmer is a person who writes programs
by using programming language.
Basic Concept
Lower or higher depending on how close
they are to the language that the computer
uses.
Lower (0s and 1s)
Higher (more English-like)
Generation of Programming language
Machine language
Assembly language
High Level language (Pascal, C, C++, Java etc)
Basic Concept
Machine Language
Binary number codes understood by a specific CPU.
Computer understands only binary numbers, 0s and
1s.
These represent the on and off electrical states of
the computer
Programs that are written in 0s and 1s is actually
machine language
Basic Concept
Assembly Language
Very low-level
Use mnemonic codes, abbreviations to replace 0s and 1s
More easier to understand
A for add, C for compare, MUL for multiply, STO for
storing information in memory
Assembler is a translation program that converts the
assembly language program into machine language
Basic Concept
• A Machine-language Program Fragment and
Its Assembly-Language Equivalent
Syntax
Grammar rules of programming language
Object file
File of machine-language instructions that is the output of a compiler
Algorithm
A precise step-by-step action to perform overall task of the program
Basic Concept
Compilation Process
Input Output
Program Development Process
Computer programming is a process to
develop a computer program
Step 1. Problem analysis
Step 2. Program design
Step 3. Coding
Step 4. Testing
Step 5. Documentation
Program Development Process
Problem Analysis
A process of identifying the output, processes and input of a
program.
How to identify input?
Nouns and adjectives Keywords – accept, enter, receive, read
How to identify output?
Nouns and adjectives Keywords – print, display, produce
Define the storage (variable) and data type to hold data
Outline the process (Arithmetic or logic operation)
Input-Process-Output (IPO) chart as tool
Program Development Process
Problem Analysis
Evaluate the following problem statement and identify
the input and output.
30
Program Development Process
Coding
Implement the flowchart or pseudo code into specific
programming language rules (syntax)
Translate the algorithm into a formal programming
language.
Syntax or grammatical rules are important in coding
Identify the storage requirement
Use text editor to write your program
Compilation
Program Development Process
Error correction
Syntax Error occurs when a character or string incorrectly
placed in a command or instruction that causes a failure in
execution. It is an error in a program due to a code that does not
conform to order expected by the programming language.
Logic Error is a bug in a program that causes it to operate
incorrectly, but not to terminate abnormally (or crash). A logic
error produces unintended or undesired output or other
behavior, although it may not immediately be recognized as such.
Runtime Error is a program error that occurs while the
program is running. It might cause the memory leak (infinity loop)
or program crash (quits while running).
Program Development Process
Testing
Program must be freed from syntax error
Use a set of test data to validate the output.
Program must produce receive valid input and produce
correct output.
Program must handle invalid input efficiently.
Does the program accept out of range value?
Program Development Process
Testing and Debugging
System
Library
Diagnostic
Message
Source
Program
Listing
Program Development Process
Documentation
User manual
Program description
Capability, limitation, user guide
Program Control Structure
There are SIX basic operation:
1. Accept Input through keyboard or files
2. Produce Output and displayed it on screen or file
3. Assign value to a storage
4. Perform arithmetic and logic
5. Make decision using selection statement
6. Repeat the same action
Program Control Structure
THREE types of control structure:
Sequential Structure
Selection Structure
Iteration Structure (Repetition or loop)
Program Control Structure
Sequential Structure
ALL statement(s) will be executed from top to bottom.
Statement can be an input/output statement or a processing statement.
Begin Begin
Read Number1,
Statement 1 Number2
Compute Total
Statement 2
Compute Average
Statement n
Print Total
and Average
End
End
Program Control Structure
Selection Structure
Provides alternative actions
Only one action will be executed
Three types:
One-way Selection
A set of statements will be executed if and only the condition is TRUE.
Two-way Selection
Either one of two set of statements will be executed if the condition is TRUE.
Multi-way Selection
Has more than one conditions and alternative set statements.
Program Control Structure
One-way Selection (Flow Chart)
Begin
Begin
Read Score
FALSE FALSE
Condition Score >= 50
TRUE
TRUE
Statement (s)
Status is “Passed”
End
End
Read Score
FALSE
Condition
FALSE
TRUE Score >= 50
End
End
Begin
FALSE
Condition 1
TRUE FALSE
Condition 2
Statement (s)
TRUE FALSE
Condition n
Statement (s)
TRUE
End
Program Control Structure
Multi-way Selection (Example)
Begin
FALSE
code == ‘M’
TRUE FALSE
code == ‘F’
gender is “Male”
TRUE
End
Program Control Structure
Iteration or loops
Perform the same operation more than once
A set of statements will be executed more than once
Can be controlled either by counter or sentinel value.
Initialize counter
False
Test
True Exit loop
Statement(s)
update counter
Program Control Structure
The following flowchart illustrates the process of reading five (5) numbers
and calculate the total.
count = 0
False
count < 5
True
exit loop
Read a
number
Count + 1
Program Control Structure
Sentinel Controlled loop flowchart:
False
Test
Sentinel value
True
Statement(s)
Exit loop
Program Control Structure
The following flowchart illustrates the process of reading a series of
students’ scores and compute the total score. The process will stop
if the value of score read is equal to 999.
Read score
False
Score != 999
Read Score
Self Exercise
1. Write a flow chart to calculate and display an average of three numbers
using sequential design
2. Rewrite the above design, determine whether the average is higher than
50 or not. Display the appropriate message for each case.
3. Rewrite problem 1 using iteration any iteration approach.
4. Write a flow chart to calculate an average score of 20 students.
5. Write a flow chart to calculate an average of a series positive numbers.
The process will stop if number entered has a negative value.
6. The cost to send a telegram to UK is RM15.50 for the first 15 letters
and RM0.50 for the subsequent letters. Draw a flowchart to calculate the
cost of sending the telegram.
References
Jeri R. hanly, Essential C++ for Engineers and Scientists, 2nd Edition,
Addison Wesley
J. Glenn Brookshear, Computer Science An Overview, 8th Edition, Pearson
Addison Wesley