Computer Programming Chapter2part1
Computer Programming Chapter2part1
Programming
Chapter 2 Part 2 : Structure of C
Objective
• At the end of this chapter, student should be able to understand and
apply
• Understand and implement the basic structure of computer programming.
• Write a computer program using C programming language.
• Convert algorithm into computer program.
Table of contents
Data type
Variable declarations
Program Development
Environment
Program Development Environment
Program
Edit Compile Execute
Phase 1 : Edit
❑ Programmer types or creates
program in an editor.
❑ Makes corrections if
necessary.
❑ Saves or stores program on EDITOR
disk such as C:\
❑ Editor or text editor is a type
of program used for editing
plain text files
❑ Note pad
❑ Dev C ++ : editor + compiler
Phase 2 : Compile
❑ Programmer gives command to compile the
program.
❑ Preprocessor program executes automatically and
process the program code.
•
Phase 2 : Compile (cont)
❑ The object code will be only created if the translation
process into machine code is successful.
❑ Otherwise, if unsuccessful, error messages will be displayed
in the compiling dialogue box.
❑ Programmer must fix all the errors before proceed to the
next phase.
❑ The process of correcting errors is called debugging.
Phase 3 : Execute
❑ CPU takes each instructions and executes it.
❑ Results or output will be displayed.
Common Programming Error
• During compile
Syntax Errors
Run-time Errors
Logic Errors
Syntax Errors
• Error occurred during compilation
normally due to syntax problem
• Example :
❑Misplaced else.
❑ Declaration sytanx error
❑ Undefined symbol ‘_main’ in
module.
❑ Statement missing in function
main()
Logic Errors
❑Error occurred due to inappropriate output.
❑Programming mistake.
❑Not detected during compilation.
Run-time Errors
❑Error occurred due to wrong user input.
❑User’s mistake.
❑System would either display alert message or hang.
Basic Syntax of
Programming
C language elements
Pre-processor
Comment Main function
directive
Main function
Identifiers/ variables
C statement
Preprocessor Directive
❑ Utility program which link files from compiler library to the program code.
❑ Must be included in the first line of a computer program.
❑ Must be started with the symbol #, otherwise syntax errors will be occurred.
❑ Two types of common preprocessor directive: #include and #define.
Example
Format:
#include <stdio.h>
#include <header file> #include <conio.h>
#include constant_name constant_value #define Max 100
Preprocessor Directive (cont)
include define
conio.h clrscr(),putch().getc().dll
math.h sqrt(),pow(), log(),dll
Function
To increase To provide To
As a future
program additional document
reference
readability information program
Program body and Main function
❑ The part in which the program code will be started to execute.
❑ Consists of main function, C statements and identifiers.
❑ Use { to start the program code and } to end the
program code.
Format:
int main () // main function
{ //identifiers
//C statements }
C statement
❑Instructions to be executed by computers
❑Every statements must be ended with semicolon
Types
Function Declaration Input/output Control Compound
statement statement statement statement statement
Identifiers & Variables
Reserve word
case default switch break
❑Standard/special word in standard library
for continue float double
❑Contain special meaning understood by compiler
❑Must be written in small case return while if do int
Example
❑ void : Refer to the function that will not return any value
❑ int : The acronym for integer
Identifier
❑Representing particular name in programming
❑Store values to be used in programming
❑Refers to the storage in computer
❑Space in memory USED to Store data values/result/output
constant
Type
variable
Identifier - Rules
Identifiers name can only consists of name, number and underscore Fsktm, 123, _
Valid identifiers
WHY?
Invalid identifiers
8Century BIT 1033 Two*four
‘Sixsense’ void
WHY?
WHY? WHY?
Identifier - constant
❑Name which used to store data value
X X
❑Space in memory USED to Store data values/result/output
❑Refer to name of one cell in computer storage
❑Constant value is fixed
❑Constant name ? Follow identifiers rules Example :
Declaration example
Declaration of a variable number of integer data type. int number; int fsktm_uthm; int uthm2023;
Declaration of a variable weight of floating point data type. float cpa; float gpa_Sem1; float gpa2023;
Declaration of a variable alphabet char name; char firstName; char Name2nd;
of character data type.
HOW to STORE value to variable
Interactive Initialization
i. Salary of an employee
ii. Student’s mark for programming subject
iii. ATM pin number
iv. Phone number
v. Price of one item
vi. Bus seat number
vii. Student name
Lab Exercise 2a : Q3
Based on the following problem, determine the appropriate variables can be declared:
1) Given the value of x is 10 and a is 12, find the result of the following equation:
y = 2x + a – 6
2) Mrs Sue needs to determine her students grade for programming subject based on the mark scored
during final examination. The ‘A’ grade will be given if the mark scored is between 85 to 100. If a
student has scored 90 marks, what is the grade should Mrs Sue gives to the student?
3) Uncle John wants to buy 5 tins of paint from Loly’s shop. The price of each tin of the paint is RM
15.60. Calculate the price which Uncle John have to pay for all the tin of paints he bought.
Lab/Class exercise 2a : Q4
Submit during Lab session
Instructions : Answer all exercise. Based on the following problem
1. Determine the appropriate variables can be declared,
2. write a declaration
2. Assign value to variable
• Question
1. A box has height, width and length. Calculate the volume of a box.
2. Given the following formula, calculate the porosity of rock:
porosity = (pure volume /total rock volume) x 100%
3. Compute and display the total cost of apples given the number of pounds of apples purchased and
the cost of apples per pound.
C language elements example