+ All Categories
Home > Documents > UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

Date post: 31-Dec-2015
Category:
Upload: alannah-warner
View: 220 times
Download: 0 times
Share this document with a friend
Popular Tags:
42
UNIMAP Sem1-08/0 9 EKT120: Computer Programm ing 1 Week 1 – Introduction to Computer and Algorithm
Transcript
Page 1: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

1

Week 1 – Introduction to Computer and

Algorithm

Page 2: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

2

General Information

Contributes 4 units: 2 hours - lectures 4 hours – lab and tutorial

Main Objective: Students can independently write a

computer program to solve calculation problems, especially those related to engineering

Page 3: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

3

Course Outcomes Understand programming concept and

principle. Ability to identify, analyze and produce

software program using flowchart and pseudocode.

Familiarize with LINUX environment for coding, compiling, executing and debugging computer software program.

Obtain logical, creative and innovative thinking skills.

Ability to work as a team.

Page 4: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

4

Overall Evaluation 4 main components:

Assignments + lab activities (30%) 1st & 2nd Test (15% +25%) Group Mini-project (30%)

Assignments are individual “take home” + lab

The two tests are written tests Project : Max number of group member

is 3

Page 5: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

5

References C How to program, Deitel, Suhizaz, R

Badlishah, Yasmin, Pearson Prentice Hall

C How To Program 5th edition”, Deitel and Deitel, Pearson-Prentice Hall ,2007

“C Programming for Engineering & Computer Science ”, H.H.Tan and T.B.Orazio, Mc Graw Hill, 1999.

Any other C programming books

Page 6: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

6

Notes This course is NOT about the

language per se, it’s about problem solving / analytical skills & to apply C to solve problems.

Write C program in Linux environment

Do early reading Do not hesitate to ask during

lecture sessions

Page 7: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

7

Outline Computer fundamentals

Computer organization and hardware Computer software

Programming languages Machine language Assembly language High-level language

Algorithm : pseudocode and flowchart Control structures Simple C program

Page 8: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

8

Computer Fundamentals Computer system are divided into

hardware and software. Hardware refers to physical components

of computers which are: Main memory Central Processing Unit(CPU) Input Device Output Device Secondary memory device

Page 9: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

9

Computer Hardware

Input Device Output Device

Secondary Memory

Control Unit

Arithmetic and Logic Unit

Register

CPU

Main Memory

Page 10: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

10

Central Processing Unit(CPU) CPU is the computer’s administrator

and is responsible for supervising the operation of the other section

Consists of two functional units: a control unit and an arithmetic-logic unit(ALU)

Control unit supervises all activities of the computer system

ALU perform basic arithmetic operations and comparison operations

Page 11: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

11

Main Memory

keeps information from the input unit also keeps processed information until

it can be placed on output devices all programs must be loaded into main

memory before they can be executed and all data must be brought into main memory before it can be manipulated.

Page 12: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

12

Main Memory Main memory can be further

classified into two types: Random Access Memory (RAM)

information in RAM will be lost when the computer is turned-off.

Read Only Memory (ROM) they have been set during the

manufacturing process. ROM usually contains instructions and information considered to be fundamental to the computer.

Page 13: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

13

Secondary Memory

Main memory is only used during processing following certain instructions

Permanent information is NOT stored in main memory but is stored in secondary memory E.g. program file, data fail etc E.g hard disk, diskette, CD

Page 14: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

14

Input/Output Device

Input device - feeds data and programs into computers E.g. keyboard, mouse, touch screen,

scanners Output device - display results

produced by computer E.g. monitor, printer, speaker

Page 15: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

15

Software

As a complement to hardware, computer system needs software to solve problems.

Software are classified into : System software Application software

Page 16: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

16

Software System software : manage the

computer and its peripheral devices (hardware) E.g. Operating systems(OS) Text editor Preprocessor Language translator Linker Loader

Page 17: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

17

Software Application software : performs specific

tasks There are two types:

Program to solve specific problems Program written by user to solve specified

problem E.g word processor, desktop publishing

software, spreadsheets, database, graphics, communication, program perform a specific task such as accounting, scientific, engineering, education etc

Page 18: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

18

Programming Languages

Programming language is divided into three categories: Machine Language Assembly Language High-Level Language

Page 19: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

19

Machine Language Language understood by the computer Bunch of 0’s and 1’s Program written in machine language

can be executed without being translated

Nevertheless, hard to learn because it is written in 0’s and 1’s

Program is too long to solve simple problem Machine-dependant and not portable E.g. 0101 1000 0101 1001 1100 0111

0101 0001 1100 0100 1011 1000

Page 20: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

20

Assembly Language

Strings of 0’s and 1’s are replaced into instructions which resembles English language to represent computer operation element

Easier to understand and written E.g LOAD rate

MULT hourSTOR wages

Page 21: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

21

Assembly Language

Nevertheless, need language translator called Assembler to change Assembly Language to Machine Code for execution purpose

still too long and not portable

Page 22: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

22

High-Level Language Improve weaknesses in Machine

Language and Assembly Language portable Written in one instruction to carry out

several instruction in machine level E.g. discount_price = price – discount;

Must be changed to machine code before executed, need compiler : a system software that translate source program to object program

Page 23: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

23

Algorithms The solution to any computing problem

involves executing a series of actions in a specific order

Pseudocode : artificial and informal language that helps programmers develop algorithms

E.g. if student’s grade is greater than or equal to 50

Print “Pass” else

Print “Fail”

Page 24: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

24

Algorithms

Flowchart: visual-form of an algorithm

E.g.

Process 2Decision

Data

Process 1

Begin

End

Page 25: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

25

Algorithm-Basic symbols in a flowchart

Start/ End

Process

Input/Output

Decision

Flow direction

Connector

Page 26: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

26

Flowchart-(E.g)

Start

read num1, num2

sum=num1+num2

print sum

End

Page 27: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

27

TRY THIS!!!

Write a pseudocode, flowchart and program that calculate and print the SUM of two integers A and B.

Page 28: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

28

Pseudocode

• Begin• Input A and B• Calculate A + B• Print results of SUM• End

Flowchart

Begin

Input A,B

CalculateA + B

Prints SUM

End

Page 29: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

29

Control Structure

All programs could be written in terms of three control structures: Sequence structure Selection structure Repetition structure

Page 30: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

30

Sequence structure

Is a series of steps executed sequentially by default

Read num1,num2

Calculate total=num1+num2

Print total

Pseudocode Flowchart

Read num1,num2

total = num1+num2

print total

Page 31: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

31

Selection structure

Used to choose among alternative courses of action

C has three types: if, if..else, and switch

Page 32: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

32

The if selection structure

if structure is a single-entry/single-exit structure

true

false

grade >= 60

print “Passed”

 

If student’s grade is greater than or equal to 60

Print “Passed”

Page 33: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

33

The if..else selection structure

Specifies an action to be performed both when the condition is true and when it is false

truefalse

print “Failed” print “Passed”

grade >= 60

If student’s grade is greater than or equal to 60

Print “Passed”else

Print “Failed”

Page 34: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

34

Repetition structure

Specifies a block of one or more statements that are repeatedly executed until a condition is satisfied

Three types : while, for, do-while

Page 35: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

35

The while repetition structure Programmer specifies an action is to be

repeated while some conditions remains true

product <= 1000 product = 2 * producttrue

falseWhile product is less than or equal 1000

calculate product=2 * product

Page 36: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

36

Simple C Program//This program will calculate number of RM 50, RM 20, RM 10, RM 5 and RM 1 notes//for a given value entered by user.#include <stdio.h>

int main(void){ int value, balance; int note50, note20, note10, note5, note1;

printf("Enter value in RM : "); scanf("%d",&value); note50 = value / 50; balance = value % 50; note20 = balance /20; balance = balance % 20; note10 = balance / 10; balance = balance % 10; note5 = balance / 5; note1 = balance % 5; printf("Number of RM 50 notes : %d\n", note50); printf("Number of RM 20 notes : %d\n", note20); printf("Number of RM 10 notes : %d\n", note10); printf("Number of RM 5 notes : %d\n", note5); printf("Number of RM 1 notes : %d\n", note1); return 0;}

Page 37: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

37

Basics of a Typical C Program Development Environment

Page 38: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

38

Variables & Reserve Words Variables

labels for program elements case sensitive can consists of capital letters[A..Z], small

letters[a..z], digit[0..9], and underscore character _ First character MUST be a letter or an underscore No blanks Reserve words cannot be identifiers

Reserve words already assigned to a pre-defined meaning eg: delete, int, main, include, double, for, if etc.

Page 39: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

39

Data Types & Mem. Alloc.

1Boolean representation of logic states. Can only be assigned true (1) or false (0).

bool

8A more precise version of float. Has larger dynamic range and better representation of decimal points.

double

4Floating-point number. Set of real numbers. float

4Integer quantity. Can be represented in signed or unsigned form (with the unsigned keyword).

int

1A single character. Internally stored as a coded integer value (refer to ASCII table).

char

Size (byte

s)Description

Data Type

Page 40: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

40

Type declarations int a; int A; float SUM; float income;

float net_income; int index =0, count =0; char ch=‘a’, ch2; const float epf = 0.1, tax = 0.05;

float income, net_income;

Declare and initialize

Named constant declared and

initialized

Page 41: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

41

#include <stdio.h>int main(void){ int A, B, SUM; printf (“input first integer \n”); scanf (“%d”, &A) printf (“input second integer \n”); scanf (“%d”, &B) SUM = A + B;

printf (“Sum is %d\n”, SUM); return 0;}

OUTPUT

Input first integer

39

Input second integer

27

Sum is 66

Page 42: UNIMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm.

UNIMAP Sem1-08/09

EKT120: Computer Programming

42

End Week 1 – Session 1

Q & A!


Recommended