You are on page 1of 52

Introduction To Computer

Programming
(CSC425)

by
PUAN AFIZA ISMAIL
Faculty of Computer & Mathematical Sciences
UiTM MALAYSIA
2013
Introduction

Chapter One
Contents
 Basic Concept
 Program Development Process
 Program Control Structure

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 3


PROGRAMMING
Basic Concept
 Computer Programming
 Program
 Programmer
 Programming Language
 Compiler
 Interpreter
 Interactive Programming Environment
 Algorithm

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 4


PROGRAMMING
Basic Concept
Reasons for studying Concepts of Programming Languages :

1. Increased capacity to express ideas and solve problems.


 Studying programming languages may increase the capacity
and ability of students to express their ideas in a formal,
computational form.

2. Increased ability to automate process


 Programs are built mainly so that simple, or even complicated
processes to be executed automatically.

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 5


PROGRAMMING
Basic Concept
 Computer programming
• Craft of developing a computer program.
• Require knowledge, skill and creativity.
• Both skill and knowledge in problem solving
and programming language.

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 6


PROGRAMMING
Basic Concept
 Computer program
• Hanly [2001], “List of instructions that direct
the computer to transform information from
one form to another.”
• Information refers to the contains of specific
memory location.
• It is written using programming language.

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 7


PROGRAMMING
Basic Concept
 Computer program
• Turn data (input) into useful information
(output).
• Program uses variables to store data

Input Program Output

Example
Storage

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 8


PROGRAMMING
Basic Concept
 Programming Language
• A set of rules, words and symbols are used to
write a computer program.
• Generation of Programming language
• Machine language
• Assembly language
• High Level language (Pascal, C, C++, Java etc)

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 9


PROGRAMMING
Basic Concept
 Machine Language
• Binary number codes understood by a
specific CPU.
 Assembly Language
• Mnemonic codes that correspond to machine
language instructions

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 10


PROGRAMMING
Basic Concept
 A Machine-language Program Fragment and Its
Assembly-Language Equivalent
Memory Address Machine-Language Assembly-Language
Instructions Instructions

00000000 00000000 CLA

00000001 00010101 ADD A

00000010 00010110 ADD B

00000011 00110101 STA A

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 11


PROGRAMMING
Basic Concept
 Type of High-level languages
Language Application Area Origin Name
FORTRAN Scientific Programming Formula Translation

COBOL Business data Processing Common Business-Oriented


Language
Lisp Artificial Intelligent List processing

C System Programming Predecessor Language was named B

Prolog Artificial Intelligent Logic Programming

C++ Support objects and object Incremental modification of C (++ is


oriented programming the C incremental operator)

Java Supports Web Programming Originally name “Oak”

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 12


PROGRAMMING
Basic Concept
 Compiler
• A program that translates a high-level language
program into machine language as a complete unit
 Interpreter
• A set of program that executes the instructions as they
were translated. Interpreter executes a program from
its high-level form.
 Interactive Development Environment (IDE)
• A program that provides user with an environment for
editing, debugging and compiling the source code the
program on- line.

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 13


PROGRAMMING
Basic Concept
 Source file
• A file containing a program written in a high-level
language; the input for the compiler
 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.

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 14


PROGRAMMING
Basic Concept
 Compilation Process

Source File Compiler Object File

(Input) (Output)

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 15


PROGRAMMING
Program Development Process
 Computer programming is a process to
develop a computer program.
 5 steps:
1. Problem analysis,
2. Program design,
3. Coding,
4. Testing, and
5. Documentation.

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 16


PROGRAMMING
Program Development Process
 Problem Analysis
• A process of identifying the output, processes and input of a
program.
• How to identify output?
• Nouns and adjectives
• Keywords – print, display, produce
• How to identify input?
• Nouns and adjectives
• Keywords – accept, enter, receive, read
• Define the storage (variable) and data type to hold data
• Outline the process
• Arithmetic or logic operation
• Input-Process-Output (IPO) chart as tool

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 17


PROGRAMMING
Program Development Process
 Problem Analysis
• Evaluate the following problem statement and identify the input
and output.

Nation’s Air force has asked you to write a program to


label supersonic aircraft as military or civilian. Your
program is to be given the plane’s observed speed in
km/h and its estimated length in meters. For planes
traveling in excess of 1100km/h, you will label those
longer than 52 meters “civilian” and shorter aircraft
as “military”. For planes traveling at slower speeds,
you will issue an “aircraft type unknown” message.

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 18


PROGRAMMING
Program Development Process
 Input-Process-Output (IPO) chart
INPUT PROCESSING OUTPUT
 speed  Validate the speed and  Classification
 length length Can be one if the
following values

• Speed >1100km/h AND Civilian


length > 52m
• Speed > 1100km/h AND Military
length <= 52m
• Speed <= 1100km/h Aircraft type unknown

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 19


PROGRAMMING
Program Development Process
 Program Design
• Developing an algorithm
• Tools used for Program Design are:-
• Flow Chart
• Pseudo Code

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 20


PROGRAMMING
Program Development Process
 Algorithm is a precise step-by-step
action to perform overall task of the
program.
 Can be represented either using
flowchart or pseudo-code

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 21


PROGRAMMING
Program Development Process
 A set of symbols and edges used in flow chart:

Terminal (Begin and End) Condition/Evaluation

To represent the process

Direction
Input/Output Operation

Example

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 22


PROGRAMMING
Program Development Process
 Flow chart
Begin

Read Speed

Read Length

False
Speed > 1100

True
False
Length > 52
Classification = “Aircraft Type Unknown”
True Classification = “Military”

Classification = “Civilian”

Display classification

End

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 23


PROGRAMMING
Program Development Process
 Desk-Check the algorithm
Speed Length Classification
1170 35 ?
1180 56 ?
900 66 ?
800 34 ?

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 24


PROGRAMMING
Program Development Process
 Coding
• Implement the flowchart or pseudo code into
specific programming language rules (syntax)
• Identify the storage requirement
• Compilation
• Error correction
• Syntax Error,
• Logic Error or
• Runtime Error Source Code

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 25


PROGRAMMING
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?

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 26


PROGRAMMING
Program Development Process
 Documentation
• User manual
• Program description
• capability, limitation, user guide

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 27


PROGRAMMING
Program Control Structure
 There are SIX basic operation:
1. Accept Input through keyboard or files
2. Produce Output and displayed it on screen
of file
3. Assign value to a storage
4. Perform arithmetic and logic
5. Make decision using selection statement
6. Repeat the same action

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 28


PROGRAMMING
Program Control Structure
 THREE types of control structure:
• Sequential Structure
• Selection Structure
• Iteration Structure (Repetition or loop)

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 29


PROGRAMMING
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.

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 30


PROGRAMMING
Program Control Structure
 Sequential Structure
Begin

Statement 1

Statement 2

Statement n

End

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 31


PROGRAMMING
Program Control Structure
 Sequential Structure (Flow Chart)
Begin

Read Number1,
Number2

Compute Total

Compute Average

Print Total
and Average

End

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 32


PROGRAMMING
Program Control Structure
 Selection Structure
• Provides alternative actions
• Only one action will be executed
 Three types:
• One-way Selection
• Two-way Selection
• Multi-way Selection

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 33


PROGRAMMING
Program Control Structure
 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.

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 34


PROGRAMMING
Program Control Structure

 One-way Selection (Flow Chart)


Begin

FALSE
Condition

TRUE

Statement (s)

End

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 35


PROGRAMMING
Program Control Structure
 One-way Selection (Example)
Begin

Read Score

FALSE
Score >= 50

TRUE

Status is “Passed”

End

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 36


PROGRAMMING
Program Control Structure

 Two-way Selection (Flow Chart)


Begin

FALSE
Condition

TRUE

Statement (s) Statement (s)

End

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 37


PROGRAMMING
Program Control Structure
 Two-way Selection (Example)
Begin

Read Score

FALSE
Score >= 50

TRUE

Status is “Passed” Status is “Failed”

End

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 38


PROGRAMMING
Program Control Structure
 Two-way Selection (Example)
Begin

Read Score

FALSE
Score >= 50

TRUE

Status is “Passed” Status is “Failed”

End

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 39


PROGRAMMING
Program Control Structure
 Multi-way Selection (Flow Chart)
Begin

FALSE
Condition 1

TRUE FALSE
Condition 2
Statement (s)
TRUE FALSE
Condition n
Statement (s)
TRUE

Statement (s) Statement (s)

End

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 40


PROGRAMMING
Program Control Structure

 Multi-way Selection (Example)


Begin

FALSE
code = ‘M’

TRUE FALSE
code = ‘F’
gender is “Male”
TRUE

gender is “Female” Status is “Unknown”

End

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 41


PROGRAMMING
Program Control Structure
 Iteration or loops
• Perform the same operation more than once
• A set of statements will be executed more
than once
• Example – Read 40 students’ score and
compute the total.
• Can be controlled either by counter or
sentinel value.

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 42


PROGRAMMING
Program Control Structure
 Counter Controlled loop flowchart:

Initialize counter

False
Test

True Exit loop


Statement(s)

update counter

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 43


PROGRAMMING
Program Control Structure
 The following flowchart illustrate the process of reading
five (5) numbers and calculate the total

count = 0

False
count < 5
True
exit loop
Read a
number

Add number to total

Count + 1

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 44


PROGRAMMING
Program Control Structure
 Sentinel Controlled loop flowchart:

False
Test
Sentinel value

True

Statement(s)

Exit loop

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 45


PROGRAMMING
Program Control Structure
 The following flowchart illustrate 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?

True Exit loop

Add score to total

Read Score

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 46


PROGRAMMING
Program Control Structure
 What are the differences between
counter controlled loop and sentinel
controlled loop?

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 47


PROGRAMMING
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.

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 48


PROGRAMMING
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

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 49


PROGRAMMING
Example 1
(Input – Process – Output)

Number1 (40) Calculate Total (70)


Number2 (30) total

Number1 [ 40 ]

Number2 [ 30 ]
Total [70 ]

Back

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 50


PROGRAMMING
Example 2 (Flow Chart)

Begin

Read two
numbers

Calculate Total

Print Total

Back
End

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 51


PROGRAMMING
Source Code
#include <iostream.h>
#include <string.h>
//program to classify the type of aircraft
main()
{
int speed, length;
char classfication[30];

cout << “Enter Observed speed : ”;


cin >> length;
cout << “Enter estimated length : ”;
cin >> length;

if (speed > 1100)


if (length > 52)
strcpy(classification,“civilian”);
else
strcpy(classification,“Military”);
else
strcpy(classification,“Aircraft type unknown”);

cout << “Classification is ” << classification << endl;

return 0;
}//end main() Back

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 52


PROGRAMMING

You might also like