Basic C-Programming - Short.pdf

Post on 22-Dec-2015

19 views 0 download

Tags:

Transcript of Basic C-Programming - Short.pdf

Computer programming

By: tulawat chunwijitra

OUTLINE

Basic C-Programming

Data Structure

stack, queue, linked list, tree, hash table

Recursion concept

Object Oriented Programming concept

Relational Database concept

Basic C-Programming

by: tulawat chunwijitra

outline

Introduction

Before we start coding .....

Software Development process

Flowchart and pseudocoding

Workshop 1: Grading program

First C-Programming

Introduction

Time Table for today

Goal is “YOU CAN PROGRAM IN C”

What is your background in C-Programming?

Theme of learning ------> Get your hand dirty !!!!

before we start coding

คอมพิวเตอรทำงานในระบบฐานสอง ซึ่งมนุษยเขาใจยาก

ออกแบบตัวอักษรภาษาอังกฤษแทนคำสั่งเลขฐานสอง เรียกวา Mnemonic

ภาษาคอมพิวเตอรที่ใช Mnemonic คือ ภาษา Assembly ซึ่งถูกจัดใหเปนภาษาระดับต่ำ

ตอมา Assembly ไดรับการพัฒนาใหใกลเคียงภาษามนุษย เรียกวาภาษาระดับสูง เชน C, Modulas-2, Pascal, Java, Fortran, Basic, etc.

ในการเขียนโปรแกรม ไมวาจะเขียนดวยภาษาระดับสูง หรือระดับต่ำ เราจะตองแปลใหเปนรหัสภาษาเครื่องที่คอมพิวเตอรเขาใจเสียกอน ตัวแปลภาษานี้ ถูกเรียกวา Compiler หรือ Interpreter

compiler vs interpreter

Interpreter

แปลความหมายของคำสั่งทีละคำสั่ง (แปลไปทำไป) ถาไมพบขอผิดพลาด เครื่องจะทำคำสั่งที่แปลได แตถามีขอผิดพลาด เครื่องจะหยุดทำงานและแจงขอผิดพลาดออกมา

เชน ภาษาเบสิก

Compiler

แปลความหมายของ Source code ทั้งหมดกอน แลวจึง Run

โปรแกรมทำงานไดรวดเร็วกวา Interpreter เพราะไมตองไปเสียเวลานั่งแปลอีกตอน Run

c-compiler

compiler

linker

Easy.C

Easy.EXE

Easy.OBJ

C Library

software development process

1. กำหนดและวิเคราะหปญหา (Define Problem and Problem Analysis)

2. เขียนผังงาน และ ซูโดโคด (Pseudocoding)

3. เขียนโปรแกรม (Coding)

4. ทดสอบและแกไขโปรแกรม (Program Testing and Debugging)

5. ทำเอกสาร และบำรุงรักษาโปรแกรม (Program Documentation and Maintenance)

pseudocode

เขียนโปรแกรมโดยใชภาษามนุษยทั่วไป

ใชถอยคำผสมระหวางภาษาอังกฤษ และภาษาโปรแกรม

คำเฉพาะ (Reserve Word) ที่อยูในภาษาโปรแกรม จะใชอักษรตัวใหญ

สั้น เขาใจงาย ไดใจความ

example: calculate average

Algorithm Average_Sum

1. count = 0

2. sum = 0

3. INPUT (value)

4. IF value > 0 THEN

count = count + 1

sum = sum + value

GOTO 3

ELSE GOTO 5

5. average = sum / count

6. OUTPUT (average)

7. END

flowchart

Flowchart คือ แผนภาพที่ใชออกแบบอธิบายการทำงานของโปรแกรม รวมทั้งทิศทางการไหลของขอมูล Flowchart มีสองแบบคือ System Flowchart และ Program Flowchart

การเขียนผังงานที่ดี

เขียนตามสัญลักษณที่กำหนด

ใชลูกศรแสดงทิศทางการทำงานจากบนลงลาง

อธิบายสั้น เขาใจงาย

ทุกแผนภาพตองมีทิศทางเขาออก

ไมควรโยงลูกศรไปที่ไกลมาก ๆ ถาตองทำ ใหใชสัญลักษณเชื่อมตอแทน

สัญลักษณ์ใน flowchart

การเริ่มตนและการสิ้นสุดการทำงานของโปรแกรม

ลูกศรแสดงทิศทางการทำงานของโปรแกรมและการไหลของขอมูล

การประมวลผลการคำนวณตาง ๆ

การทำงานยอย

การรับหรือแสดงขอมูล โดยไมระบุชนิดของอุปกรณ

การตรวจสอบเงื่อนไข เพื่อเลือกทำอยางไดอยางหนึ่ง

แสดงผลลัพธหรือรายงานทางเครื่องพิมพ

จุดเชื่อมตอผังงาน

example

เริ่มต้น

อ่านค่า A และ B

คํานวณ X=A+B

ทําทําต่อหรือไม่

แสดงผลรวม X

จบ

ไม่ทํา

รูปแบบการจัด Flowchart

รูปแบบการจัด Flowchart ที่นิยมมี 5 รูปแบบ คือ

โครงสรางการทำงานแบบลำดับ (Sequence)

โครงสรางการทำงานแบบมีการเลือก (Selection)

ผังโปรแกรมทำซ้ำแบบเงื่อนไขเปนจริง

ผังโปรแกรมทำซ้ำแบบจนเงื่อนไขเปนจริง

ผังโปรแกรมแบบทำซ้ำตามจำนวนที่ระบุ

โครงสร้างการทํางานแบบลําดับ (Sequence)

เริ่มตน

รับคาตัวแปร

ทำชุดคำสั่งที่ 1

จบ

แสดงผล

ทำชุดคำสั่งที่ 2

โครงสร้างการทํางานแบบมีการเลือก (Selection)

การเลือกแบบหนึ่งเสนทาง

การเลือกแบบสองเสนทาง

Noชุดคำสั่ง

ตรวจสอบเงื่อนไข

จริงตรวจสอบเงื่อนไข

เท็จ

ชุดคำสั่งที่2ชุดคำสั่งที่1

ผังโปรแกรมทําซ้ําแบบเงื่อนไขเป็นจริง

จะใชในงานที่มีการตรวจสอบเงื่อนไข ถาเปนจริงจะทำงานซ้ำ โดยจะตรวจสอบเงื่อนไขกอนทำงานทุกครั้ง

ตรวจสอบเงื่อนไขเท็จ

จริง

ออกจากทำซ้ำ

ชุดคำสั่งที่ตองทำงานซ้ำ

ผังโปรแกรมทําซ้ําแบบจนเงื่อนไขเป็นจริง

จะใชในระบบที่ตองทำงานกอนตรวจสอบเงื่อนไข และทำงานซ้ำจนเงื่อนไขเปนจริง

ตรวจสอบเงื่อนไข

เท็จ

จริงออกจากทำซ้ำ

ชุดคำสั่งที่ตองทำงานซ้ำ

ผังโปรแกรมแบบทําซ้ําตามจํานวนที่ระบุ

ใชในระบบที่ตองทำงานตามจำนวนรอบที่กำหนด โดยเริ่มจากรอบเริ่มตนไปยังรอบสุดทาย ตามปกติแลว คาการนับรอบจะเพ่ิมขึ้นครั้งละหนึ่งคา

FOR i=1 TO N

ชุดคำสั่งที่ตองการแกไข

Workshop 1Grading Program

Requirement

ผูใชปอนคะแนนดิบ โปรแกรมจะทำการตัดเกรดให วาอยูในชวงใด

80 - 100 ไดเกรด A

70 - 79 ไดเกรด B

60 - 69 ไดเกรด C

50 - 59 ไดเกรด D

ต่ำกวา 50 ไดเกรด F

development process

Define problem : โปรแกรมสามารถตัดเกรดไดทีละคน (narrow down the problem)

Your task: Draw the Flowchart and write pseudocode

!! BREAK !!

Getting to know Cthe first C program

โครงสร้างของ C-program

Preprocessor directives

Global Declarations

The main() function

User-defined function

Program comments

/*Programmed BY CHUNNY*/

# include "stdio.h"main(){   printf("Hello World !!\n");}

Preprocessor directive

Denoted by the directive sign (#) followed by the name of a program or a variable

Sometimes called “Header Part”

For example

“# include” tells the compiler to read in other files when compile

2 ways of writing “include”

“# define” defines a variable

# include “stdio.h”# include “Pro1.c”

หมายความว่า อ่านไฟล์ stdio.h เข้ามาด้วยหมายความว่า อ่านไฟล์ Pro1.c เข้ามาด้วย

# include “.......”# include <.......>

Read from current directoryRead from other predefined location

# define END 20# define A 5*6+3

Define END to be 20Define A to be 5*6+3

global declaration

Declare any variables or functions that will be used globally in the program

May be omitted

Will see this later on

main() function

Every program must have one main() function

The program starts working from here

Contains commands, functions, instruction for the program to works

Here is how it looks like

main(){

instruction 1;instruction 2;.........

}

user-defined functions

Functions that you write by yourself and will be used in the program

# include “stdio.h”main(){

.....function(); /*call the function*/.....

}

function(){

.... /*instruction for this function*/

....return ; /*Return the result of this function*/

}

Program comments

Explain the program: Make it easy to understand the program; easier to develop

Will not be read by the compiler

Good comment = Good programming !!!!

Here is how you write it

Use indentation system and Be consistent !!

/*..........your comments go here..........*/

A first C-program

Here is your first program;

“# include” is called directive “stdio.h” is called header file

Escaping sequence “\n” means new line

Header : อ่านไฟล์ stdio.h เข้ามาด้วยmain function

printf(“This is”);prinff(“a book”);

printf(“This is \n”);prinff(“a book”);

output

output

This is a book

This isa book

/*Programmed BY CHUNNY*/

# include "stdio.h"main(){   printf("Hello World !!\n");}

Comment : อธิบายโปรแกรม

Variable

Think of a “variable” as a “Box” that contains data

Variable name cannot be longer than 63 characters.

Variable must match with your data Data Type (will be mentioned later on)

int stores integer ranges from -32,768 to 32,767

use “=” to assign a data to a variable

# include “stdio.h”int feet, inches;main(){

feet = 6;inches = feet * 12;printf(“Height in inches is %d”, inches);

}

Store integer

Control String

How variables store data

Variable as a Box: You can put something in or take something out

Examples

More examples

#include “stdio.h”int x;main(){

x = 6;x = 8;printf(“X is %d”,x);

}

num1 = 5;num2 = 14;num1 = num2;printf(“%d”, num1);printf(“%d”, num2);

What is the output?What is the output?

count = count + 1;sum = sum + x;num = 3*num;

Type of variable

คอมพจะใชหนวยความจำในการเก็บ โดย 8 bit = 1 byte เก็บไดในชวง 0-255

Type Range

char -128 ถึง 127

int −32768 ถึง 32767

long int -2,147,483,648 ถึง 2,147,483,647

float 3.4E +/- 38 (7 หลัก)

double 1.7E +/- 308 (15 หลัก)

กฎการตั้งชื่อตัวแปร

คำสงวนใน ANSI Stand C มีดังนี้ : auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

อยาเอาตัวเลขนำหนา อยาใหมาตรงกับคำสงวน

อยาใหมีเครื่องหมายใดลอยนวล ไมสมควรมีชองวางใด ๆ เลย

การประกาศตัวแปร

เปนการสรางตัวแปรขึ้นมาใชงาน เพื่อเก็บขอมูลตาง ๆ

รูปแบบการประกาศ คือ type variable_list; เชน

int data1, data2;

int data1;float data2;

int data1 = 5;float data2 = 2.653;

การประกาศค่าคงที่การประกาศคาคงที่ (constant)

อยูในหนวยความจำ และมีคาคงที่ตลอดโปรแกรม

ประกาศคาคงที่โดย ประกาศเปน constant

ไมกำหนดคา เชน const int a;

กำหนดคา เชน const int count = 100;

ประกาศคาคงที่โดย #define

เปนคำสั่งสำหรับนิยามคา

ไมตองระบุประเภทของขอมูล

ไมตองมีเครื่องหมาย ; ตอทาย

ตัวอยาง #define PI 3.14 หรือ #define X (5+3)/2

ใชตัวใหญในการตั้งชื่อตัวแปรคาคงที่

การเปลี่ยนประเภทของข้อมูล (Data Type Casting)

โดยปกติ Operator จะใชกับนิพจนประเภทเดียวกัน เชน เรานำ int บวกกับ int ได int

บางครั้ง เราจำเปนตอง นำ int บวกกับ float ซึ่งผลลัพธจะได float แตเราตองการผลลัพธเปน int เราจึงตองเปลี่ยนผลลัพธที่ได ใหเปน int โดยการ Casting

Example: ตองการเปลี่ยนคาที่ไดจาก a คูณ 2.5 ใหมีคาเปนจำนวนเต็ม

ใชรูปแบบคำสั่ง คือ (int)(a*2.5);

!!!! Be careful !!!!!

Quiz: Right or wrong

int age;

double grade2;

char 1ch;

int name$;

int numofpizza;

long int a = 40;

char int;

char my salary;

double ratio_rate = 100;

short 123free;

Basic input output function

printf(“ ”); scanf(“ ”); and other input functions

3 ways of using printf(“...”);

For example;

printf(“Your text goes here”);printf(“control String”, variable);printf(“ control Strings”, variable list,.....);

printf(“My name is Chunny”);printf(“I am %d years old”, myAge);printf(“%f = %d %s”,dollars, baht, currency);

Escape sequence (รหัส backslash)

Code Meaning

\n New line

\t Tab (8 characters)

\xhh Display a character when hh is a hexadecimal

\a “Beep” sound

\\ Backslash sign

Control String

Control String Meaning

%d Type a decimal number

%u Unsigned integer

%f Floating point

%e Type Real Number in the power form

%c Type a character

%s Type a string

%% Type a “%”

%o Type octal number

%x Type Hexadecimal number

scanf()

Read the input from the keyboard as ASCII code

Using Control String to define the type of the variable and data

Don’t forget the “&” sign which means it points to an address in the memory

For examples;

For a string, don’t need the “&” sign

Problem !! Can’t read a string contains spaces (use gets() instead)

scanf(“control string”, &variable list, ......);

int num;scanf(“%d”, &num);

int i, j;scanf(“%o %x”, &i &j);printf(“%o %x” i, j);

char str[80];printf(“Enter a string : ”);scanf(“%s”, str);printf(“Here’s your string: %s”, str);

no “&”

Other input/output functions

Function Syntax What it does

gets() gets(str) Read String input

fgets() fgets(str) Read String input (fixed length)

getch() getch(cha) Read character input (don’t have to press enter)

getchar() getchar(cha) Read character input (must press enter)

puts() puts(str) Output a string to a monitor

putchar() putchar(cha) Output a character to a monitor

String Type

String คือการนำเอาอักขระ (char) มาเรียงตอ ๆ กัน โดยสามารถเก็บตัวอักขระได 255 ตัว

In the other words, A String is an array of characters

อยูในเครื่องหมาย “ ” ในการเขียนโปรแกรม และมีการเติมอักษรวาง NULL (\0) เปนตัวสุดทาย ทั้งนี้ เพื่อเปนการบอกวาถึงจุดสิ้นสุดของ String นั้นๆ แลว

เชน ตองการเก็บคำวา “COMPUTER” ไวใน String จะใชเนื้อที่ในการเก็บ 9 bytes

“C” “O” “M” “P” “U” “T” “E” “R” “\0”

Workshop 2Creating c programs

Operatorby: Tulawat Chunwijitra

Operator

ตัวดำเนินการเลขคณิต (Arithmetic Operators)

ตัวดำเนินการเปรียบเทียบ (Relation Operators)

ตัวดำเนินการตรรก (Logical Operator)

ตัวดำเนินการแบบบิต (Bitwise Operators) (ไมเรียน)

ตัวดำเนินการลำดับการใหคา (Comma Operators)

Arithmetic operator

ตัวดำเนินการ กระบวนการ ขอมูลที่ถูกกระทำ ขอมูลผลลัพธ

+ บวก จำนวนเต็ม, จำนวนจริง จำนวนเต็ม, จำนวนจริง

- ลบ จำนวนเต็ม, จำนวนจริง จำนวนเต็ม, จำนวนจริง

* คูณ จำนวนเต็ม, จำนวนจริง จำนวนเต็ม, จำนวนจริง

/ หาร จำนวนเต็ม, จำนวนจริง จำนวนจริง

% หารเอาเศษ จำนวนเต็ม จำนวนเต็ม

++ เพิ่มค่าขึ้น 1 จำนวนเต็ม จำนวนเต็ม

-- ลดค่าลง 1 จำนวนเต็ม จำนวนเต็ม

Example

3 + 4 = 7

7.0 - 3.0 = 4.0

6 * 1.5 = 9.0

1.5 - 1 = 0.5

2.25 * 1.5 = 3.375

5.8 + 3 = 8.8

9/2 = 4.5

9 % 2 = 1

18 % 2 = 0

-14 % 3 = -2

14 % -3 = 2

18.2 / 2 = 9.1

NOTE:

• จำนวนจริง ไปกระทำกับเลขใด จะได้จำนวนจริง

• Real/Real = Real

• Real/Int = Real

• Int/Real หารไม่ได้

Operator Precedence

ลำดับกอนหลังของ Operator แสดงในตารางดานลาง

Precedence Operator Note

1 ( ) ถ้าหากมีวงเล็บซ้อนกัน ทำวงเล็บในสุดก่อน

2 *, /, %ถ้าหากมีตัวดำเนินการลำดับขั้นเดียวกันในประโยค

ให้ทำจากซ้ายไปขวา

3 +, -ถ้าหากมีตัวดำเนินการลำดับขั้นเดียวกันในประโยค

ให้ทำจากซ้ายไปขวา

Examples

จงหาผลลัพธ 12 % 5 * 3

ตอบ 6 เปนจำนวนเต็ม

จงหาผลลัพธ 6 * 5 / 10 * 2 + 10

ตอบ 16.0 เปนจำนวนจริง

จงหาผลลัพธ (6 * 5) / (10 * 2) + 10

ตอบ 11.5 เปนจำนวนจริง

จงหาผลลัพธ (6 * 5) / (10 * 2 + 10)

ตอบ 1.0 เปนจำนวนจริง

จงหาผลลัพธ (6 * 5) / (10 * (2 + 10))

ตอบ 0.25 เปนจำนวนจริง

ตัวดําเนินการเปรียบเทียบ (Relation Operator)

นำขอมูลสองคามาเปรียบเทียบกัน

ผลที่ได คือ ขอมูลชนิดตรรกะ เชน จริง หรือ เท็จ เชน 6>2 ไดผลลัพธคือ จริง

Operator Process

== เท่ากับ

!= ไม่เท่ากับ

<= น้อยกว่าหรือเท่ากับ

>= มากกว่าหรือเท่ากับ

> มากกว่า

< น้อยกว่า

Logical Operator

มีสามตัว คือ AND , OR และ NOT

Example: Is this true or false? (5 == 4+1)&&(18<=6*4)

Answer: True

Operator Process

&& ถ้าค่าทั้งสองเป็นจริง ผลลัพธ์จะเป็นจริง

|| ถ้าค่าทั้งสองเป็นเท็จ ผลลัพธ์จะเป็นเท็จ

! เปลี่ยนค่าจากจริงเป็นเท็จ จากเท็จเป็นจริง

Precedence

Precedence Operator

1 !

2 > >= < <=

3 != ==

4 &&

5 ||

Increment and Decrement Operator

การเพิ่มคา x ขึ้น ทีละ 1 ใชคำสั่ง x = x+1; หรือ x++;

การลดคา x ลง ทีละ 1 ใชคำสั่ง x = x-1; หรือ x--;

คำถาม ผลลัพธของสองชุดคำสั่งดานลาง คืออะไร เหมือนกันหรือไม

ตอบ ไมเหมือน เพราะทางซายเพิ่มคา x ขึ้น 1 กอนสงใหตัวแปร y ซึ่งจะได y = 11 สวนทางดานขวา สงคา x ใหตัวแปร y กอน จึงเพิ่มคา x ขึ้น 1 ดังนั้น x = 11 สวน y = 10

x = 10;y = ++x;

x = 10;y = x++;

Compound Assignment

Operators บางตัว สามารถนำมารวมกันได เพื่อการเขียนที่สั้นลง เชน...

Operator Example Process*= total *= 1.25; total = total * 1.25;

/= ant /= factor; ant = ant / factor;

%= day %=3; day = day % 3;

+= count += 1; count = count + 1;

-+ adjust -= 0.5; adjust = adjust - 0.5;

<<= a <<= b a = a << b;

&= a &= b a = a & b;

|= a |= b a = a | b;

^= a ^= b a = a ^ b;

Workshop 3

flow controlBy: Tulawat Chunwijitra

Outline

if , if-else , if-elseif-else

switch-case

for loop, while loop, do while loop

IF

ในการเขียนโปรแกรม บางครั้ง จะตองการใหโปรแกรมมีทางเลือก เพื่อที่จะทำอยางใดอยางหนึ่ง เชน การถามวาคุณตองการออกจากโปรแกรมหรือไม -----> Quit (Y/N)

ในการเลือกทำ เราใชคำสั่งสองคำสั่ง คือ if และ if...else...

รูปแบบ

ถา เงื่อนไข (condition) เปนจริง จะทำคำสั่งที่ตามมา และถา condition เปนเท็จ จะขามชุดคำสั่งใน if ไป

เชน

if (condition){ statements;}

if (age >= 18){   printf(“of age \n”);}printf(“good luck”);

If flow chart

ตรวจสอบเงื่อนไข

ชุดคําสั่ง

จริง

เท็จ

Nested if...else...

if...else... สามารถซอน ๆ กันหลาย ๆ ชั้นได ดังรูป

if (condition1){   statements1;}else if (condition2){  statements2;}else if (condition3){  statements3;}else{ statements;}

switch case

Switch - case ใชกับเงื่อนไขที่ตองตัดสินใจขึ้นกับตัวแปรตัวเดียว

เชน MENU

ผูใชเลือกวิธีคำนวณโดยการปอนคา input เขาไป โดยเรานำคา input ที่รับเขาไปเก็บไวในตัวแปรตัวหนึ่ง และใชคำสั่ง switch เลือกวามีคาเทากับขอใด

MENU1. คำนวณบวกเลข2. คำนวณคูณเลข3. คำนวณหารเลข4. คำนวณลบเลข

syntax

switch (variable){

case constant_1 : statement; break;

case constant_2 : statement; break;

case constant_3 : statement; break;

case constant_4 : statement; break;

..................... .................case constant_n : statement;

break;default : statement;

}

ตัวแปรที่จะทดสอบ

ถ้าค่าของ variable ไม่ตรงกับ case ใดเลย ก็จะทํา statement หลัง default

ถ้าค่าของ variable ตรงกับ Case ใด ก็จะทํา statement

หลังค่าคงที่นั้น ๆ

selector = const 1

selector = const 2

selector = const n

default

ชุดคำสั่ง 1

ชุดคำสั่ง n

ชุดคำสั่ง 2

จริง

จริง

จริง

ข้อควรทราบ

คาคงที่หลัง case จะตองเปน int หรือ char เทานั้น

คำสั่ง switch ตางจากคำสั่ง if ตรงที่ switch สามารถทดสอบเงื่อนไขไดหลายอยาง แตคำสั่ง if จะตรวจสอบเฉพาะความสัมพันธ หรือ ลอจิกเทานั้น

คาคงที่ของ switch สามารถมีคาซ้ำกันได

ถาคาคงที่เปนตัวอักษร คำสั่ง switch จะมองเปนเลขจำนวนเต็ม

คา default จะมีหรือไมมีก็ได

Example

switch (year){

case 1: printf(“Fresherman \n”); break;

case 2 : printf(“Sophormore \n”); break;

case 3 : printf(“Junior \n”); break;

case 4 : printf(“Senior \n”); break;

default : printf(“Nonmatriculated”);}

Selector

for loop

เปนการใหโปรแกรมทำซ้ำ จนกวาคาตัวแปรจะครบ ตามที่ตั้งไว

โดยจำมีการกำหนดคาเริ่มตน (initialize) ใหกับตัวแปรตัวหนึ่ง และทำการเพิ่ม/ลดคาตัวแปรนั้นไปเรื่อย จนกวาเงื่อนไข (condition) จะไมเปนจริง จึงหยุดทำ

for (initialization ; condition ; increment){

statements;}

ค่าเริ่มต้น เงื่อนไข เพิ่ม/ลดค่า

for loop

อาน “จาก counter=1 ถึง counter=10 ให print คาของ counter ออกทางหนาจอ”

โปรแกรมจะพิมพคา counter ตั้งแต 1 จนถึง 10 โดยมีการกำหนดคาเริ่มตนให counter ใหเปน 1 กอน แลวตรวจสอบวา counter นอยกวาหรือเทากับ 10 หรือไม ถาไม ใหทำ statement แลวเพิ่มคา counter

for (counter=1 ; counter<=10 ; counter++){

printf(“%d\n”,counter);}

flowchart

counter = 1

printf(“%d”,counter);counter<=10 จริง

เท็จ

คำสั่่งตอไป

counter++

while loop

ประโยคคำสั่ง while loop เปนการสั่งใหคอมพิวเตอรทำงานซ้ำ โดยจะ ตรวจสอบเงื่อนไขกอน

ถาเงื่อนไขเปนจริงจะทำซ้ำ และจะวนรอบจนกวาเงื่อนไขจะเปนเท็จ

ตางจาก for loop ตรงที่จำนวนที่ทำซ้ำจะไมแนนอน ขึ้นอยูกับเงื่อนไข

while (condition){

statements;}

Flow chart

ชุดคําสั่ง

ตรวจสอบเงื่อนไข

จริง

เท็จ

while

ออกจาก loop

Examples

Output

int n = 7;while(n>=0){

printf(“%d\n”,n);n = n-5;printf(“Hi %d\n”,n);

}

7Hi 22Hi -3

ArrayBy: Tulawat Chunwijitra

Outline

รูจักกับขอมูลชนิด array

ตัวแปร array 1 มิติ

ตัวแปร array 2 มิติ

ขอมูลชนิด string

array

array คือตัวแปรประเภทหนึ่งที่ใชชื่อตัวแปรชื่อเดียว แตสามารถเก็บขอมูลเปนกลุมได

มีทั้งแบบ 1 มิติ (1 D array) และ หลายมิติ

เมื่อเราประกาศตัวแปรเปน array มันจะจองเนื้อที่ในหนวยความจำไวใหเรา ตามขนาดที่เราระบุ โดยจะแบงเปนชอง ๆ

ขอมูลแตละตัวใน array หนึ่ง ๆ เราเรียกวา Element

การอางถึงขอมูลใน array เราจะใช index โดย index จะมีคาเริ่มตนจาก 0

Example

ตองการ array มาเก็บขอมูลคะแนนของนนร. 10 คน

ขอมูลคะแนน -> int

ขนาด = 10

int x[10];

x[2] = 35 , x[1+4] = x[5] = 21

x[2]+x[3] = 35+84 = 119

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

18 20 35 84 81 21 45 65 71 39

index

array 1 มิติ

เก็บขอมูลตอเนื่องกันไปเปนแถว

syntax

ตัวอยาง

int salary[20];

Computer จะกันหนวยความจำไวให 20 ชอง

การอางถึงขอมูลที่เก็บไวใน array ใช index เชน salary[5]

type variable_name[size];

ขอควรจำ !!!

ขอมูลใน array ทุก element

จะตองเปนขอมูลประเภทเดียวกัน

Example

Question : ชุดคำสั่งตอไปนี้ ทำอะไร ?

Answer : ใสขอมูล 0 ถึง 4 เขาไปในตัวแปร array

int i[5];int j;for(j=0 ; j<5 ; j++){

i[j] = j;}

array (cont.)

คำสั่ง scanf () สามารถใชรวมกับ array ได เชน

scanf(“%d”,&count[9])

รับคาจาก keyboard แลวไปเก็บไวในชองที่ 10 (index = 9)

เราสามารถกำหนดคาเร่ิมตนใหกับตัวแปร array ได โดยใช syntax ตอไปนี้

เชน

int A[5] = {1, 4, 9, 16, 25};

char a[3] = {‘A’, ‘B’, ‘C’};

type array-name[size] = {value-list}

More about String

String คือ ชุดของตัวอักษรที่นำมาตอเรียงกัน

ดังนั้น String ก็เปน array ชนิดหนึ่ง

อักขระที่บงบอกวาถึงจุดสิ้นสุดของ string นั้น ๆ คือ “\0”

Example: char name[8];

เก็บขอความได 7 อักขระ

ตัวสุดทาย เก็บ “\0”

Example: char str[8] = “BAT”;

str[0] = ‘B’ , str[1] = ‘A’ , str[2] = ‘T’ , str[3] = ‘\0’

scanf() VS gets()

ลองรัน code ตัวอยางตอไปนี้ แลวสังเกตุดูความแตกตาง

ปญหาของ scanf () ในการรับคา คือ มันไมสามารถรับคา string ที่มีการเวนวรรคได

วิธีแก gets() ใชสำหรับรับคา string จาก keyboard

#include "stdio.h"main(){ char str[80]; printf("Enter a string : "); gets(str); printf("%s",str);}

#include "stdio.h"main(){ char str[80]; printf("Enter a string : "); scanf("%s",str); printf("%s",str);}

Some useful string functions

strlen(str) ใชหาความยาวของ string

strlwr(str) ใชเปลี่ยน string ใหเปนตัวพิมพเล็ก

strupr(str) ใชเปลียน string ใหเปนตัวพิมพใหญ

strrev(str) ใชกลับตัวอักษรใน string จากซายไปขวา

strcpy(str2,str1) คัดลอก string str1 ไปใหตัวแปร str2

strncpy(str2,str1,n) คัดลอก str1 ไป str2 เปนจำนวน n ตัว

strcat(str2,str1) เชื่อมตอ str1 กับ str2

strcmp(str2,str1) เปรียบเทียบตัวแปร str1 กับ str2 วาเทากันหรือไม

FunctionBy : Tulawat Chunwijitra

Function• Function คือกลุมคำสั่งที่ถูกสรางขึ้นมาเพื่อใหทำงานอยางใดอยาง

หนึ่ง

Function

User-Defined Function

Standard Function

ไม่ส่งค่ากลับส่งค่ากลับไม่ส่งค่ากลับส่งค่ากลับ

ประเภทของ function• function ในภาษา c ถูกแบงออกเปน 2 ประเภท (ตามที่มา)

• ฟงกช่ันมาตรฐาน (Standard Function)

• มีอยูแลว และเก็บไวใน library

• เชน clrscr(); , puts() และอื่น ๆ

• ฟงกชั่นที่ผูเขียนโปรแกรมสรางขึ้น (User-defined

Function)

• เปนฟงกช่ันยอยที่ผูใชสรางขึ้นมาไวใชเอง เพื่อทำงานใดงาน

หนึ่ง

• สามารถเรียกใชไดตามตองการ

Benefit• เรียกใชไดหลายครั้งตามตองการ (Reusable)

• ทำใหโปรแกรมดูงาย และมีระเบียบมากขึ้น

• สะดวกตอการพัฒนาโปรแกรม (สามารถแยกสวนของโปรแกรมไป

ชวยกันพัฒนาได)

#include "stdio.h"void main(){ ..... computeAverage(); ..... computeAverage(); .....}

void computeAverage(){ //code for computing your average //goes here.}

call

การสรางฟงกชั่น

ประเภทของข้อมูล ชื่อฟังก์ช่ัน (พารามิเตอร์){ statements; statements; ....... [return];}

การสรางฟงกชั้น (ตอ)• ประเภทของขอมูล หมายถึงประเภทของขอมูลที่สงคากลับ จะมี

หรือไมมีก็ได

• void หมายถึง ฟงกชันนั้นๆ ไมสงคากลับ

• ชื่อฟงกชัน ชื่อของฟงกชันที่สรางขึ้น สามารถเรียกใชงานผานชื่อนี้

ได

• พารามิเตอร์ เปนตัวแปรที่ถูกสงผานเขามา เพื่อประมวลผลใน

ฟงกชันนั้น ๆ จะมีหรือไมมีก็ได

• return ใชในการสงคากลับ จะมีหรือไมมีก็ได สำหรับกรณีที่มีการ

สงคากลับ จะตองเขียนคาที่สงกลับตามหลัง return

More Example

void show_A(){ int i; for (i=1; i<20; i++) printf("A");}

void show_A(int num){ int i; for (i=1; i<num; i++) printf("A");}

int add_AB(int A,int B){ int x; x = A+B; return x;}

#include "stdio.h"void main(){ int salary=10000, partTime=40000, totalIncome; show_A(); show_A(50); totalIncome = add_AB(salary,partTime);}

Example• An overall view of a program with one user-defined

function (return a value as an integer)

#include "stdio.h"int add(int a,int b){ int c; c = a + b; return c;}main(){ clrscr(); printf(" 3 + 5 = %d\n",add(3,5)); getch(); return 0;}

ขอสังเกต• User-Defined Functions มากอน แลวจึงตามดวย main()

เปนตัวสุดทาย

• มี void ไมมี return มี return ไมมี void

• main() ก็เปนฟงกช่ัน ฟงกชั่นหนึ่งเหมือนกัน และทุก ๆ

โปรแกรมตองมี

• โปรแกรมเริ่มทำงานจาก main()

• ฟงกชั่นอื่น ๆ จะถูกเรียกใชจาก ฟงกชั่น main()

• ขอสังเกตุเพิ่มเติม ??

Prototypes• โดยปกติ เราวาง User-defined function ไวกอน แลวปดทาย

โปรแกรมดวย main()

• ถาเราตองการเรียกใช function ที่อยูหลัง main() เราตอง

ประกาศชื่อฟงกชั่น นั้น ๆ ไวที่สวนหัวของโปรแกรมกอน

• โดยจะประกาศวา

• ฟงกชั่นนี้ชื่ออะไร

• มีพารามิเตอรใดบาง

• คืนคาอะไรกลับมา

• วิธีนี้ เราเรียกวา Prototypes

Example• Prototype: ประกาศ volumn () ไวกอน โดยบอกชื่อ,

พารามิเตอร และคาที่คืนกลับมา

#include "stdio.h"double volumn(double s1,double s2,double s3);

main(){ double vol; vol = volumn(12.2,5.67,9.03); printf("Volumn : %f",vol); getch(); return 0;}double volumn (double s1,double s2,double s3){ return s1*s2*s3;}

Global vs Local Variable

• ในการเขียนโปรแกรม เราสามารถประกาศตัวแปรใน function ที่

เราเขียนขึ้น

• เราไมสามารถนำตัวแปรที่ถูกประกาศในฟงกชั่นใดฟงกชั่นหนึ่ง ไป

ใชนอกฟงกชั่นนั้น ๆ

• ตัวแปรแบบนี้เรียกวา local variable

• วิธีแก : เราตองประกาศตัวแปรในลักษณะของ global variable

Global Variables• ตัวแปรที่ประกาศไวนอกฟงกชั่น

• ทุกสวนของโปรแกรมสามารถเรียกใชได

• คาของมันจะถูกเปลี่ยนแปลงไปเรื่อย ๆ ตามที่ฟงกชั่นนั้น ๆ เรียก

ข้อควรระวัง

Data Synchronization อย่าลืมว่าทุกส่วนของโปรแกรมสามารถเปลี่ยนแปลงค่าของ global variable ได้ ดังนั้น ต้อง keep track ให้ดี ๆ ว่า ตัวแปรนั้นเก็บค่าอะไรอยู่กันแน่

Local variable

• ประกาศในฟงกชั่นหนึ่ง ๆ

• สามารถเรียกใชไดแคในฟงกชั่นนั้น ๆ ฟงกชั่นอื่นจะไมรูจักตัวแปร

นี้

• ฟงกชั่นอื่นสามารถประกาศตัวแปรชื่อซ้ำกับตัวแปรนี้ได

• ชื่อตัวแปรชื่อเดียวกัน แตประกาศในคนละฟงกชั่น ถือวาเปน

ตัวแปรคนละตัวกัน

• เมื่อฟงกชั่นนั้นทำงานจบ คาของตัวแปรชนิดนี้จะหายไป

Example• ตัวแปร a ถูกประกาศแบบ global

#include "stdio.h"int a;void Ex(){ a = 5; printf("%d\n",a);}main(){ a = 3; printf("%d\n",a); Ex(); printf("%d\n",a); getch(); return 0;}

Example

#include "stdio.h"int a;void Ex(){ int a; a = 5; printf("%d\n",a);}main(){ clrscr(); a = 3; printf("%d\n",a); Ex(); printf("%d\n",a); getch(); return 0;}

ผลการรันโปรแกรม

353

• ชื่อของ global variable และ local variable อาจซ้ำกันได

Basic standard function math.hsqrt(x) หาค่ารากที่สองของจํานวนเต็ม x

exp(x) หาค่า Exponential (ex)

log(x) หาค่า log ฐาน e

log10(x) หาค่า log ฐาน 10 ของ x

fabs(x) หาค่าสัมบูรณ์ของ x

ceil(x) ใช้หาค่าปัดเศษทศนิยมของ x

floor(x) ใช้หาค่าตัดเศษทศนิยมของ x ทิ้งไป

pow(x,y) ใช้หาค่า x ยกกําลัง y

sin(x) ใช้หาค่า sin ของ x

cos(x) ใช้หาค่า cos ของ x

tan(x) ใช้หาค่า tan ของ x

? Question ?