Computer Programming 1
Computer Programming 1
Computer Programming 1
Computer Programming 1
Programming
2
COMPUTER
▰ Computer Fundamental
CPU – where all the processing take place
Memory – store all data currently processed, before program can be
execute, program must be loaded into memory
Input Device – Device use to input data to be processed into
computer
Output Device – Device use to display processed data (output)
Secondary Memory – Store permanent data (can be as output
device. CPU
Main Memory
Secondary Memory 4
COMPONENTS OF COMPUTER SYSTEMS
6
Software
▰ System software : software manages the
computer and its peripheral devices
(hardware)
E.g. Operating systems(OS)
Text editor
Preprocessor
Language translator
Linker
Loader
7
Software
▰ Application software : performs specific tasks
▰ E.g word processor, desktop publishing software,
spreadsheets, database, graphics, communication,
program perform a specific task such as
accounting, scientific, engineering, education etc.
8
Programming Languages
9
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
10
Assembly Language
11
Assembly Language
12
High-Level Language
▰ Example of high level programming language are COBOL, Fortran,
PASCAL, C - Language, C++, C#, Java, Visual Basic etc
▰ One instruction of it, consist several instructions in machine level or
assembly language.
▻ E.g. Grosspay := Hourswork * Payrate;
▰ Before it can be executed by computer, the program must be changed to
machine code before executed. For high level language compiler will
translate the coding into assembly code and binary code.
▰ Compiler is a type of system software that we use to compile our C
code.
13
COMPILER AND INTERPRETER
Coding the
solution
15
PROGRAMMING CYCLE
▰ Generally there are five (5) steps in Programming Cycle.
o Defining the problem – defining what has to be done or
what problem has to be solved
o Planning the solution – select the best method for solving
the problem
o Coding the solution – prepare the set of instructions for
the computer to execute
o Checking out the program – perform debugging and
testing the program using representative input data
o Documentation – write up the full specifications for other
program users
16
Types of Problems
18
Problem Solving Concept For Computer
19
Algorithm
▰ A specific and step-by-step set of instructions for carrying
out a procedure or solving a problem, usually with the
requirement that the procedure terminate at some point
▰ 2 types of algorithm representation will be explained:
Flowchart
Pseudocode
▰ Structured Method (will be explained later)
A method of designing problem solution
20
Algorithm
Knowledge:
Understand algorithm representation using flowchart
and pseudocode
Skill:
Map problem to solution in flowchart and pseudocode
forms
21
Algorithms
▰ List of instruction for carrying out some process step by step
▰ Organizing the solution where programmer develop a set of
instruction for the computer and this is call algorithm.
▰ Pseudo code may be use, it is close to the actual programming
language that the programmer will used later.
▰ To complete it, the programmer will write another separate set of
instruction (coding/program) that will be understand by the
computer.
▰ Instead of Pseudo code, we can also represent the set of
instruction in term of Flowchart ( a graphic presentation of the
algorithm.
22
‘Divide and Conquer’ Strategy in Algorithm
1. Start
2. Prepare a Breakfast
3. End
23
“Divide and Conquer” Strategy in Algorithm
1. Start
2. Prepare a Breakfast
2.1 Prepare a tuna sandwich
2.2 Prepare some chips
2.3 Make a cup of coffee
3. End
24
“Divide and Conquer” Strategy in Algorithm
1. Start
2. Prepare a Breakfast
2.1 Prepare a tuna sandwich
2.1.1 Take 2 slices of bread
2.1.2 Prepare tuna paste
2.2 Prepare some chips
2.3 Make a cup of coffee
3. End
25
“Divide and Conquer” Strategy in Algorithm
1. Start
2. Prepare a Breakfast
2.1 Prepare a tuna sandwich
2.1.1 Take 2 slices of bread
2.1.2 Prepare tuna paste
2.2 Prepare some chips
2.2.1 Cut potatoes into slices
2.2.2 Fry the potatoes
2.3 Make a cup of coffee
3. End
26
“Divide and Conquer” Strategy in Algorithm
1. Start
2. Prepare a Breakfast
2.1. Prepare a tuna sandwich
2.1.1 Take 2 slices of bread
2.1.2 Prepare tuna paste
2.2. Prepare some chips
2.2.1 Cut potatoes into slices
2.2.2 Fry the potatoes
2.3. Make a cup of coffee
2.3.1 Boil water
2.3.2 Add water with sugar and coffee
3. End
27
Something to ponder…
28
Pseudocode
▰An outline of a program, written in a form that can easily be
converted into real programming statements. It resembles the
actual program that will be implemented later. However, it
cannot be compiled nor executed.
▰Pseudocode normally codes the following actions:
Initialization of variables
Assignment of values to the variables
Arithmetic operations
Relational operations
29
Example of Pseudocode
1. Start
2. Read quantity
3. Read price_per_kg
4. price = quantity * price_per_kg
5. Print price
6. End
30
Flowchart
Flowchart represents
algorithm graphically. It is
intended for communication
and documentation
31
Algorithm-Basic symbols of flowchart
FLOWCHART SYMBOL EXPLANATION
A start and an end of a module. For some end for a main module and exit
Terminal
for other module
Processing Processing block, for the calculation, and other step of instructions.
Input / Output (I/O) Input and output data to from the computer memory.
Flowchart can be sectioned, the connector will show where the section
Connector on page off page
continue
32
Flowchart – Other Syntax
Symbol Semantic
Function call
Magnetic Disc
Stored Data
Document/File
Multiple Document/File
33
Flowchart-(ex.: from previous)
Algorithm Flowchart Algorithm Flowchart
Main Module Read Module
1. Repeat start 1000 start
Process Read 1. Read Hours, Payrate
Read
Process Calc 2. Exit
Read
Process Print Calc Hours, Payrate
Until no more
employee Print
exit
2. End
false No more
employee
True
Algorithm end Algorithm Flowchart
Calc Modul Print Module
2000 start 3000
start
1. GrossPay = Hour * 1. Print Pay
payrate 2. Exit
2. Exit
GrossPay = Hours * Payrate Print
GrossPay
exit exit
34
Something to ponder…
35
Problem Solving Process
36
Problem Solving Process
Calculate and display the price of a number of
apples if the quantity in kg and price per kg are
given.
37
Flowchart: Calculate Price of Apples
Start
Input
Quantity
Input
Price_per_kg
Output
Price
End
38
C# Program: Calculate Price of Apples
Output
Price
End
39
C# Program: Calculate Price of Apples
40
C# Program: Calculate Price of Apples
static void Main(string[] args) Start
{
int quantityOfApple; Declaration
double priceOfApple;
double Amount;
Console.Write("Enter the quantity of Apple in Kg.: ");
quantityOfApple = Int32.Parse(Console.ReadLine());
Console.Write("Enter the price of Apple in Kg.: ");
priceOfApple = double.Parse(Console.ReadLine());
} Input
Amount = quantityOfApple * priceOfApple; Process
Console.Write("Total Amount is {0}", Amount);
} End Output
41
C# Program: Calculate Price of Apples
static void Main(string[] args)
{
int quantityOfApple; Chapter 4
double priceOfApple;
double Amount;
Console.Write("Enter the quantity of Apple in Kg.: ");
quantityOfApple = Int32.Parse(Console.ReadLine());
Console.Write("Enter the price of Apple in Kg.: ");
priceOfApple = double.Parse(Console.ReadLine());
} Chapter 6
Amount = quantityOfApple * priceOfApple; Chapter 5
Console.Write("Total Amount is {0}", Amount);
}
Chapter 6
42
Example 2
A car park has the following charges:
The 1st three hours costs ₱20.00. The subsequent
hours cost ₱5.00 per hour. Write an algorithm
based on a vehicle’s entry and exit time.
43
Flowchart: Calculate Car Park Charge
Start
Input Entry_time
Input Exit_time
No Yes
Charge =20 If Charge = 20+(Period-3) * 5
Period > 3
Output
Charge
End
44
C# Program: Calculate Car Park Charge
End
45
C# Program: Calculate Car Park Charge
static void Main(string[] args)
{
int EntryTime, ExitTime, Period, Charge;
}
if (Period > 3)
Charge = 20 + (Period-3) * 5; Chapter 7
else
Charge = 20;
}
46
Example 3
Write a program to calculate the average
mark of three ITC112’s students.
Input Process Output
• Mark A
???? Average_mark
• Mark B
• Mark C
THINK!!
47
Algorithm Development Guidelines
49
CFlow Demonstration
50
End of Lecture 2
Yes !! That’s all?
What’s next???
INTRODUCTION TO
C# on the way …
51
THANKS!
Any questions?
You can find me at
delfirome@yahoo.com