programming in life

56
Programming in Life ComCamp 22 Programming Training Session: 1-1 Create by Chris http://coffeemilkcrazy.wordpress.com/ Wednesday, March 31, 2010

Upload: chris-coffeemilkcrazy

Post on 22-Jan-2018

312 views

Category:

Education


0 download

TRANSCRIPT

Programming in LifeComCamp 22 Programming Training

Session: 1-1

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

What is a Programming?

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Once upon a time

• Augusta Lovelace Ada คือ โปรแกรมเมอร์คนแรกของโลก

• Edsger Wybe Dijkstra ใช้คําว่า โปรแกรมเมอร์ (Programmer) กับโลกของคอมพิวเตอร์เป็นคนแรกDo not try to change the world. Give the world the opportunity to change itself

Edsger Wybe Dijkstra เป็น theoretical physicist และเสียชีวิตด้วยโรคมะเร็ง 6 ส.ค. 2002 (อายุ 72 ปี)

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

History programming Timeline

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Programming Skill

• Syntax

• Structure

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

C language = BackendCommand Line

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Why C language?

• Easy

• Simple

• Root

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Get Started

Tools Knowledge Time

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Tools : Codeblocks

Create Project(1) Open Codeblocks(2) “File” > “New” > “Empty file”(3) “Save” or “Save as” than name your project “myPrj”(4) Format “C/C++ files”

At myPrj tab coder(5) Paste “ “

(6) Build and run

#include <stdio.h>

main(){ printf(“Hello, World!”);}

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Basic CComCamp 22 Programming Training

Session: 1-2

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Basic C

A Simple C ProgramThis is your first C program. Paste this example into a plain text editor and name the file test1.c

#include <stdio.h>

main(){ printf(“We are ComCamp”);}

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Basic C Concepts

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Header

Main Function

Basic C

A Simple C ProgramThis is your first C program. Paste this example into a plain text editor and name the file test1.c

#include <stdio.h>

main(){ printf(“We are ComCamp”);}

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Basic CTyped VariableIn C, the rules are more strict. You have to declare the type of data a variable will hold, and the type can't change later. Here's the C equivalent of the snippet above:

int variable1 = 2;float variable2 = 1.618;char variable3 = 'A';

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Basic CTyped FunctionsIn C, you have to declare the type of data you plan to return from a function. The return type can be any C variable type, and is placed to the left of the function name.

int numberOfPeople (){ return 3;}

float dollarsAndCents (){ return 10.33;}

char firstLetter (){ return 'A';}

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Basic CYou can also specify the return type as void. For now, you can just think of this as saying that no value will be returned:

void printHello (){ printf ("Hello\n");}

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Basic CTypes for ParametersIt's also necessary to define types for values passed into a function. Unlike scripting languages, though, you can't set default values.

int difference (int value1, int value2){ return value1 - value2;}

float changeDue (float amountPaid, float costOfItem){ return amountPaid - costOfItem;}

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Basic CDeclaring FunctionsIn C, a function has to be declared before any other code can call it. You can put all the functions before main(), but this quickly becomes a lot of work.

int difference ( int value1, int value2 );float changeDue ( float amountPaid, float costOfItem );

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Basic C

Header

Main Function

Declaring Sum Function

Sum Function

ExampleHere's an example. Paste the contents into a file called test2.c

#include <stdio.h>

int sum ( int x, int y );

main (){ int theSum; theSum = sum (10, 11); printf ( "Sum: %i\n", theSum );}

int sum ( int x, int y ){ return x + y;}

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Basic CIn C, you can't embed variables directly inside text. You have to use a format string with markers for the variables:

int var1 = 3;int var2 = 8;printf ("First value: %d second value: %d", var1, var2);

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Basic CExercise 1, Say HelloStep 1 : Paste “#include <stdio.h>” Header standard function.Step 2 : Paste “main() {...}“ Main function for first run.Step 3 : Insert “printf(“Hello We are ComCamp”);” Console show this log.

#include <stdio.h>

main(){ printf(“Hello We are ComCamp”);}

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Basic CExercise 2, Say Hello AdvanceStep 1 : Paste “#include <stdio.h>” Header standard function.Step 2 : Paste “main() {...}” Main function for first run.Step 3 : Insert “char name[10];” Into Main function, declare variableStep 4 : Insert “printf (“What your name: ”);” printf Console show log.Step 5 : Insert “scanf(“%s”, name);” scanf Console read user enter.Step 6 : Insert “printf(“Hello %s, We are ComCamp”, name);”

#include <stdio.h>

main(){ char name[10]; printf(“What your name: “); scanf(“%s”, name); printf(“Hello %s, We are ComCamp”, name);}

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

ConditionComCamp 22 Programming Training

Session: 1-3

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

What is Condition?

Programming = การกําหนดระบบและกลไกให้กับเครื่องคอมพิวเตอร์ได้ประมวลผล

Condition = เงือนไข Ex. x=2, x>0 is true or false

ในการเขียนโปรแกรมให้ซับซ้อนและตัดสินได้ผ่านเงือนไขที่เรากําหนด จะต้องใช้ if และ else

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

How to useHow to use if and elsesyntax of if else function, if(condition) {statement} else{statement}

...

if(age>18){ printf(“You can pass”);}else{ printf(“You can’t pass”);}

...

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

If and Else ConditionExercise 3, Dek ComCampStep 1 : Paste “#include <stdio.h>” Header standard function.Step 2 : Paste “main() {...}” Main function for first run.Step 3 : Insert “char ans;” Into Main function, declare variableStep 4 : Insert “printf (“Are you hot (Y/N): ”);” printf Console show log.Step 5 : Insert “scanf(“%c”, &ans);” scanf Console read user enter.Step 6 : add condition if say “yes” program say “You are ComCamp!”condition = (ans==’Y’)

#include <stdio.h>

int main(){ char ans; printf("Are you hot (Y/N): "); scanf("%c", &ans); if (ans=='Y') {! ! printf("You are ComCamp!");! } return 0;}

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

How to useHow to use else if Extendsystax of if else function, if(condition1) {statement1} else if(condition2) {statement2} else{statement3}

...

if(age>=18 && age<=100){ printf(“You can pass”);}else if(age<18 && age>=0){ printf(“You can’t pass”);}else{ printf(“Error you must enter 0-100”);}

...Create by Chris

http://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

ConditionExercise 3, Dek ComCamp ExtendStep 1 : Paste “#include <stdio.h>” Header standard function.Step 2 : Paste “main() {...}” Main function for first run.Step 3 : Insert “char ans;” Into Main function, declare variableStep 4 : Insert “printf (“Are you hot (Y/N): ”);” printf Console show log.Step 5 : Insert “scanf(“%c”, &ans);” scanf Console read user enter.Step 6 : add condition if say “Y” program say “you are comCamp!”condition = (ans==’Y’) Step 7 : add condition else if say “N” program say “Oh!...You aren’t ComCamp!”condition = (ans==’N’) Step 8 : add else say not Y and N program say “You must enter only (Y/N).”

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Symbol of CCONDITION

&& AND|| OR== EQUAL!= NOT EQUAL>= MORE THAN OR EQUAL<= LESS THAN OR EQUAL

MATH

> MORE THAN< LESS THAN= SET VARIABLE+ PLUS- MINUS* MULTIPLE/ TIME

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Code#include <stdio.h>

int main(){ char ans; printf("Are you hot (Y/N): "); scanf("%c", &ans); if (ans=='Y') {! ! printf("You are ComCamp!");! } else if (ans==’N’) { printf("Oh!...You aren’t ComCamp!"); } else { printf(“You must enter only (Y/N)”); } return 0;}

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

LoopComCamp 22 Programming Training

Session: 2-1

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

What is Loop?

Loop = การวนซํ้า

ในการเขียนวนซํ้าทําให้ช่วยลดการเขียนโปรแกรมลงได้

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Loop Function

While or Do While

For

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

What is While Loop?How to use while loopsyntax of if While function, While(condition) {statement}

...

printf(“How old are you: “);scanf(“%d”, &age);while(age<18){ printf(“You can’t pass”); printf(“How old are you: “); scanf(“%d”, &age);}printf(“You can pass”);

...

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

While LoopExercise 4, AccountingStep 1 : Paste “#include <stdio.h>” Header standard function.Step 2 : Paste “main() {...}” Main function for first run.Step 3 : Insert “int op; int budget; int amount;” Into Main function, declare variableStep 4 : Insert “printf (“Insert your budget: ”);”Step 5 : Insert “scanf(“%d”, &budget);” Step 6 : Insert “printf (“1 Income, 2 expense,0 Exit: ”);”Step 7 : Insert “scanf(“%d”, &op);” scanf Console read user enter.Step 8 : Insert “while(op!=0) {...}” while loopStep 9 : add condition if say “1” program ask “Income amount: ”condition = (op==1) and scanf to receive Income amount than calculate formula: budget = budget + amount;Step 10 : else program say “Expanse amount:” and scanf to receive Expanse amount than calculate formula: budget = budget - amount;Step 11 : show budget and loop to menu again

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Code#include <stdio.h>

int main(){! int op, budget, amount; printf("Insert your budget: ");! scanf("%d", &budget);! printf("1 Income, 2 expense, 0 Exit: ");! scanf("%d", &op);! while (op!=0) {! ! if (op==1) {! ! ! printf("Income amount: ");! ! ! scanf("%d", &amount);! ! ! budget = budget + amount;! ! }! ! else {! ! ! printf("Expense amount: ");! ! ! scanf("%d", &amount);! ! ! budget = budget - amount;! ! }! ! printf("Your balance is %d", budget);! ! printf("1 Income, 2 expense, 0 Exit: "); scanf("%d", &op); } return 0;}

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

What is Do While Loop?How to use do while loopsyntax of if While function, do {statement} While(condition);

...

do { printf(“How old are you: “); scanf(“%d”, &age); if(age<18) printf(“You can’t pass\n”);} while(age<18);printf(“You can pass”);

...

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Do While LoopExercise 5, LoginStep 1 : Paste “#include <stdio.h>” Header standard function.Step 2 : Paste “main() {...}” Main function for first run.Step 3 : Insert “int pwd;” Into Main function, declare variableStep 4 : Insert “do{...}”Step 5 : Insert “printf (“Enter your password: ”);”Step 6 : Insert “scanf(“%d”, &pwd);” Step 7 : Insert “while(pwd!=0000);” 0000 is a password.Step 8 : Insert “printf (“Password Correct!”);”

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Code#include <stdio.h>

int main(){! int pwd;! do {! ! printf("Enter your password: ");! ! scanf("%d", &pwd);! } while (pwd!=0000);! printf("Password Correct!");}

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

DebugComCamp 22 Programming Training

Session: 2-2

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

What is Debug?

Debug = การแก้ไขจุดผิดพลาดของโปรแกรม

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Debug

จากโปรแกรมที่แล้ว Exercise 4, Accounting ตรงเมนูถ้าเลือก นอกเหนือจากตัวเลือกในเมนูแล้วเช่น ใส่ 5 ไป โปรแกรมจะไปยัง รายจ่าย ซึ่งจริงๆแล้วโปรแกรมควรฟ้องว่า ErrorStep 6 : Insert “printf (“1 Income, 2 expense,0 Exit: ”);”Step 7 : Insert “scanf(“%d”, &op);” scanf Console read user enter.Step 8 : Insert “while(op!=0) {...}” while loopStep 9 : add condition if say “1” program ask “Income amount: ”condition = (op=1) and scanf to receive than calculateStep 10 : else program say “Expanse amount:”and scanf to receive than calculateStep 11 : show budget and loop to menu again

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

DebuggingExcercise 6, DebuggingAccounting Program โดยใช้ else if(...) {...}Step 6 : Insert “printf (“1 Income, 2 expense,0 Exit: ”);”Step 7 : Insert “scanf(“%d”, &op);” scanf Console read user enter.Step 8 : Insert “while(op!=0) {...}” while loopStep 9 : add condition if say “1” program ask “Income amount: ”condition = (op==1) and scanf to receive than calculateStep 10 : add condition else if say “2” program say “Expanse amount:”condition = (op==2) and scanf to receive than calculateStep 10.5 : else not say 1,2,0 program say “Please enter only (1,2,0)\n”Step 11 : show budget and loop to menu again

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Code#include <stdio.h>

int main(){! int op, budget, amount; printf("Insert your budget: ");! scanf("%d", &budget);! printf("1 Income, 2 expense, 0 Exit: ");! scanf("%d", &op);! while (op!=0) {! ! if (op==1) {! ! ! printf("Income amount: ");! ! ! scanf("%d", &amount);! ! ! budget = budget + amount;! ! }! ! else if(op==2) {! ! ! printf("Expense amount: ");! ! ! scanf("%d", &amount);! ! ! budget = budget - amount;! ! } else { printf(“Please insert only(1,2,0)\n”); }! ! printf("Your balance is %d", budget);! ! printf("1 Income, 2 expense, 0 Exit: "); scanf("%d", &op); } return 0;} Create by Chris

http://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Debug AgainExcercise 7, Debugging Proเมื่อ User ใส่ค่าติดลบลงไป โปรแกรมจะต้องให้ใส่ค่าใหม่อีกครั้ง และแจ้งผู้ใช้ โดยใช้ while(budget<0) {...}while (budget<0) {! printf("Don't insert minus number.\n");! printf("Insert your budget: ");! scanf("%d", &budget);}

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Loop (continue)ComCamp 22 Programming Training

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

What is For LoopHow to use For loopsyntax of if For function, for(initial; condition; increment) {statements}

...

for(i=0;i<=12;i++){ printf(“2 x %d = %d\n”, i, i*2);}

...

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

For LoopExcercise 8, Multiple...

for(i=0;i<=12;i++){ printf(“%d x %d = %d\n”, multi, i, i*multi);}

...

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

Project Show Case

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010

See U Soon At KMUTT :)

Create by Chrishttp://coffeemilkcrazy.wordpress.com/

Wednesday, March 31, 2010