Anda di halaman 1dari 16

How to design efficient code that works?

Algorithm: a process or set of rules used for


calculation or problem-solving, esp. with a
computer.

Program: a series of coded instructions to


control the operation of a computer or other
machine.
Problem: Find the greatest
common divisor (GCD) of two
integers, m and n.
int gcd(int m, int n) {
while( m > 0 ) {
Euclid's Algorithm: if( n > m ) {
while m is greater than zero }
int t = m; m = n; n = t;

If n is greater than m m -= n;
swap m and n. Subtract n from m. }
return n;
n is the GCD }
What is Algorithm?
a clearly specified set of simple instructions to be followed
to solve a problem

Data structures
Methods of organizing data
Program = algorithms + data structures

Problem Algorithm Data Structures


input
Problem : Sort N numbers in an array
Solution (algorithm) : use bubble sort technique
Data structure: array
Steps :
1. Read into array
2. Scan pairs from left to right, and swap if out of
order
8 , 6 , 2 , 3 -- > 6 ,8 , 2, 3 -- > 6 , 2, 8 ,3 -- > 6, 2 , 3 ,8
3. Repeat scans until no more changes
6 ,2 , 3 , 8 -- > 2 ,6 , 3, 8 -- > 2 , 3, 6 ,8 -- > 2, 3 , 6 ,8
Result (Time):
1) Seconds with 10 numbers (Just fine)
2) Days with 1 million numbers (No good)
Problem : Sort N numbers in an array
2 Solutions (algorithms)
1. Use bubblesort (array structure)
2. Use Heapsort (tree structure)
Input Bubblesort Heapsort
10 2 seconds 5 seconds
500 100 10 second
seconds
500,000 1,000,000 3000
seconds seconds
trilions 300 years 6 days

Which one is better ?


Write faster code
Write code that saves memory
Always faster code ???
2 situations
1) slow code is ok
Eg: Direct deposit of pay cheque
Time : 1 hour (from time cheque is entered)
Result: customer just thinks cheque hasnt cleared yet
Solution: slower code just fine (maybe simple code is used)
2) Slow code is not ok
Eg: customer transfers money from online account
Time: takes 15 minutes
Result: customer panics
Solution : faster code is needed
An algorithm is correct
If, for every input instance, it halts with the correct output
Incorrect algorithms
Might not halt at all on some input instances
Might halt with other than the desired answer

Input (N) Algo 1 (time) Algo 2 (time)


100 2 seconds 10 seconds
1000 10 seconds 100 seconds
10000 20 seconds 35000 seconds
100000 Error 6 hours

Which one should we choose?


Choose only right and correct algorithms and
data structures in your program!
Then choose faster and efficient algorithms
You can write code!
If need a brush up
Read online programming notes
Read programming text book
Get a personal tutor
Practice, practice and practice
To solve a problem
When you are given a problem, what should
you do?
From the problem, you need to identify:
Input
Processes
Output
Calculate the sum of two integer numbers.

Input ? Process ? Output?


Universiti Pendidikan Sultan Idris has 150 staffs
consisting academic and a non academic
groups. Each staff is given an option to select
either a KWSP scheme or a PENSION scheme.
Display the details of each staff information
and the total number of academic staffs and
non academic staffs that have selected a KWSP
scheme and a PENSION scheme. Use an array to
solve this problem.
UPSI swimming club is going to organize a swimming
competition to select the best swimmer to represent
UPSI in SUKMA 2020. 12 candidates have been
chosen to participate in this competition. You as a
programmer is required to write a program that will
help the club to determine the fastest swimmer to
represent UPSI in SUKMA. The output must show the
name and id of the swimmer and the time that
he/she achieved.
Permata Child Care center imposed charges for
child-care services based on the baby or the
childs age. Charges for the baby 1-12 months
old and 13-24 months old are RM250 and
RM220 per month respectively. On the other
hand, if the child is above 2 years old to 4
years olds, the charge is RM180 per month
and RM150 for the child aged 4 years old and
above to 6 years old. Determine the charge
that needs to pay by the parent for the service.
ABC Finance Company is planning to offer a credit card facility to the
customers who fulfill the criterias which are monthly income must be at
least RM5000 and the existing financial commitment is less than 25% of the
monthly income. For qualified customers, there are three type of credit cards
offered; platinum, gold and silver. Table 1 shows the minimum requirement
for each type of card and its total credit limit respectively.
Type Requirement(Minimum monthly income) Credit Limit

Platinum(P) RM40,000 RM25,000

Gold(G) RM15,000 RM10,000

Silver(S) RM5,000 RM3,000

Write a program that will let the user to key in his/her data for the credit
card application. If the application is successful, a message Tahniah!
Permohonan Anda Berjaya will be displayed at the computer screen and
then the program will write his/her record which consists of the IC_num,
name, Card-type and Credit_limit fields into the fail name Credit.dat. A
message Maaf, permohonan anda tidak berjaya will be displayed if the
application is rejected.

Anda mungkin juga menyukai