Programming 1
Guntur Budi Herwanto
gunturbudi@ugm.ac.id
Hello
•Lecturers:
•Guntur Budi H (Before Mid)
•Mhd Reza Pulungan (After Mid)
Google Classroom Code
7u00v2
Join in Netacad
ugm.id/cpaprg19
Course Companion: CPA C++
Before we Start
1 5 6 3 9 2 4
What is this course about
INTENDED FOR YOU
WITH LITTLE OR NO
“PROGRAMMING”
EXPERIENCE.
01
LEARN “ALGORITHM“
THE ROLE
COMPUTATION CAN
PLAY IN SOLVING
PROBLEMS
02
WRITING CODE
USING A
“PROGRAMMING
LANGUAGE”
03
Why C++ ?
• You can’t learn to program without a programming
language
• The purpose of a programming language is to allow
you to express your ideas in code
• C++ is the language that most directly allows you to
express ideas from the largest number of application
areas
• C++ is the most widely used language in engineering
areas (http://www.stroustrup.com/applications.html)
Why C++
C++ is precisely and
comprehensively
defined by an ISO
standard
C++ is available on
almost all kinds of
computers
Programming concepts
that you learn using C++
can be used fairly
directly in other
languages
Including C, Java, C#, and (less
directly) Fortran
Course Outcome
1. Have knowledge about the importance of algorithms and data structures in
solving problems
2. Have knowledge about components in algorithms and can construct
algorithms to solve simple problems.
3. Have knowledge about data structures and C++ programming language.
4. Have knowledge about data types for array and records / struct and can
implement them in a computer program.
5. Have knowledge about modular programming and can implement it in a
computer program.
6. Be able to explain and competent in how to implement sorting and searching
algorithms.
7. Have knowledge about pointer data type and can implement it in a computer
program.
8. Be able and competent in solving more complex programming problems.
Assesment
CO1: Problem 1 in mid-
term exam (5%) and
exercise 1 (5%) - 10%
CO2: Problem 2 in mid-
term exam (5%) and
exercise 2 (5%) - 10%
CO3: Problem 3 in mid-
term exam (5%); problem 4
in mid-term exam (5%);
assignment 1: make an
algorithm and computer
program (5%); and exercise
3 (5%) - 20%
CO4: Problem 5 in mid-
term exam (5%); problem 1
in final exam (5%) and
exercise 4 (5%) - 15%
CO5: Problem 2 in final
exam (5%); assignment 2:
make a function and
recursive (5%); and
exercise 5 (5%) - 15%
CO6: Problem 3 in final
exam (5%) and exercise 6
(5%) - 10%
CO7: Problem 4 in final
exam (5%) and exercise 7
(5%) - 10%
CO8: Problem 5 in final
exam (5%) and assignment
3: make a program based
on a real-life problem (5%)
- 10%
Main Reference
C++ PROGRAMMING :
FROM PROBLEM ANALYSIS TO
PROGRAM DESIGN
SIXTH EDITION
D.S. MALIK
Course Topics
Introduction Algorithm
Data Type &
Operator
Control
Structure –
Condition
Control
Structure –
Repetition
Array Struct Function
Sorting &
Searching
Pointer
Why programming?
Our civilization runs on
software
Most engineering activities involve software
Note: most programs do not run on things that look like a
PC (a screen, a keyboard, a box under the table)
14
Phones
• Chat
• Social Media
• Payment
• Security
• Call
• SMS
PC / Tablet / Workstation
• Operating System
• Computer
Program
Energy
• Control
• Monitoring
• Analysis
• Design
• Communications
• Visualization
• Manufacturing
Aircraft
• Communication
• Control
• Display
Ships
• Design
• Construction
• Management
19
Just about everywhere
http://www.stroustrup.com/applications.html
Algorithm
• An unambiguous specification of how to solve a class of problems.
Algorithms can perform calculation, data processing and automated
reasoning tasks.
• Algorithms are essential to the way computers process data.
• Algorithms can be expressed in many kinds of notation, including
natural languages, pseudocode, flowcharts, or programming
languages.
Algorithm Design
1. Problem definition
2. Development of a model
3. Specification of algorithm
4. Designing an algorithm
5. Checking the correctness of algorithm
6. Analysis of algorithm
7. Implementation of algorithm
8. Program testing
9. Documentation preparation
Example #1
Find the largest number in a list of numbers of random order
Example #1 – High Level Description
1. If there are no numbers in the set then there is no highest number.
2. Assume the first number in the set is the largest number in the set.
3. For each remaining number in the set: if this number is larger than
the current largest number, consider this number to be the largest
number in the set.
4. When there are no numbers left in the set to iterate over, consider
the current largest number to be the largest number of the set.
Example #1 - Pseudocode
Example #2
• Compute the greatest common divisor (GCD) to two numbers
• Euclid’s Algorithm
• Euclid poses the problem thus: "Given two numbers not prime to one
another, to find their greatest common measure"
Example #2 – Flowchart
Example #2 - Code
Exercise
Design an algorithm that calculates the monthly paycheck of a salesperson
at a local department store
• Every salesperson has a base salary.
• The salesperson also receives a bonus at the end of each month, based on
the following criteria:
• If the salesperson has been with the store for five years or less, the bonus is $ 10 for
each year that he or she has worked there.
• If the salesperson has been with the store for more than five years, the bonus is $ 20
for each year that he or she has worked there.
• The salesperson can earn an additional bonus as follows: If the total sales made by
the salesperson for the month are at least $ 5,000 but less than $ 10,000, he or she
receives a 3% commission on the sale. If the total sales made by the salesperson for
the month are at least $ 10,000, he or she receives a 6% commission on the sale.
First Program:
Hello, world
30
Updated by: Malak Abdullah
Processing a C++ Program
#include <iostream>
using namespace std;
int main()
{
cout << "My first C++ program." << endl;
return 0;
}
Sample Run:
My first C++ program.
31
Processing a C++ Program (cont'd.)
• To execute a C++ program:
• Use an editor to create a source program in C++
• Preprocessor directives begin with # and are processed by a the preprocessor
• Use the compiler to:
• Check that the program obeys the rules
• Translate into machine language (object program)
32
Processing a C++ Program (cont'd.)
• To execute a C++ program (cont'd.):
• Linker:
• Combines object program with other programs provided by the SDK to create executable
code
• Loader:
• Loads executable program into main memory
• The last step is to execute the program
33
Processing a C++ Program (cont'd.)
34
Programming with the Problem Analysis–Coding–
Execution Cycle
• Programming is a process of problem solving
• One problem-solving technique:
• Analyze the problem
• Outline the problem requirements
• Design steps (algorithm) to solve the problem
• Algorithm:
• Step-by-step problem-solving process
• Solution achieved in finite amount of time
35
The Problem Analysis–Coding–Execution Cycle
(cont’d.)
36
The Problem Analysis–Coding–Execution Cycle
(cont'd.)
• Run code through compiler
• If compiler generates errors
• Look at code and remove errors
• Run code again through compiler
• If there are no syntax errors
• Compiler generates equivalent machine code
• Linker links machine code with system resources
37
The Problem Analysis–Coding–Execution Cycle
(cont'd.)
• Once compiled and linked, loader can place program into main
memory for execution
• The final step is to execute the program
• Compiler guarantees that the program follows the rules of the
language
• Does not guarantee that the program will run correctly
38
39
So what is programming?
• Conventional definitions
• Telling a very fast moron exactly what to do
• A plan for solving a problem on a computer
• Specifying the order of a program execution
• But modern programs often involve millions of lines of code
• And manipulation of data is central
• Definition from another domain (academia)
• A … program is an organized and directed accumulation of resources to
accomplish specific … objectives …
• Good, but no mention of actually doing anything
• The definition we’ll use
• Specifying the structure and behavior of a program, and testing that the program
performs its task correctly and with acceptable performance
• Never forget to check that “it” works
• Software == one or more programs
40
Programming
• Programming is fundamentally simple
• Just state what the machine is to do
• So why is programming hard?
• We want “the machine” to do complex things
• And computers are nitpicking, unforgiving, dumb beasts
• The world is more complex than we’d like to believe
• So we don’t always know the implications of what we want
• “Programming is understanding”
• When you can program a task, you understand it
• When you program, you spend significant time trying to understand the task you want to
automate
• Programming is part practical, part theory
• If you are just practical, you produce non-scalable unmaintainable hacks
• If you are just theoretical, you produce toys
Reference
• Bjarne Stroustrup, 2015, Texas A&M University
• D.S. Malik, C++ Programming: From Problem Analysis to Program
Design, Fourth Edition

Pengenalan algoritma dasar dalam pemrograman

  • 1.
    Programming 1 Guntur BudiHerwanto gunturbudi@ugm.ac.id
  • 2.
    Hello •Lecturers: •Guntur Budi H(Before Mid) •Mhd Reza Pulungan (After Mid)
  • 3.
  • 4.
  • 5.
  • 6.
    Before we Start 15 6 3 9 2 4
  • 7.
    What is thiscourse about INTENDED FOR YOU WITH LITTLE OR NO “PROGRAMMING” EXPERIENCE. 01 LEARN “ALGORITHM“ THE ROLE COMPUTATION CAN PLAY IN SOLVING PROBLEMS 02 WRITING CODE USING A “PROGRAMMING LANGUAGE” 03
  • 8.
    Why C++ ? •You can’t learn to program without a programming language • The purpose of a programming language is to allow you to express your ideas in code • C++ is the language that most directly allows you to express ideas from the largest number of application areas • C++ is the most widely used language in engineering areas (http://www.stroustrup.com/applications.html)
  • 9.
    Why C++ C++ isprecisely and comprehensively defined by an ISO standard C++ is available on almost all kinds of computers Programming concepts that you learn using C++ can be used fairly directly in other languages Including C, Java, C#, and (less directly) Fortran
  • 10.
    Course Outcome 1. Haveknowledge about the importance of algorithms and data structures in solving problems 2. Have knowledge about components in algorithms and can construct algorithms to solve simple problems. 3. Have knowledge about data structures and C++ programming language. 4. Have knowledge about data types for array and records / struct and can implement them in a computer program. 5. Have knowledge about modular programming and can implement it in a computer program. 6. Be able to explain and competent in how to implement sorting and searching algorithms. 7. Have knowledge about pointer data type and can implement it in a computer program. 8. Be able and competent in solving more complex programming problems.
  • 11.
    Assesment CO1: Problem 1in mid- term exam (5%) and exercise 1 (5%) - 10% CO2: Problem 2 in mid- term exam (5%) and exercise 2 (5%) - 10% CO3: Problem 3 in mid- term exam (5%); problem 4 in mid-term exam (5%); assignment 1: make an algorithm and computer program (5%); and exercise 3 (5%) - 20% CO4: Problem 5 in mid- term exam (5%); problem 1 in final exam (5%) and exercise 4 (5%) - 15% CO5: Problem 2 in final exam (5%); assignment 2: make a function and recursive (5%); and exercise 5 (5%) - 15% CO6: Problem 3 in final exam (5%) and exercise 6 (5%) - 10% CO7: Problem 4 in final exam (5%) and exercise 7 (5%) - 10% CO8: Problem 5 in final exam (5%) and assignment 3: make a program based on a real-life problem (5%) - 10%
  • 12.
    Main Reference C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN SIXTH EDITION D.S. MALIK
  • 13.
    Course Topics Introduction Algorithm DataType & Operator Control Structure – Condition Control Structure – Repetition Array Struct Function Sorting & Searching Pointer
  • 14.
    Why programming? Our civilizationruns on software Most engineering activities involve software Note: most programs do not run on things that look like a PC (a screen, a keyboard, a box under the table) 14
  • 15.
    Phones • Chat • SocialMedia • Payment • Security • Call • SMS
  • 16.
    PC / Tablet/ Workstation • Operating System • Computer Program
  • 17.
    Energy • Control • Monitoring •Analysis • Design • Communications • Visualization • Manufacturing
  • 18.
  • 19.
  • 20.
  • 21.
    Algorithm • An unambiguousspecification of how to solve a class of problems. Algorithms can perform calculation, data processing and automated reasoning tasks. • Algorithms are essential to the way computers process data. • Algorithms can be expressed in many kinds of notation, including natural languages, pseudocode, flowcharts, or programming languages.
  • 22.
    Algorithm Design 1. Problemdefinition 2. Development of a model 3. Specification of algorithm 4. Designing an algorithm 5. Checking the correctness of algorithm 6. Analysis of algorithm 7. Implementation of algorithm 8. Program testing 9. Documentation preparation
  • 23.
    Example #1 Find thelargest number in a list of numbers of random order
  • 24.
    Example #1 –High Level Description 1. If there are no numbers in the set then there is no highest number. 2. Assume the first number in the set is the largest number in the set. 3. For each remaining number in the set: if this number is larger than the current largest number, consider this number to be the largest number in the set. 4. When there are no numbers left in the set to iterate over, consider the current largest number to be the largest number of the set.
  • 25.
    Example #1 -Pseudocode
  • 26.
    Example #2 • Computethe greatest common divisor (GCD) to two numbers • Euclid’s Algorithm • Euclid poses the problem thus: "Given two numbers not prime to one another, to find their greatest common measure"
  • 27.
    Example #2 –Flowchart
  • 28.
  • 29.
    Exercise Design an algorithmthat calculates the monthly paycheck of a salesperson at a local department store • Every salesperson has a base salary. • The salesperson also receives a bonus at the end of each month, based on the following criteria: • If the salesperson has been with the store for five years or less, the bonus is $ 10 for each year that he or she has worked there. • If the salesperson has been with the store for more than five years, the bonus is $ 20 for each year that he or she has worked there. • The salesperson can earn an additional bonus as follows: If the total sales made by the salesperson for the month are at least $ 5,000 but less than $ 10,000, he or she receives a 3% commission on the sale. If the total sales made by the salesperson for the month are at least $ 10,000, he or she receives a 6% commission on the sale.
  • 30.
  • 31.
    Updated by: MalakAbdullah Processing a C++ Program #include <iostream> using namespace std; int main() { cout << "My first C++ program." << endl; return 0; } Sample Run: My first C++ program. 31
  • 32.
    Processing a C++Program (cont'd.) • To execute a C++ program: • Use an editor to create a source program in C++ • Preprocessor directives begin with # and are processed by a the preprocessor • Use the compiler to: • Check that the program obeys the rules • Translate into machine language (object program) 32
  • 33.
    Processing a C++Program (cont'd.) • To execute a C++ program (cont'd.): • Linker: • Combines object program with other programs provided by the SDK to create executable code • Loader: • Loads executable program into main memory • The last step is to execute the program 33
  • 34.
    Processing a C++Program (cont'd.) 34
  • 35.
    Programming with theProblem Analysis–Coding– Execution Cycle • Programming is a process of problem solving • One problem-solving technique: • Analyze the problem • Outline the problem requirements • Design steps (algorithm) to solve the problem • Algorithm: • Step-by-step problem-solving process • Solution achieved in finite amount of time 35
  • 36.
  • 37.
    The Problem Analysis–Coding–ExecutionCycle (cont'd.) • Run code through compiler • If compiler generates errors • Look at code and remove errors • Run code again through compiler • If there are no syntax errors • Compiler generates equivalent machine code • Linker links machine code with system resources 37
  • 38.
    The Problem Analysis–Coding–ExecutionCycle (cont'd.) • Once compiled and linked, loader can place program into main memory for execution • The final step is to execute the program • Compiler guarantees that the program follows the rules of the language • Does not guarantee that the program will run correctly 38
  • 39.
    39 So what isprogramming? • Conventional definitions • Telling a very fast moron exactly what to do • A plan for solving a problem on a computer • Specifying the order of a program execution • But modern programs often involve millions of lines of code • And manipulation of data is central • Definition from another domain (academia) • A … program is an organized and directed accumulation of resources to accomplish specific … objectives … • Good, but no mention of actually doing anything • The definition we’ll use • Specifying the structure and behavior of a program, and testing that the program performs its task correctly and with acceptable performance • Never forget to check that “it” works • Software == one or more programs
  • 40.
    40 Programming • Programming isfundamentally simple • Just state what the machine is to do • So why is programming hard? • We want “the machine” to do complex things • And computers are nitpicking, unforgiving, dumb beasts • The world is more complex than we’d like to believe • So we don’t always know the implications of what we want • “Programming is understanding” • When you can program a task, you understand it • When you program, you spend significant time trying to understand the task you want to automate • Programming is part practical, part theory • If you are just practical, you produce non-scalable unmaintainable hacks • If you are just theoretical, you produce toys
  • 41.
    Reference • Bjarne Stroustrup,2015, Texas A&M University • D.S. Malik, C++ Programming: From Problem Analysis to Program Design, Fourth Edition