COMPUTER PROGRAMMING - PowerPoint PPT Presentation

1 / 133
About This Presentation
Title:

COMPUTER PROGRAMMING

Description:

This course presents the C programming language for ... Pengantar Pengaturcaraan C, Marini Abu Bakar, Sufian Idris, Penerbitan UKM, (ISBN:983-99919-0-6) ... – PowerPoint PPT presentation

Number of Views:1102
Avg rating:3.0/5.0
Slides: 134
Provided by: FKEE
Category:

less

Transcript and Presenter's Notes

Title: COMPUTER PROGRAMMING


1
COMPUTER PROGRAMMING
BEE1222
ADDIE IRAWAN HASHIM
2
  • Course
  • BEE 1222 Computer Programming
  • 2 Credit Lecture Lab 4 hours
  • Lecturer
  • Addie Irawan b. Hashim
  • Faculty of Electrical Electronic Engineering
  • Phone 09-5492356
  • E-mail addieirawan_at_kuktem.edu.my

By Addie Irawan (FKEE)
3
Synopsis
  • This course presents the C programming language
    for studying on electrical engineering,
    electronic engineering, computer engineering,
    applied computer science and applied information
    technology.
  • The focus point of the subject is to make sure
    the student literates on software and hardware
    interfaces.
  • On the final of the subject, student will have
    several skills on solving the problem, methodical
    planning and fault finding for software
    hardware problem using C programming language.

By Addie Irawan (FKEE)
4
General objective
  • At the end of this course, the students
  • will be able to
  • Have primarily knowledge of programming method
    and analysis using C programming language.
  • Have skills on manipulating the basic functions
    and operators of C programming language for
    application software development.
  • Understand the uses of C programming language in
    electrical electronic engineering field.
  • Manipulate the C programming language for
    hardware control (interfaces).

By Addie Irawan (FKEE)
5
Syllabus
  • Introduction (2 hours)
  • Input Output Statement (2 hours)
  • Structured Program Development (2 hours)
  • Selection Statement (2 hours)
  • Repetitive Statement (3 hours)
  • Function (2 hours x 2)
  • Array String (2 hours)
  • File I/O (2 hours x 2)
  • Hardware Software Interfaces (2 hours)
  • Project ( 4 hours x 3)

By Addie Irawan (FKEE)
6
References
  • C program design for engineers, Jeri R. Hanly
    Elliot B.Koffman, Addison
  • Wesley, (ISBN9-780201-708714)
  • C How to Program, HM Deitel PJ Deitel
    Prential Hall
  • C by example, Greg Perry, Que College (ISBN
    1-56529-453-X)
  • Step-by-Step Guide to C Programming, Jean Paul
    Corriveau, Prentice Hall
  • Publications 1998 BOOK (0-13-339946-X)
  • C for electronic engineering with applied
    software engineering, William
  • Buchanan, Prentice Hall (ISBN 0-13-342668-8)
  • Programming with C, Byron Gottfried, McGraw-Hill
    Schaum's outline series
  • (ISBN0-07-114259-2)
  • Pengantar Pengaturcaraan C, Marini Abu Bakar,
    Sufian Idris,
  • Penerbitan UKM, (ISBN983-99919-0-6)

By Addie Irawan (FKEE)
7
Assessments
  • Final grade will be calculated as
  • follows
  • Assignment 10 (1x)
  • Quiz 10
  • Test 60 (20 x
    3)
  • Project 20 (1x)
  • Total 100

By Addie Irawan (FKEE)
8
Class Hours
Group E20BB Tuesday 400 550 p.m. (2
hours) Thursday 400 550 p.m. (2
hours) Group E21BB Tuesday 800 950 a.m.
(2 hours) Thursday 800 950 a.m. (2
hours) Group E22BB Monday 900 1050 a.m.
(2 hours) Friday 300 450 p.m. (2
hours)
All in FKE9
By Addie Irawan (FKEE)
9
Leave Plan
December 2005
February 2006
Raya Haji Chinese New Yr. leave
January 2006
My vacation
Class Cancelled
By Addie Irawan (FKEE)
10
CHAPTER 1
INTRODUCTION
By Addie Irawan (FKEE)
11
Introduction
  • Hardware, software and firmware
  • Basic computer architecture
  • Compiling, linking producing an executable
    program
  • C language elements Pre-processor,
    Pre-processor directive
  • Declaration of variable and constants
  • C operators
  • Exercises
  • References

By Addie Irawan (FKEE)
12
The Important Elements
  • Hardware ? bits that can be touched.
  • The equipment/physical
    module
  • eg the components,
    screw and nuts,
  • electrical wire
  • Software ? Programs that run on programmable
  • hardware and change their
    operation depending
  • on the inputs to the
    system.
  • egi/p keyboard
  • Firmware ? Hardware device that is programmed
    using
  • software. egEEPROM,
  • Micro-controller such as
    PIC, BS-2 etc.

By Addie Irawan (FKEE)
13
Basic computer architecture
  • Three main elements of basic computer system
  • CPU (central processing unit/microprocessor)
  • Memory
  • Input/Output
  • Three main buses
  • Address bus
  • Control bus
  • Data bus

By Addie Irawan (FKEE)
14
Simple Block Diagram of computer system
Data Bus
External devices
Control lines
Address Bus
By Addie Irawan (FKEE)
15
Programming Process execution
  • Compiler ? changes the high-level language into
  • machine code, e.g. high
    level (C, BASIC,
  • COBOL, Fortran, Pascal)
  • Linker ? links the object code with other object
    code
  • files (to produce executable
    program)

By Addie Irawan (FKEE)
16
Computer Language Hierarchy
Application Level Language
High Level Language
Low level Language
Machine Language
By Addie Irawan (FKEE)
17
The different
  • Machine language ? a collection of binary number
  • Assembly language ? a language in which computer
  • operations are represented by mnemonics

By Addie Irawan (FKEE)
18
  • High level language ? instructions that needed to
    be written by human programmer. It allows the
    programmer to write the programs with far less
    concern about the internal design of the machine
    such as memory allocation manipulation.
  • Application level language ? More user friendly
    type of programming language such as graphical
    programming eg. Visual Basic, LabVIEW etc.

By Addie Irawan (FKEE)
19
Programming processes
Source code file (FILE.C)
Object code file (FILE.OBJ)
Executable file (FILE.EXE)
By Addie Irawan (FKEE)
20
C language elements
Pre-processor ? a system program that modifies a
C program prior to its compilation Pre-processor
directive ? a C program line beginning with
that provides an instruction to the
pre-processor Library ? a collection of useful
function and symbols that may be accessed by a
program Constant macro ? a name that is replaced
by a particular constant value before the program
is sent to the compiler Comment ? text
beginning with / and ending with / that
provides supplementary info but is ignored by the
pre-processor and compiler
By Addie Irawan (FKEE)
21
C language elements (cont'd)
Replace macros with define directive
Object code file (FILE.OBJ)
Processed source code file
Executable file (FILE.EXE)
Source code file (FILE.C)
Include files with include directive (e.g
stdio.h, math.h, etc) ANSI
Libraries and other object code
Operations on the program to produce an file
By Addie Irawan (FKEE)
22
C language elements (cont'd)
C Structure
By Addie Irawan (FKEE)
23
C language elements (cont'd) eg. C language
elements in Miles-to-kilometers conversion program
By Addie Irawan (FKEE)
24
Header file Comment ---------------------------
--------------------------------------------------
ctype.h character classification and
conversion math.h maths functions stdio.h
I/O routines, such as input from
keyboard, output to display and
file handling (stdiostandard
I/O) conio.h - what is about ? -
explain standard indentifier involves? Typical
header files
By Addie Irawan (FKEE)
25
Declaration of identifiers
Variables Variable within a program can be
stored number or character Values can be
changed during program executed Rules for
declaration of variables Must defined the
particular name for each variable Constants
Values cannot be changed during execution of
program Type of constants - character,
integer, floating point, string Rules of
declaration constant Using const or define
By Addie Irawan (FKEE)
26
General rules for identifiers
  • First character must be alphabetic character or
  • underscore
  • 2. Must consists ONLY alphabetic character,
    digits or
  • underscore.
  • 3. First 31 characters are significant
  • 4. Cannot duplicate a reserved word

example
By Addie Irawan (FKEE)
27
Data type
--------------------------------------------------
----------------- Type Range -----------
--------------------------------------------------
------ char -128 to 127 int -3276
8 to 32767 float -3.4E38 to
3.4E38 double -1.7E308 to
1.7E308 double-precision
3.4x10-4932 to 1.1x104932 _____________________
_____________________
example int num_steps, bit_mask float r
esistor1, resistor2 char char1, char2
By Addie Irawan (FKEE)
28
C operators
C have four main types of operators -
Arithmetic - Logical - Relational
By Addie Irawan (FKEE)
29
Arithmetic Operator
  • To conduct arithmetic operation, also known as
    math
  • operations known as unary operators if use
    for single
  • operand, e.g. a.
  • When used between constants or variables or
    both, it's
  • called a binary operator because it operates
    with two
  • values, e.g. zxy
  • Type of arithmetic
  • multiplication
  • / division and integer division
  • modulus (gives reminder after
  • integer division)
  • addition
  • - substraction
  • The assignment operate () is used when a
    variable
  • 'takes on the value' of an operation.

By Addie Irawan (FKEE)
30
Examples of arithmetic ________________________
________________ Statement Equivalent ------
--------------------------------------------------
----------- x3.0 xx3.0 voltage/sqrt(
2) voltagevoltage/sqrt(2) no_values-- n
o_valuesno_values 1 i-- ii -
1 screen_ptr screen_ptrscreen_ptr1
By Addie Irawan (FKEE)
31
Relational
  • Relational operator use for data comparison
  • Use with if statements
  • The result is TRUE (1) and FALSE (0)
  • There are six relational operators in C. They
    are

Operator Difination lt less
than lt less than or equal
to gt greater than gt greater
than or equal to equal
to ! not equal to
By Addie Irawan (FKEE)
32
e.g Suppose that i, j and k are integer
variables whose values are 1, 2 and 3
respectively. Several logical expressions
involving these variables are shown below
Expression Interpretation Value 1. i lt
j true 1 2. (ij) gt
k ? ? 3. (jk) gt
(i5) ? ? 4. k !3 ?
? 5. j2 ? ?
By Addie Irawan (FKEE)
33
e.g Suppose that i is an integer variables
whose value is 7, f is a floating-point variable
whose value is 5.5 and c is character variable
that represent the character 'w'. Several logical
expressions involving these variables are shown
below
Expression Interpretation Value 1. f gt
5 ? ? 2. (if) lt
10 ? ? 3. c 119 ?
? 4. c !'p' ? ? 5. c gt 10 ( i
f) ? ?
By Addie Irawan (FKEE)
34
Logical
  • Logical operator use to combine more than one
  • relational test. Also known as compound
    relational
  • operators.
  • Contains two logical operators
  • - logical AND ()
  • - logical OR ( )

By Addie Irawan (FKEE)
35
e.g. Suppose that i is an integer variables
whose value is 7, f is a floating-point variable
whose value is 5.5 and c is character variable
that represent the character 'w'. Several logical
expressions involving these variables are shown
below
Expression Next Action 1. (i gt 6) (c
'w') ? ? 2. (i gt 6) (c
119) ? ? 3. (f lt 11) (i gt
10) ? ? 4. (c ! 'p') ((i f) lt
10) ? ?
By Addie Irawan (FKEE)
36
Different in C
  • Library More additional elements Function
  • Basically using include ltiostreamgt
  • as a basic Input/Output library
  • and basic function
  • Easy on structural program

By Addie Irawan (FKEE)
37
CHAPTER 2
INPUT/OUTPUT
By Addie Irawan (FKEE)
38
Outline
  • Introduction
  • Standard output
  • Standard input
  • Exercises
  • References

By Addie Irawan (FKEE)
39
  • Every program has some form of o/p and i/p.
  • Type of i/p keyboard, a file, ports (parallel
    or
  • serial), mouse, etc.
  • Type of o/p displays, printers, hard-disk,
    etc.

By Addie Irawan (FKEE)
40
  • Standard I/O functions are not built-in to the C
  • language
  • It stored in libraries and linked into the
    program
  • The include pre-processor directive includes
    the
  • header file
  • stdio.h is the file associated to the standard
    I/O
  • functions

By Addie Irawan (FKEE)
41
  • Two basic o/p functions are
  • a) printf (format, arg1, arg2 ... argn)
  • format formatted string to the
    standard o/p
  • (display)
  • arg1, arg2, argn arguments that can be
    variables, const,
  • character or combination all of them
  • b)puts(string)
  • outputs a text string to the standard o/p
    and appends it with a
  • new line
  • For C
  • C) cout ltlt string

By Addie Irawan (FKEE)
42
a) printf(Welcome to KUKTEM) Output Welc
ome to KUKTEM (cursor remain here) b)
puts(Welcome to KUKTEM) Output Welcome
to KUKTEM (cursor at HERE!!!)
By Addie Irawan (FKEE)
43
c) printf(Welcome to KUKTEM) printf(Welcome
to KUKTEM) Output Welcome to
KUKTEMWelcome to KUKTEM d) printf(Welcome to
KUKTEM\n) printf(Welcome to
KUKTEM\n) Output Welcome to
KUKTEM Welcome to KUKTEM
By Addie Irawan (FKEE)
44
e) printf(Welcome to KUKTEM\nWelcome to
KUKTEM2\n) printf(Welcome to
KUKTEM3) Output Welcome to
KUKTEM Welcome to KUKTEM2 Welcome to
KUKTEM3 The \n called special control
character. Table 1 shows other special control
character and the functions
By Addie Irawan (FKEE)
45
Character Function
\a bell \b backspace (move
back one space) \f form-fed \r carriage
return \t horizontal tab spacing \n new
line (line-fed) Table 1 Special control
character
By Addie Irawan (FKEE)
46
  • Describe the format how the message string uses
    the other arguments
  • Table 2 show the conversion control characters
  • --------------------------------------------------
    -----------------------------
  • Operator Format
  • --------------------------------------------------
    -----------------------------
  • c single character
  • d single decimal integer
  • e scientific floating point
  • f floating point
  • s string of character
  • Table 2 Conversion control character

By Addie Irawan (FKEE)
47
printf(Values are f d c\n, va1,va2,ch)
By Addie Irawan (FKEE)
48
include ltstdio.hgt include ltconio.hgt main
() int var1, var2 float var3 var15
var26 var315 printf(the sum of f and f
is equal to f\n, var1,var2,
var1var2) printf(The value of var3 is d and
var1 is f\n, var3,var1) getch() Determin
e the o/p
By Addie Irawan (FKEE)
49
Requ
R1
Determine the equivalent resistance of three
resistors connected in parallel. Figure above
gives a schematic diagram of this set-up. The
resistors connected are R1(1.0e3?), R2(500? )and
R3(250?? and the equivalent i/p resistance is
Requ. Use the following formulas for
Requ. Requ 1 /( 1/R11/R21/R3)?
By Addie Irawan (FKEE)
50
include ltstdio.hgt include ltconio.hgt main
() float R1,R2,R3,R_equ R11.0e3 R2500
R3250 printf(three resistors connected in
parallel) R_equ 1/(1/R11/R21/R3) printf(
R1f,R2f,R3f\n,R1,R2,R3) printf(Equiv.
Resistance is f ohm\n,R_equ) getch()
Determine the o/p Note Modify the
program if three resistors is connected in
series. (Must know the formula equivalent
resistor)
By Addie Irawan (FKEE)
51
  • basic i/p functions
  • scanf (format, arg1, arg2 ... argn)
  • format a string that defines the format of
    all
  • entered values. It should only contain
    the conversion control characters such
    as d,
  • f, c, s etc, separated by spaces.
  • arg1, arg2, argn arguments that can be
    variables, const,
  • character or combination all of them

By Addie Irawan (FKEE)
52

scanf(Values are f d c, va1,va2,ch)
By Addie Irawan (FKEE)
53
include ltstdio.hgt include ltconio.hgt main
() float va1,va2 printf(type in two
value of floating numbergtgt) scanf (f f,
va1, va2) printf(Floating no. entered were
f fgtgt,va1,va2) getch()
Determine the o/p Note Replaced
the function scanf with this format
(ff , va1,va2) ? Note your
observation!!!
By Addie Irawan (FKEE)
54
include ltstdio.hgt include ltconio.hgt main
() int day,month,year printf(enter
date in format day month year) scanf (d d
d,day,month,year) printf(Dayd,
Monthd, Yeard,day,month,year) getch()
Determine the o/p
By Addie Irawan (FKEE)
55
Modify the following program for resistors that
connected in series with variable
value. include ltstdio.hgt include
ltconio.hgt main () float R1,R2,R3,R_equ p
uts(prog to determine equivalent
resistance) puts(of three resistors connected
in parallel) puts (enter three value of
resistance gtgt) scanf(f f
f,R1,R2,R3) R_equ 1/(1/R11/R21/R3) p
rintf(R1f, R2f, R3f ohms\n,
R1,R2,R3) printf(Equ resisf ohm,
R_equ) getch()
56
Program the calculation system which can
calculate the velocity acceleration using the
equation as follows v d/t a d/t2
By Addie Irawan (FKEE)
57
Ohm's law for a steady electrical current can
written as VIR where Vpotential difference
across a conductor, Icurrent in the conductor
and Rresistance of conductor. Write a program
capable of filling in the blanks in the following
table enter current and resistance value
gtgt? ? ----------------------------------------
------ V(volts) I(Amps) R(ohms) ----------
------------------------------------ ? ?
?
By Addie Irawan (FKEE)
58
CHAPTER 3
STRUCTURED PROGRAM DEVELOPMENT
By Addie Irawan (FKEE)
59
Outline
  • Problem solving
  • Problem specifics analysis
  • Requirement specification
  • Analysis
  • Design
  • Algorithm studies
  • Characteristics of a good algorithm
  • Algorithm representation
  • Guide line of pseudo code flowchart
  • Exercises
  • References

By Addie Irawan (FKEE)
60
Problem solving
Process of transforming the description of a
problem into a solution of that problem by using
our knowledge of the problem domain and by
relying on our ability to select and use
appropriate problem-solving strategies,
techniques and tools.
By Addie Irawan (FKEE)
61
Problem specifics and analysis
By Addie Irawan (FKEE)
62
  • Requirement specification
  • Understanding what the problem is ?
  • What is needed to solve it ?
  • What is solution should provide ?

By Addie Irawan (FKEE)
63
  • Analysis
  • Inputs to the problem
  • Expected output
  • Any special constraints/condition/formulas to be
    used

By Addie Irawan (FKEE)
64
  • Design
  • Method of solution
  • Algorithm sequence of finite number of
  • steps arranged in a specific logical order
  • when executed to produce solution of a
  • problem

By Addie Irawan (FKEE)
65
Case study Example
Study some maps that give distance in kilometres
and some that use miles. You and your co-workers
prefer to deal in metric measurement. Write a
program that performs the necessary conversion
By Addie Irawan (FKEE)
66
By Addie Irawan (FKEE)
67
By Addie Irawan (FKEE)
68
Why we need to study the algorithms ?
  • Describe the steps needed to perform a
    computation
  • Important for writing efficient code
  • - code that execute faster which uses less
  • memory
  • A finite number of steps for solving a
    particular
  • problem within a certain time frame
  • - a finite step by step problem solving
    techniques

By Addie Irawan (FKEE)
69
Algorithm representation
  • Easy for programmer to begin the program
  • writing on paper
  • Pseudo code
  • - a semi formal English like language
  • with a limited vocabulary can be used to
  • design describe algorithm
  • Flowchart
  • - a graph consisting of geometrical
  • shapes that are connected by flow lines

By Addie Irawan (FKEE)
70
Example Add 2 numbers
By Addie Irawan (FKEE)
71
Exercises
Program the calculation system as follows
P IV I2R V2/R Please stated
the - Requirement - Analysis -
Design representative (pseudo flowchart)
By Addie Irawan (FKEE)
72
CHAPTER 4 5
SELECTION REPETITIVE STATEMENTS
By Addie Irawan (FKEE)
73
Outline
  • Introduction
  • What is How to use
  • - if ifelse statement
  • - switch() case statement
  • - for () statement
  • - while dowhile statement
  • Exercises

By Addie Irawan (FKEE)
74
Introduction
  • For a structure program, a logical flow is
  • control by 3 basic control structure as
    follows
  • a) selection
  • b) sequential
  • c) looping
  • In sequential program a series statements is
  • executed 1-by-1 in a sequential manner.
  • Selection statements allow alternative courses
  • of action to be taken depending on the result
  • of an operation.
  • We will use if, if..else, switchcase,
  • for(), while(), dowhile(), break and
  • continue

By Addie Irawan (FKEE)
75
  • Syntax
  • if (expression)
  • statement block
  • Operation
  • - the statement block is executed if expression
    is TRUE
  • - test for ONE condition only
  • E.g.
  • a) if (k lt j)
  • mink
  • b) if (kltj)
  • mink
  • printf(k lebih kecil dari j)
  • note If the statement block has only one
    statement the braces ( ) can be excluded

By Addie Irawan (FKEE)
76
  • Syntax
  • if (expression)
  • statement block1
  • else
  • statement block2
  • Operation
  • the statement block1 is executed if expression
    is TRUE
  • (non-zero)
  • the statement block 2 is executed if expression
    is FALSE
  • (zero)
  • 2 different flow for both condition

By Addie Irawan (FKEE)
77
  • E.g.
  • a) if (k lt j)
  • mini
  • else
  • minj
  • b) if (kltj)
  • mini
  • maxj
  • else
  • minj
  • maxi

By Addie Irawan (FKEE)
78
  • Syntax
  • if (expression1)
  • statement block1
  • else if (expression2)
  • statement block2
  • else if (expression3)
  • statement block3
  • else
  • statement block4
  • Operation
  • - the statement block1 is executed if expression1
    is TRUE (non-zero)
  • - the statement block2 is executed if expression2
    is TRUE (non-zero)
  • - the statement block3 is executed if expression3
    is TRUE (non-zero)
  • - the statement block4 is executed if all
    expression are FALSE (non-zero)

By Addie Irawan (FKEE)
79
  • E.g.
  • if (option1)
  • printf(good morning\n)
  • printf(order breakfast\n)
  • else if (option2)
  • printf(order lunch\n)
  • else if (option3)
  • printf(order dinner\n)
  • else
  • printf(order nothing laa\n)

By Addie Irawan (FKEE)
80
switch statement
  • Switch statement is used when there is a multiple
  • decision to be made.
  • Normally used to replaced the if statement when
    there
  • are many routes of execution

break statement
  • Used for exit from the switch statement
  • Used to continue with the statement that follows
    the
  • closing brace of the switch statement body

By Addie Irawan (FKEE)
81
  • Syntax
  • switch (expression)
  • case ltconst1gt ltstatementgt break
  • case ltconst2gt ltstatementgt break
  • case ltconst3gt ltstatementgt break
  • default ltstatementgt break

By Addie Irawan (FKEE)
82
  • Operation (switch)
  • - only one variable is tested
  • - all branches must depend on the value of
    that
  • variable (int, long, short or char)
  • - each possible value of variable control
    single
  • branch
  • - default branch may be used to trap all
    unspecified
  • cases
  • Operation (break)
  • - passes control to the first statement
    following the
  • innermost enclosing brace

By Addie Irawan (FKEE)
83
Determine the errors in following programs
include ltstdio.hgt main ( ) int a5,b3 char
ch puts ( enter operator (, - , or /
) chgetchar( ) switch (ch) case ''
cab case '-' ca-b case ''
cab case '/' ca/b printf ( d c c
d , a,ch,b,c) getch ( )
By Addie Irawan (FKEE)
84
  • E.g.
  • switch (colour)
  • case 0
  • printf(black)
  • break /Line 1/
  • case 1
  • printf(brown)
  • break /Line 2/
  • default
  • printf(Input Error!)
  • break

By Addie Irawan (FKEE)
85
Repetitive
  • Statement executed more than 1 times (allows
  • the looping of a set of statements)
  • C gives you a choice of three types of loop
  • - for
  • - while()
  • - dowhile ()
  • All require test condition which determines if
    the
  • loop continues or not.
  • TRUE gtgt continues the loop
  • FALSE gtgt stop the loop

By Addie Irawan (FKEE)
86
  • Performs looping of instruction executions
  • Syntax
  • int i
  • for (istart valueiltend valuei)
  • statement block
  • Note start end value is in digit

By Addie Irawan (FKEE)
87
Example
include ltstdio.hgt include ltconio.hgt main()
int i for (i0ilt10i)
printf(Number i\n,i)
clrscr() Note Only digit could be used as a
counter value in for statement
By Addie Irawan (FKEE)
88
Example
include ltstdio.hgt include ltconio.hgt main()
int f for (f0flt10f4)
printf(Number d\n,f)
clrscr() Note Only digit could be used as a
counter value in for statement
By Addie Irawan (FKEE)
89
  • The while loop keeps repeating an action until
    an
  • associated test returns FALSE
  • This is useful where the programmer does not
  • know in advance how many times the loop will
    be
  • traversed.
  • Syntax
  • while (condition)
  • (statement block)

By Addie Irawan (FKEE)
90
  • Condition test at the end of loop
  • The loop will be repeated if the condition is
  • TRUE. The test condition at the bottom of the
  • loop.
  • The alternate flow will be executed at least
    once
  • Syntax
  • do
  • statement block
  • while (condition)

By Addie Irawan (FKEE)
91
Example
while Statement
include ltstdio.hgt include ltconio.hgt main()
int x1 while (xlt10) printf (
Number d\n,x) clrscr()
By Addie Irawan (FKEE)
92
Example
do...while Statement
include ltstdio.hgt include ltconio.hgt main()
int k0,n printf ( Enter a nonnegative
integer ) scanf (d,n) do n
2 k while (ngt0) printf (
The number counter d\n,k) clrscr()
By Addie Irawan (FKEE)
93
Exercises 1. Find the errors if any in each of
these statements a) if (today 7) printf (Go
to the park) b) if (today 7) c) if (today
7) d) if (today 7) if (money gt 100) printf (
dine out!) e) if (today 7) ji f) if (Today
7) ji1 k100/j g) if ( today 7)
printf ( go to the park) else
printf (go to work) h) if ( today 7) else
printf (go to work)
By Addie Irawan (FKEE)
94
Exercises 2. Find the errors if any in these
statements a) if (i gt 100) printf (Hot\n)
else printf (warm\n) else
printf (cool\n) b) if (i gt 100) printf
(Hot\n) if ( i 100 ) printf
(warm\n) else printf
(cool\n) 3. Find the errors if any in these
statements, assuming a1, b2, c3, d4 a) if (a
gt b) printf (This is an arithmetic
if_statement\n) b) if ( a gt b) printf
(This is a block if_statement\n)
other_statements . . .
By Addie Irawan (FKEE)
95
Exercises c) if (a gt b c) printf (This is a
block if_else statements\n) other_statements .
. . else 4. Determine the errors in
the following programs include
ltstdio.hgt include ltconio.hgt main ( ) int
i puts (enter value of i) scanf (d
,i) if (i5) puts (i is equal to
five) getch ( )
By Addie Irawan (FKEE)
96
Exercises 5. Rewrite the following program by
using statement (if...else) char ch switch
(ch) case 'a' printf (huruf vokal)
break case 'e' printf (huruf vokal)
break case 'i' printf (huruf vokal)
break case 'o' printf (huruf vokal)
break case 'u' printf (huruf vokal)
break default printf (huruf konsonan)
break 6. Use a switch statement to write a
program to calculate the tax based on the
following tax code income lt 1000, taxincome
x 20 percent 1000 lt income lt 2000 taxincome
x 30 percent income gt 2000 taxincome x 40
percent
By Addie Irawan (FKEE)
97
Exercises 1. Find the errors is any in each of
these statements a) for day1,3,1 b) for
(day1,daylt3,day) c) for (day10daylt20day)
d) for (day10daylt5day) e) for
(day100daylt100,day--) f) for
(day10daygt100day--) g) for (i20igt10i--)
ii3 2. Program a system which can give
deduction of 2 from the input of user (using
for statement). 3. Program a counter system
which will repeat the counter from zero when
counter result reach to 1000 (using dowhile
statement).
By Addie Irawan (FKEE)
98
CHAPTER 6
FUNCTION
By Addie Irawan (FKEE)
99
Outline
  • Introduction
  • Function type
  • Function definition
  • How to use and create function?
  • Function with returns no returns
  • What is Array
  • How to use array?
  • Array Type dimensional
  • multidimensional
  • Exercises

By Addie Irawan (FKEE)
100
bridges function...
By Addie Irawan (FKEE)
101
  • Introduction
  • C program made of one or more function, one and
    only one of them must be named main.
  • Execution of program always starts and ends with
    main, BUT it can call other function.
  • Function are called from any part of a program
    and can split into more manageable tasks.
  • It also can be independently tested.

102
  • Introduction (cont'd)
  • Other standard functions such as printf( ),
  • scanf( ),gets( ),
  • Function processes the i/ps in a way dictated by
    its function and provides some o/ps.
  • Figure 6.1 shows a details of writing and using
    functions.In this chapter, we focus on functions
    that are defined by the programmer.
  • Figure 6.2 shows a main function calling several
    subfunctions (or modules).

103
Introduction
Categories of functions
By Addie Irawan (FKEE)
104
Introduction
Hierarchical decomposition of a program
By Addie Irawan (FKEE)
105
Function Type The general syntax of a
function type_def function_name (parameter
list) (statement block) type_def
data type of function compiler will
assume int if no coded. use void if
the function return nothing function_name
similar to the variable condition parameter
list parameter that being send to the
function as an input. use void if the
function return nothing
By Addie Irawan (FKEE)
106
  • Function definition
  • Consists of 2 parts
  • - Function header
  • - Function body
  • Function header
  • - return type ( C will assume an integer as a
  • return type if it is not coded)
  • - function name
  • - formal parameter list
  • - no semicolon at the end
  • - use void if the function return nothing

By Addie Irawan (FKEE)
107
  • Function definition
  • Function body
  • - begin and must end with curly braces
  • - contain decalaration and statements for the
  • function
  • - starts with a local definitions
  • - terminating with a return statement if it
  • has a return type

By Addie Irawan (FKEE)
108
Example 1
include ltstdio.hgt (FUNCTION PROTOTYPE OR
DECLARATION) main ( ) A ( ) B ( ) A(
) (LOCAL declaration) (statement
blocks) void B( ) (LOCAL declaration)
(statement blocks)
By Addie Irawan (FKEE)
109
  • Parameter passing
  • Data types and names of parameter passed into
    function declared in the function header
  • It can passed either value (passing by value) OR
    as pointers (passing by reference)
  • passing by value
  • - sending a copy into a function
  • - NOT possible to change value of a variable

By Addie Irawan (FKEE)
110
Example 2
int a,b,c int main (void) int d,e,f funct
ion1 (d,e,f) function1 (int g, int h, int
i) int j,k jah
By Addie Irawan (FKEE)
111
  • Return value
  • return statement returns a single value from a
    function to the calling routine.
  • Figure 6.3 illustrated the return statement.
  • no return statements means execution returns
    automatically to the calling routine (after the
    final statement within the function).
  • general syntax return statement
  • return or
  • return expression or
  • return (expression) for fixed value of
    expression

By Addie Irawan (FKEE)
112
Input parameters
return value
Illustration of return statement
By Addie Irawan (FKEE)
113
Functions with returns Identify type_def,
function_name and parameter list. float
kira_luas (int radius) /receive parameter for
radius/ float area area(22.0/7.0)radiusra
dius return area /return area value with data
type float/
By Addie Irawan (FKEE)
114
Summarizes
Does not pass arguments
Does pass arguments
int main ( ) fun ( ) void fun ( )
int main ( ) fun (23 ) void fun (int i
)
No return
int main ( ) x fun ( ) int fun ( )
return 20
int main ( ) x fun (3 ) int fun (int x
) return (xx)
With a return
By Addie Irawan (FKEE)
115
Function with No returns include
ltstdio.hgt include ltconio.hgt void
additional(void) void main (void) funny() p
rintf("Welcome to KUKTEM!") getch()
void additonal (void) printf("Have a nice
day\n")
By Addie Irawan (FKEE)
116
Function with returns a) Developing a program
for the formulation as follows
By Addie Irawan (FKEE)
117
include ltstdio.hgt include ltconio.hgt int
factorial(int n) int main(void) int n, r,
c printf("Enter total of components (n)\n")
scanf("d", n) printf("Enter number of
components selected (r)\n") scanf("d", r)
if (r lt n) // Formula calculation
(surface) c factorial(n)/(factorial(r)factor
ial(n-r)) printf("The number of combinations
is d\n",c) else printf("Components
selected cannot exceed total number\n")
return(0) // Function of factorial formula
calculation int factorial (int n) int i,
product 1 for (i n i gt 1 --i) product
i return (product)
By Addie Irawan (FKEE)
118
CHAPTER 7 8
String, Array File I/O
By Addie Irawan (FKEE)
119
Array
  • Syntax
  • an d0,d1,dn-1
  • a10 0,1,2,3.9 ? example a4 3
  • Why we use array?
  • - Easy for memory allocation and saving
  • data
  • Programmer can put in any type of data
  • in the defined array memory such as
  • integer, floating point, character etc.

By Addie Irawan (FKEE)
120
Example of memory allocation for 10-elements array
a0 a1 a2 a3 a4 a5 a6 a7 a8 a9
a10
0
Name of array
1
Value in memory location
2
3
4
5
6
7
8
9
By Addie Irawan (FKEE)
121
Array Type
  • Single array an
  • 2-D array anm
  • Multi-dimantion array anmv
  • String array char an

122
Example
include ltstdio.hgt include ltconio.hgt main (
) int a6 1, 2, 223, 56, 889, 90, i
for(i0ilt5i) printf ("d\t",ai)
printf("\n") for(i1ilt5i)
printf ("d\n",ai) return(0)
123
Example
include ltstdio.hgt include ltconio.hgt main (
) char a4 D', 'A', R', K' int
i for(i0ilt3i) printf("c\t",ai)
printf("\n") expression()
return(0)
124
Example
include ltstdio.hgt include ltconio.hgt void
expression(void) main( ) char a4 'G',
'A', 'M', 'E' int i for(i0ilt3i)
printf("c\t",ai) printf("\n")
expression() return(0) void
expression (void) printf("O\tV\tE\tR")
125
2-D Array
Value in memory location
an0 an1 an2 an2
a0m a1m a2m a3m
A
0
9
4
2
8
4
2
33
30
7
4
5
7
7
4
n 0,1,2,3 m 0,1,2,3
By Addie Irawan (FKEE)
126
String
  • library ltstring.hgt, ltstdlib.hgt
  • I/O Statement
  • - printf(Topic s\n,string_var)
  • - scanf(s,j)
  • String function
  • - strcpy(name, Ali)
  • - strlen(computer)
  • Dynamic memory allocation

By Addie Irawan (FKEE)
127
Example
include ltstdio.hgt include ltstring.hgt include
ltconio.hgt main ( ) char name
strcpy(name,Siti) printf( My name is
c\n, name) strlen(name) return(0)
Compile result My name is Siti 4
By Addie Irawan (FKEE)
128
Example
include ltstdio.hgt include ltstdlib.hgt include
ltconio.hgt main ( ) char string1 int
array_of_nums int str_siz, num_nums, i
printf("Enter string length and string ")
scanf("d", str_siz) string1
(char)calloc(str_siz,sizeof(char))
scanf("s",string1) printf("\nHow many
numbers?\n") scanf("d",num_nums)
array_of_nums (int)calloc(num_nums,
sizeof(int)) array_of_nums0 5 for(i
1 i ltnum_nums i) array_of_numsi
array_of_numsi-1i return (0)
By Addie Irawan (FKEE)
129
Compile result Enter string length and string
9 addie How many numbers
4 Illustration
string1
d
d
i
e
\0
\0
\0
a
\0
array_of_nums
5
5
10
30
By Addie Irawan (FKEE)
130
File I/O
  • Saving data into file ( .txt or .dat)
  • Using fopen, fclose, FILE, fprintf,
  • fscanf.

Define
Open file
Write
Close file
By Addie Irawan (FKEE)
131
Example
By Addie Irawan (FKEE)
132
Example
include ltstdio.hgt include ltconio.hgt define
STRZ 80 int main(void) char in_nameSTRZ,
out_nameSTRZ FILE inp, outp char ch
int status printf("Enter name of file \n")
for(scanf("s",in_name) (inp
fopen(in_name,"r")) NULL
scanf("s",in_name)) printf("Cannot open
s for input \n", in_name)
printf("Re-enter the file name \n")
printf("Enter the name of backup file \n")
for(scanf("s",out_name) (outp
fopen(out_name,"w")) NULL
scanf("s",out_name)) printf("Cannot open
s for input \n", out_name)
printf("Re-enter the file name \n")
//Backup file copy program for(status
fscanf(inp,"c",ch) status ! EOF status
fscanf(inp,"c",ch)) fprintf(outp,"c",
ch) fclose(outp) fclose(outp)
printf("Copied s to s. \n", in_name,
out_name) return(0)
By Addie Irawan (FKEE)
133
CHAPTER 9
Software Hardware Interfaces
By Addie Irawan (FKEE)
Write a Comment
User Comments (0)
About PowerShow.com