Computer Programming 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

ITC 112

Computer Programming 1

Programming
2
COMPUTER

▰ Electronic device that can manipulate data


▰ is a device that computes, especially a programmable electronic machine that
performs high-speed mathematical or logical operations or that assembles,
stores, correlates, or otherwise processes information.
▰ is a device that accepts data and manipulates it
▰ is a machine for manipulating data according to a list of instructions.
▰ the term usually refers to an electronic device that can perform automatically a
series of tasks according to a precise set of instructions
3
Problem Solving Concept with 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

Input Device Output Device

Main Memory

Secondary Memory 4
COMPONENTS OF COMPUTER SYSTEMS

▰ Hardware - refers to the part of the computer that you


can touch (tangible). It consists of interconnected
devices that control everything the computer does
▰ Peopleware - refers to the person who uses the
computer.
▰ Data - refers to the raw facts the computer can
manipulate. Data can consist of letters, numbers,
sounds, or images
5
Components (cont…)

▰ Software - refers to set of electronic instructions that


tell the hardware what to do. These sets of
instructions are also known as programs, and each of
them has a specific purpose
▰ In computer science software are classified into :
 System software
 Application software

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

▰ Programming language is divided into


three categories:
 Machine Language
 Assembly/Low-Level Language
 High-Level Language

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

▰ It is a low level programming language.


▰ To make it easier for the programmer, the strings of
0’s and 1’s are replaced into instructions which
resembles English language to represent computer
operation element
▰ So easier to understand and written
LOAD rate
▰ E.g MULT
STOR
hour
wages

11
Assembly Language

▰ Nevertheless, need language translator called


Assembler to change Assembly Language to
Machine Code for execution purpose
▰ Assembler is a type of system software, that we use
to translate our assembly code into binary code
(understand by computer.

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

▰ Compiler: A compiler is program that converts the instruction of a high


level language into machine language as a whole. A program written in
high level language is called source program. After the source program
is converted into machine language by the compiler, it is called an object
program.
▰ Interpreter: An interpreter is a program that converts one statement of a
program at a time. It executes this statement before translating the next
statement of the source program. If there is an error in the statement, the
interpreter will stop working and displays an error message.
▰ The advantage of interpreters over compilers is that an error is found
immediately. So the programmer can make corrections during program
development.
14
PROGRAMMING CYCLE

People make decisions


every day that affect
their lives. Documentation
Defining the
problem

Checking out Planning the


the program solution

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

▰ Algorithmic Solution – problem can be solved with a


series of actions. So once we eliminate the
alternatives and have chosen one best methods,
these steps are called algorithm.
▰ Heuristic Solution – Problem with solutions require
reasoning built on knowledge and experience, and a
process of trial and error. Usually use in the Artificial
Intelligent software or Expert system software.
17
Problem Solving with Computer

▰ Solution means the instructions that must follow to


produce the best results.
▰ Results mean the outcome or completed computer –
assisted answer.
▰ Program means the set of instructions that make up
the solution after they have been coded into a
particular computer language.

18
Problem Solving Concept For Computer

▰ Problem that can be solve with


computers generally consist of three:
 Computational – problem with mathematical processing
 Logical – problem involving with relational or logical
processing. This is kind of processing involve in decision
making.
 Repetitive – problem involving repeating a set of
mathematical or logical instructions.

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

Problem: Prepare a Breakfast

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…

▰ What is the connection


between these real life
processes and
algorithm?

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

Flow Direction indicator Flow of the direction

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.

Decision Usually has one entrance and two exit.

Process Module Task that process in different place, module

The number of loop execute will start with A, counter incremented by S,


Preparation
and until the end value B

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…

Are the steps in the


algorithm discussed
earlier specific enough
to be executed by
computer?

35
Problem Solving Process

Input Process Output

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.

Input Process Output


• Quantity Price
• Price_per_kg Price = Quantity * Price_per_kg

37
Flowchart: Calculate Price of Apples
Start

Input
Quantity

Input
Price_per_kg

Price = Quantity * Price_per_kg

Output
Price

End
38
C# Program: Calculate Price of Apples

static void Main(string[] args) Start


{
Input
Console.Write("Enter the quantity of Apple in Kg.: "); Quantity
quantityOfApple = Int32.Parse(Console.ReadLine());
Console.Write("Enter the price of Apple in Kg.: "); Input
priceOfApple = double.Parse(Console.ReadLine()); Price_per_kg
Amount = quantityOfApple * priceOfApple;
Console.Write("Total Amount is {0}", Amount);
} Price = Quantity * Price_per_kg

Output
Price

End
39
C# Program: Calculate Price of Apples

It’s not complete!


Declare the
static void Main(string[] args) variables…
{

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());
Amount = quantityOfApple * priceOfApple;
Console.Write("Total Amount is {0}", Amount);
}

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.

Input Process Output


• Entry_time Charge
• Exit_time
????

43
Flowchart: Calculate Car Park Charge

Start

Input Entry_time
Input Exit_time

Period = Exit_time – Entry_time

No Yes
Charge =20 If Charge = 20+(Period-3) * 5
Period > 3

Output
Charge

End

44
C# Program: Calculate Car Park Charge

static void Main(string[] args)


Start
{
int EntryTime, ExitTime, Period, Charge;
Input Entry_time
Console.Write("Entry Time: "); Input Exit_time
EntryTime = Int32.Parse(Console.ReadLine());
Console.Write("Exit Time: ");
ExitTime= Int32.Parse(Console.ReadLine());
Period = ExitTime - EntryTime; Period = Exit_time – Entry_time
if (Period > 3)
Charge = 20 + (Period-3) * 5;
else No Yes
Charge = 20; Charge =20 If Charge = 20+(Period-3) * 5
Period > 3
Console.Write("Total Charge is {0}", Charge);
Console.ReadLine();
Output
} Charge

End

45
C# Program: Calculate Car Park Charge
static void Main(string[] args)
{
int EntryTime, ExitTime, Period, Charge;

Console.Write("Entry Time: ");


EntryTime = Int32.Parse(Console.ReadLine());
Console.Write("Exit Time: ");
ExitTime= Int32.Parse(Console.ReadLine());
Period = ExitTime - EntryTime;

}
if (Period > 3)
Charge = 20 + (Period-3) * 5; Chapter 7
else
Charge = 20;

Console.Write("Total Charge is {0}", Charge);


Console.ReadLine();

}
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

▰ Identify the input and output of the problem.


▰ If necessary, use ‘Divide & Conquer’ strategy to
decompose the problem into smaller and
manageable sub problems. Decompose the
problem until all the sub problems can be solved to
get the required results
▰ For each sub problem, identify and list out the steps
involved in solving it
48
Something to ponder

What might be the


disadvantage of
flowchart?

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

You might also like