flowchart, types, and values - mybogi.files.wordpress.com · why do we need to take this course?...
TRANSCRIPT
www.telkomuniversity.ac.id
Flowchart, Types, and Values
Course Number : FEH1H3
CLO : 1
Week : 1-3 CLO#1 Student have the knowledge to create algorithm and iteration Know the fundamental of computer system Understand the components to build and create algorithm Understand types and basic expression Understand branching expression Understand types of iteration and how to use them
www.telkomuniversity.ac.id
IT vs. IS vs. CS
• IT = Information Technology
• CS = Computer Science
• IS = Information System
• Manage effective and efficient collaboration between CS and IT
• Industrial Engineering domain IS • Handle all application towards human
• Informatics Engineering domain CS • Handle all hardware and related algorithm
• Electrical Engineering domain IT
www.telkomuniversity.ac.id
Why do we need to take this course?
Because a programmer from CS needs to understand how the hardware works, and IT personnel needs to understand how the software works, and it starts with what you will learn in this course, i.e. programming in C.
www.telkomuniversity.ac.id
So, What is Algorithm?
In mathematics and computer science, an algorithm (al-gə-ri-dhəm) is a self-contained step-by-step set of operations to be performed. Algorithms perform calculation, data processing, and/or automated reasoning tasks.
Source: Wikipedia
www.telkomuniversity.ac.id
APPLICATION
OPERATING SYSTEM
HARDWARE
OUTPUT OUTPUT INPUT INPUT
Computer System Brain ware
www.telkomuniversity.ac.id
Let’s Make an Algorithm
How to brew a coffee?
1. Boil a cup of water
2. Put grinded coffee into empty cup
3. Pour the boiling water into the cup
How to brew a good coffee?
1. Boil a cup of tap water
2. Grind 30 coffee bean to create 1/3 cup of medium-coarse grind
3. Pour 1/3 of the boiling water into the cup, and stir
4. Pour another 2/3 of the boiling water into the cup
www.telkomuniversity.ac.id
Let’s Make another Algorithm with Branch
How to brew a coffee with your girlfriend?
1. Find a girlfriend
2. Is she pretty? 1. No! Well, that’s okay, let’s make coffee
2. Yes! Okay, let’s make coffee
3. Boil a cup of water
4. Put grinded coffee into empty cup
5. Pour the boiling water into the cup
6. Sip the coffee, 1 cup for both to create romantic stimulation
www.telkomuniversity.ac.id
Let’s Make another Algorithm with Branch and Iteration
How to brew a coffee with your girlfriend?
1. Find a girlfriend
2. Is she pretty? 1. No! Well, is she rich?
1. No! fiuuh... Live is hard boy, let’s find another girl, shall we?
2. Yes! Okay, let’s make coffee
2. Yes! Okay, let’s make coffee
3. Boil a cup of water
4. Put grinded coffee into empty cup
5. Pour the boiling water into the cup
6. Sip the coffee, 1 cup for both to create romantic stimulation
www.telkomuniversity.ac.id
Why don’t you create an Algorithm?
• How to find a perfect job?
• How to run in a gym?
• How to add all numbers between 1 to 5?
• Which one is the simple algorithm?
• Which one uses branch?
• Which one uses iteration?
www.telkomuniversity.ac.id
Flowchart
• Flowchart is one from many ways to express an algorithm
• Flowchart describes algorithm using image and arrow
• Some images are:
process start decision data document
display storage connector
www.telkomuniversity.ac.id
Let’s Draw a Flowchart !
How to brew a good coffee?
1. Boil a cup of tap water
2. Grind 30 coffee bean to create 1/3 cup of medium-coarse grind
3. Pour 1/3 of the boiling water into the cup, and stir
4. Pour another 2/3 of the boiling water into the cup
boil water
start
grind coffee
pour 1/3 water
pour 2/3 water
stop
www.telkomuniversity.ac.id
Flowchart with Branch
How to brew a coffee with your girlfriend?
1. Find a girlfriend
2. Is she pretty? 1. No! Well, that’s okay, let’s make coffee
2. Yes! Okay, let’s make coffee
3. Boil a cup of water
4. Put grinded coffee into empty cup
5. Pour the boiling water into the cup
6. Sip the coffee, 1 cup for both to create romantic stimulation
boil water
start
put coffee
pour water
enjoy coffee
stop
pretty?
find girlfriend
No Yes
www.telkomuniversity.ac.id
Flowchart with Branch and Iteration
How to brew a coffee with your girlfriend?
1. Find a girlfriend
2. Is she pretty? 1. No! Well, is she rich?
1. No! fiuuh... Live is hard boy, let’s find another girl, shall we?
2. Yes! Okay, let’s make coffee
2. Yes! Okay, let’s make coffee
3. Boil a cup of water
4. Put grinded coffee into empty cup
5. Pour the boiling water into the cup
6. Sip the coffee, 1 cup for both
boil water
start
put coffee
pour water
enjoy coffee
stop
pretty?
find girlfriend
No Yes
rich? Yes
www.telkomuniversity.ac.id
Pseudo code
• Pseudo code is another way to express an algorithm
• Pseudo code describes algorithm using structured sentence in human readable
• Has many styles: C, Pascal, Fortran, and Basic
void function print_red_or_green {
for (i = 1; i <= 100; i++) {
if i is divisible by 3 print "red";
if i is divisible by 5 print "green";
print a newline;
}
}
www.telkomuniversity.ac.id
Let’s Try an Example
1. Take 1 ball from box A
2. If the ball is red then put the ball into box B
3. If the ball is not red then put the ball into box C
www.telkomuniversity.ac.id
Let’s Try another Example
1. Take 1 ball from box A
2. If the ball is red then put the ball into box B
3. If the ball is green then put the ball into box C
4. If the ball is blue then put the ball into box D
5. If the ball is yellow then put the ball into box E
www.telkomuniversity.ac.id
Swap Problem
Initial state:
1. Apple is on plate A
2. Banana is on plate B
Final state: Need to swap the fruits, so:
1. Apple is on plate B
2. Banana is on plate A
We need a temporary plate C
www.telkomuniversity.ac.id
Iteration Problem 1
Initial state:
1. Box A has 100 ball
2. Box B has no ball
Final state:
1. Box A has no ball
2. Box B has 100 ball
Move the ball from box A to box B, 100 times
www.telkomuniversity.ac.id
Iteration Problem 2
Initial state:
1. Box A has n ball (n = unknown)
2. Box B has no ball
Final state:
1. Box A has no ball
2. Box B has n ball
Move the ball from box A to box B, n times
www.telkomuniversity.ac.id
Which one is Foolproof?
start
stop
A = empty ?
take 1 ball from A
No Yes
put the ball into B
start
stop
A = empty ?
take 1 ball from A
No Yes
put the ball into B
start
stop
A = empty ?
take 1 ball from A
No Yes
put the ball into B
start
stop
A = empty ?
take 1 ball from A
No Yes
put the ball into B
A = empty ?
No Yes
www.telkomuniversity.ac.id
Challenge
• There is 1 bag full of candies.
• I need to distribute the candies to 4 kids evenly.
• Each of 4 kids receive the same amount of candies.
• Leftover candies will not be distributed.
Create a foolproof algorithm
www.telkomuniversity.ac.id
Why C Language?
• Prolog, best for logic programming
• Basic, well... it was very popular in the old days
• Pascal, best for learning how to write a good program
• C, well... it is the standard (de facto) used by many people
• Java, best for porting
Plus, when you learn C, you also learn PHP, Matlab, Network Simulator, and hardware programming
www.telkomuniversity.ac.id
C Code Structure
1. Libraries #include <stdio.h>
#include <stdlib.h>
2. Data declaration FILE *fp;
char line[85],
*ptr;
int set=0,
col=0;
3. Main Functions int main(int argc, char *argv[])
4. Functions
www.telkomuniversity.ac.id
Very Simple Example
#include <stdio.h>
void main(void)
{
int a;
printf("Please input a number: ");
scanf("%d", &a);
printf("Input = %d", a);
}
www.telkomuniversity.ac.id
Operator, Operand, and Expression
A = B + C
“A = B + C” Expression
A, B, C Operands
=, + Operator
www.telkomuniversity.ac.id
Data Types
• Integral Types: char, signed char, unsigned char short, int, long unsigned short, unsigned, unsigned long
• Floating Types: float, double, long double
• Enumerated Types: array
• User defined Types
• Memory Types: pointer, string
www.telkomuniversity.ac.id
Data Types: Integral
• Char / Signed Char / Unsigned Char = 8 bit *) example: “a”, “d”, “u” you need to declare an array of char to store a word example: name[15] may store “nyoman karna”
• Short = 16 bit *) / Int = 16/32 bit *) / Long = 32 bit *) example: -1234, 0, 1000
• Unsigned means there are no negative number example: unsigned short: 0, 1, ..., 65535
*) depends on machine
www.telkomuniversity.ac.id
Data Types: Floating
• Float = 32 bit *)
• Double = 64 bit *)
• Long Double >= 64 bit *)
example: 1.5, 1.2345e3
*) depends on machine
www.telkomuniversity.ac.id
Data Types: Enumerated
• Array = matrix of another data types example: int grade0, grade1, grade2; /* is equal with */
int grade[3]; /* is 1-dimensional array, while */
int grade[2][3]; /* is a 2-dimensional array */
• Pointer = a special data type to point a reserved part of memory example: int a=1, b=2, *p;
p=&a;
b=*p; /* guess, what is the value of b? */
www.telkomuniversity.ac.id
Data Types: Enumerated
• String = a pointer of array of char example: char *p = "abc"; /* is an equal syntax with */
char p[3] = "abc";
What is the difference between those 2 examples?
www.telkomuniversity.ac.id
User-defined Data Types
struct student {
char studentID[10]; /* student ID number as a string */
int *answers; /* ptr to dynamic array of grades */
int rawScore; /* calculated raw score */
};
typedef struct student student;
/* assign student structure as a new var type*/
student assign_student(char student_ID[], char tudent_solution[],
int no_of_question, int *solution)
{
int i; /* used for iteration (counter) */
student new_student;/* variable type student structure*/
........
www.telkomuniversity.ac.id
Variable and Constant
• We use variable to store dynamic value example: int r; /* variable r may have any integer
value as the program goes */
• We use constant to store predefined value example: float phi=3.14; /* phi will always has a
value of 3.14 */
www.telkomuniversity.ac.id
A Simple Example of Variable and Constant
#include <stdio.h>
void main(void)
{
int r;
float phi=3.14;
printf("What is the radius of the circle? ");
scanf("%d", &r);
printf("The circle has an area of %f", phi*r*r);
}
www.telkomuniversity.ac.id
Types of Operator
• Arithmetic
• Assignment
• Logical/relational
• Bitwise
www.telkomuniversity.ac.id
Arithmetic Operator
+ For performing Addition – For performing Subtraction / For performing Division * For performing Multiplication % Modulo for finding remainder in division
operation -- Decrement (post and pre) ++ Increment (post and pre)
www.telkomuniversity.ac.id
Assignment Operator
= Equal *= Multiply /= Divide %= Modulus += Add -= Subtract
www.telkomuniversity.ac.id
Logical / Relational Operator
== Equal to != Not Equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to && Logical AND || Logical OR ! Logical NOT
www.telkomuniversity.ac.id
Bitwise Operator
& AND | Inclusive OR ^ Exclusive OR << Shift Left >> Shift Right ~ One’s compliment