Вы находитесь на странице: 1из 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

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

Basic Concept

Computer Programming
Program
Programmer
Programming Language
Compiler
Interpreter
Interactive Programming Environment
Algorithm
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


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.

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


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.

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


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.

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

Basic Concept
Computer

program

Turn data (input) into useful information

(output).
Program uses variables to store data
Input

Program

Output

Storage
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

Example
8

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)

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

Basic Concept
Machine

Language

Binary number codes understood by a


specific CPU.

Assembly

Language

Mnemonic codes that correspond to machine


language instructions

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

10

Basic Concept

A Machine-language Program Fragment and Its


Assembly-Language Equivalent
Memory Address

Machine-Language
Instructions

Assembly-Language
Instructions

00000000

00000000

CLA

00000001

00010101

ADD A

00000010

00010110

ADD B

00000011

00110101

STA A

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

11

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

System Programming

Predecessor Language was named B

Prolog

Artificial Intelligent

Logic Programming

C++

Support objects and object


oriented programming

Incremental modification of C (++ is


the C incremental operator)

Java

Supports Web Programming

Originally name Oak

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

12

Basic Concept

Compiler

Interpreter

A program that translates a high-level language


program into machine language as a complete unit
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.

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

13

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.
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

14

Basic Concept
Compilation

Source File

Process

Compiler

(Input)

December 7, 2016

Object File

(Output)

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

15

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.

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

16

Program Development Process

Problem Analysis

A process of identifying the output, processes and input of a


program.
How to identify output?

How to identify input?

Define the storage (variable) and data type to hold data


Outline the process

Input-Process-Output (IPO) chart as tool

Nouns and adjectives


Keywords print, display, produce

Nouns and adjectives


Keywords accept, enter, receive, read

Arithmetic or logic operation

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

17

Program Development Process

Problem Analysis

Evaluate the following problem statement and identify the input


and output.

Nations Air force has asked you to write a program to


label supersonic aircraft as military or civilian. Your
program is to be given the planes 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.

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

18

Program Development Process

Input-Process-Output (IPO) chart


INPUT

speed
length

PROCESSING
Validate the speed and
length

OUTPUT

Speed >1100km/h AND


length > 52m
Speed > 1100km/h AND
length <= 52m
Speed <= 1100km/h

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

Classification
Can be one if the
following values
Civilian
Military
Aircraft type unknown

19

Program Development Process


Program

Design

Developing an algorithm
Tools used for Program Design are: Flow Chart
Pseudo Code

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

20

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

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

21

Program Development Process

A set of symbols and edges used in flow chart:


Terminal (Begin and End)

Condition/Evaluation

To represent the process

Input/Output Operation

Direction

Example
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

22

Program Development Process

Flow chart
Begin

Read Speed

Read Length
False
Speed > 1100
True
Length > 52
True

False
Classification = Military

Classification = Aircraft Type Unknown

Classification = Civilian

Display classification

End

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

23

Program Development Process

Desk-Check the algorithm


Speed

Length

Classification

1170

35

1180

56

900

66

800

34

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

24

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

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

Source Code
25

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?

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

26

Program Development Process


Documentation

User manual
Program description

capability, limitation, user guide

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

27

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
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

28

Program Control Structure


THREE

types of control structure:

Sequential Structure
Selection Structure
Iteration Structure (Repetition or loop)

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

29

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.

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

30

Program Control Structure


Sequential

Structure
Begin
Statement 1
Statement 2

Statement n

End
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

31

Program Control Structure

Sequential Structure (Flow Chart)


Begin
Read Number1,
Number2

Compute Total
Compute Average
Print Total
and Average

End
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

32

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
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

33

Program Control Structure

One-way Selection

Two-way Selection

A set of statements will be executed if and only the


condition is TRUE.
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.

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

34

Program Control Structure

One-way Selection (Flow Chart)


Begin

Condition

FALSE

TRUE

Statement (s)

End

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

35

Program Control Structure


One-way

Selection (Example)
Begin
Read Score
FALSE
Score >= 50
TRUE

Status is Passed

End
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

36

Program Control Structure

Two-way Selection (Flow Chart)


Begin

Condition

FALSE

TRUE

Statement (s)

Statement (s)

End

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

37

Program Control Structure


Two-way

Selection (Example)
Begin
Read Score

Score >= 50

FALSE

TRUE
Status is Passed

Status is Failed

End
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

38

Program Control Structure


Two-way

Selection (Example)
Begin
Read Score

Score >= 50

FALSE

TRUE
Status is Passed

Status is Failed

End
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

39

Program Control Structure

Multi-way Selection (Flow Chart)


Begin

Condition 1
TRUE

Statement (s)

FALSE

Condition 2
TRUE

Statement (s)

FALSE

Condition n

FALSE

TRUE

Statement (s)

Statement (s)

End
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

40

Program Control Structure

Multi-way Selection (Example)


Begin

code = M
TRUE
gender is Male

FALSE

code = F

FALSE

TRUE
gender is Female

Status is Unknown

End
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

41

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.

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

42

Program Control Structure

Counter Controlled loop flowchart:

Initialize counter

Test
True

False

Exit loop

Statement(s)

update counter

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

43

Program Control Structure

The following flowchart illustrate the process of reading


five (5) numbers and calculate the total
count = 0

count < 5

False

True
Read a
number

exit loop

Add number to total


Count + 1
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

44

Program Control Structure

Sentinel Controlled loop flowchart:

Test
Sentinel value

False

True
Statement(s)
Exit loop

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

45

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

Score <> 999?

False

True

Exit loop

Add score to total

Read Score

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

46

Program Control Structure


What

are the differences between


counter controlled loop and sentinel
controlled loop?

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

47

Self Exercise
1.
2.

3.
4.
5.

6.

Write a flow chart to calculate and display an average of three


numbers using sequential design
Rewrite the above design, determine whether the average is
higher than 50 or not. Display the appropriate message for
each case.
Rewrite problem 1 using iteration any iteration approach.
Write a flow chart to calculate an average score of 20
students.
Write a flow chart to calculate an average of a series positive
numbers. The process will stop if number entered has a
negative value.
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.
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

48

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

December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

49

Example 1
(Input Process Output)
Number1 (40)
Number2 (30)

Calculate
total

Total (70)

Number1 [ 40 ]
Number2 [ 30 ]
Total [70 ]

Back
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

50

Example 2 (Flow Chart)


Begin

Read two
numbers

Calculate Total

Print Total

End
December 7, 2016

CSC425 : INTRODUCTION TO COMPUTER


PROGRAMMING

Back
51

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()
December 7, 2016

Back
CSC425 : INTRODUCTION TO COMPUTER
PROGRAMMING

52

Вам также может понравиться