Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

63
Computer Programming กกกกกกกกกกกกกกกกกกกกกกกกกก กกกกกกกกกก 9 กกกกกกกกกกกกกกก (Array)

description

Computer Programming การเขียนโปรแกรมคอมพิวเตอร์. สัปดาห์ที่ 9 ตัวแปรแถวลำดับ (Array). Outline. 1. Objective. 2. Why an Array is Essential?. p. 3. One-Dimensional Array. Two-Dimensional Array. 4. 5. Assignment #5. objectives. เพื่อให้นิสิตรู้จักและเข้าใจ ตัวแปรแถวลำดับ ในภาษาซี - PowerPoint PPT Presentation

Transcript of Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Page 1: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Computer Programmingการเขี�ยนโปรแกรมคอมพิ�วเตอร�

สั�ปดาห์�ที่�� 9ต�วแปรแถวลำ�าด�บ (Array)

Page 2: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Outline

Objective1

pp2

One-Dimensional Array One-Dimensional Array3

Two-Dimensional Array Two-Dimensional Array4

Assignment #5 5

Why an Array is Essential?

Page 3: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

objectives

เพิ��อให์!น�สั�ตร"!จั�กแลำะเขี!าใจัต�วแปรแถวลำ�าด�บในภาษาซี�

สัามารถเขี�ยนโปรแกรมภาษาซี�โดยใช้!ต�วแปรแถวลำ�าด�บได!

สัามารถน�าความร"!เร��องต�วแปรแถวลำ�าด�บไปประย+กต�เพิ��อแก!ไขีโจัที่ย�ป,ญห์าในช้�ว�ตประจั�าว�นได!ได!อย.างถ"กต!องเห์มาะสัม

Page 4: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Outline

Objective1

pp2

One-Dimensional Array One-Dimensional Array3

Two-Dimensional Array Two-Dimensional Array4

Assignment #5 5

Why an Array is Essential?

Page 5: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Why an array is essential?

ถ!าต!องการเก/บค.าคะแนนสัอบ (score) ขีองน�กเร�ยน จั�านวน 20,000 คน เราต!องใช้!ต�วแปร 20,000 ต�ว เพิ��อใช้!เก/บค.าคะแนนขีองน�กเร�ยนที่�0งห์มด

float score1, score2, score3, ..., score20000;

จัากขี!อจั�าก�ดขีองช้น�ดขี!อม"ลำพิ�0นฐานที่��ม�อย". (char, int, float, double) เราต!องใช้!ต�วแปรจั�านวนมาก เพิ��อเก/บขี!อม"ลำห์ลำายค.า

ภาษาซี�จั2งได!ก�าห์นดช้น�ดขี!อม"ลำแบบโครงสัร!างที่��รวมขี!อม"ลำพิ�0นฐานด�งกลำ.าวไว!เป3นลำ�าด�บห์ร�อเป3นกลำ+.ม

Page 6: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

The Advantage of Array

อาเรย� (array) เป3นโครงสัร!างขี!อม"ลำประกอบด!วยกลำ+.มขีองขี!อม"ลำช้น�ดเด�ยวก�นที่��เร�ยงก�นเป3นลำ�าด�บ โดยใช้!ต�วแปรเพิ�ยงต�วเด�ยวอ!างถ2งเช้.น ต�วแปร score เป3นช้น�ดขี!อม"ลำแบบอาเรย� ใช้!เก/บคะแนนจั�านวน 20,000 ค.าพิร!อมก�นได! เช้.น

ด�ช้น�ที่��ใช้!จัะเป3นเลำขีจั�านวนเต/ม เร��มจัาก 0 ตามลำ�าด�บ (0, 1, 2, ...)

คะแนนสัอบ

75.5

82.0

96.5

93.0

... 85.0 79.5

ด�ช้น� 0 1 2 3 ... 19,998

19,999

Page 7: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example 1 Regular Format

Example 1 : จังเขี�ยนโปรแกรมเพิ��อร�บรห์�สัน�กศึ2กษา แลำะคะแนนสัอบกลำางภาคว�ช้า Computers and Programming ขีองน�กศึ2กษาห์!อง 1 – 10

#include <stdio.h>#include<conio.h>int main () {

char id0001[9],id0002[9],id0003[9],...,id1158[9],id1159[9];

float point0001,point0002,point0003,...,pint158,point1159;

scanf ("%s",id0001);scanf ("%f",&point0001);...scanf ("%s",id1159);scanf ("%f",&point1159);return 0;

}

Page 8: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example 1 in Array Format

#include<stdio.h>#include<conio.h>

int main(){

char id [1159] [9]; float point [1159]; int i;

for (i=0;i<1159;i++){

scanf ("%s", id [ i ]);scanf ("%f", &point [ i ]);

}

return 0;}

Page 9: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Type of Array

1num[9]

num[0]

9

num[1]

-1

num[2]

-23

num[3]

44

num[4]

2

num[5]

6

num[6]

-9

num[7]

-22

num[8]

One-dimensional Array

Multi-dimensional Array

Row [0]

Row [1]

Row [2]

Row [3]

Column

[0] [1] [2] [3]

Column

Row

Depth [0]

[1] [2]

[3]

[0]

[1]

[2]

[0] [1] [2]

Page 10: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Outline

Objective1

pp2

One-Dimensional Array One-Dimensional Array3

Two-Dimensional Array Two-Dimensional Array4

Assignment #5 5

Why an Array is Essential?

Page 11: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

One-dimensional Array Variable Declaration

type array_name[size];

ต�วแปรแถวลำ�าด�บ อะเรย� ห์ร�อ อาเรย� : “An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.”

type• ช้น�ดขีองต�วแปร array_name• ช้��อขีองต�วแปรแถวลำ�าด�บที่��ประกาศึห์ร�อต�0งโดยผู้"!เขี�ยนโปรแกรม size• ขีนาดขีองต�วแปรแถวลำ�าด�บที่��จัะใช้! (จัะใช้!เที่.าไรให์!จัองเผู้��อไว! 1

ช้.องสั�าห์ร�บ \0 (null string))

Page 12: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Declaration & Assignment Examples (Memory)

char student_name [5];

student_name [0] = ‘A’; student_name [2] = ‘B’;

int bill [5];

int billy [5] = { 16, 2, 77, 40, 12071 };

float Point [20];

Point [19] = 3.57;

double x_data [100];

x_data [99] = 9.86975;

32 bits, 2 complement

8 bits, 2 complement

Page 13: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Declaration & Assignment Examples (Value)

char student_name [5];

student_name [0] = ‘A’; student_name [2] = ‘B’;

int bill [5];

int billy [5] = { 16, 2, 77, 40, 12071 };

float Point [20];

Point [19] = 3.57;

double x_data [100];

x_data [99] = 9.86975;

A BRow [0]

[0] [1] [2] [3] [4]

Column

student_name

16 2 77 40 127Row [0]

[0] [1] [2] [3] [4]

Column

billy

3.57Row [0]

[16] [17] [18] [19] [20]

Column

Point

9.86975Row [0]

[96] [97] [98] [99] [100]

Column

x_data

Page 14: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Declaration & Assignment to Array

type array-name[n]={value-1,value-2,…,value-n};

type• ช้น�ดขีองต�วแปร เช้.น int, float, char

array_name• ช้��อขีองต�วแปรแถวลำ�าด�บที่��ประกาศึห์ร�อต�0งโดยผู้"!เขี�ยนโปรแกรม

value-1,value-2,…,value-n• เป3นขี!อม"ลำที่��จัะที่�าการก�าห์นดให์!ก�บต�วแปรแถวลำ�าด�บ โดยจัะต!อง

เป3นขี!อม"ลำช้น�ดเด�ยวก�บ type ที่��ก�าห์นด

Page 15: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

#include<stdio.h>

#include<conio.h>

int main(){

intnumber[3] = {23, -186, 43};

float value_2[5]={0.98,43.213,-3.47,52.08,-0.987};

char vowel[5] = {'a','e','i','o','u'};

char name[9] = {'E','n','g','i','n','e','e','r','\0'};

return 0;}

Declaration & Assignment Examples (cont.)

Page 16: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

References Data in Array

#include<stdio.h>

int main(){

int year[5] = {2552,2553,2554,2555,2556};

printf ("%d\n",year[0]);

printf ("%d\n",year[1]);

printf ("%d\n",year[2]);

printf ("%d\n",year[3]);

printf ("%d\n",year[4]);

return 0;}

Page 17: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example 1

Example 1 : จังเขี�ยนผู้�งงานแลำะโปรแกรมเพิ��อเก/บอาย+ขีองผู้"!ใช้!งานจั�านวน 20 คนโดยเก/บขี!อม"ลำอาย+ในต�วแปรช้น�ดอาร�เรย�Output Analysis• ไม.ม�

Input Analysis• อาย+ขีองผู้"!ใช้!งานที่�0ง 20 คนที่��ป6อนเขี!ามา

Process Analysis• สัร!างต�วแปรแถวลำ�าด�บขีนาด 20 เพิ��อเก/บค.าอาย+ โปรแกรมวน

รอบเพิ��อรอร�บค.าจัากผู้"!ใช้!งาน Variable Define• age[20] เป3นต�วแปรแถวลำ�าด�บช้น�ดจั�านวนเต/มขีนาด 20

เพิ��อใช้!เก/บค.าอาย+• count เป3นต�วแปรช้น�ดจั�านวนเต/มเพิ��อใช้!น�บจั�านวน

รอบขีอง for-loop

Page 18: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Process & Pseudo-code

Processเร��มต!นที่�างานที่�าการวนรอบเพิ��อร�บอาย+ขีองผู้"!ใช้!งานที่�0ง 20 คนที่��ป6อนเขี!ามา

แลำ!วเก/บไว!ในต�วแปรแถวลำ�าด�บจับการที่�างาน

Pseudo-codeBeginFor ( i = 1 && i < = 20 ) { Keep the age of users} End

Page 19: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

True

START

END

age[20],count

count = 0

age[count]

count++

count<20False

Page 20: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Code of Example 1

#include<stdio.h>#include<conio.h>

int main(){

int age[20],count;

for (count=0; count<20; count++){

printf ("Enter age[%d] : ",count);scanf ("%d",&age[count]);

}

printf ("Finish\n");return 0;

}

Page 21: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example 2

Example 2 : จังเขี�ยนผู้�งงานแลำะโปรแกรม เพิ��อร�บจั�านวนน�กศึ2กษาในห์!อง ห์ลำ�งจัากน�0น ให์!โปรแกรมรอร�บสั.วนสั"งขีองคน n คน แลำ!วว�เคราะห์�ว.าม�น�กศึ2กษาในห์!องม�สั.วนสั"งช้.วงต.างๆ จั�านวนก��คน

แลำ!วค�านวณสั.วนสั"งเฉลำ��ย แลำ!วแสัดงผู้ลำสั.วนสั"งขีองน�กศึ2กษาที่�0งห์มด

0 – 160 161 – 170

171 – 180 181 - 200

Page 22: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Problem Analysis

Output Analysis• จั�านวนน�กศึ2กษาที่��สั"งแต.ลำะช้.วง• สั.วนสั"งขีองน�กศึ2กษาเฉลำ��ยในห์!อง• สั.วนสั"งขีองน�กศึ2กษาที่�0งห์มดInput Analysis• จั�านวนน�กศึ2กษาที่�0งห์มด แลำะสั.วนสั"งขีองแต.ลำะคน

Process Analysis• โปรแกรมร�บจั�านวนน�กศึ2กษา• วนรอบเพิ��อร�บสั.วนสั"งเที่.าก�บจั�านวนน�กศึ2กษา• วนรอบเพิ��อตรวจัสัอบช้.วงสั.วนสั"งขีองน�กศึ2กษาแลำะห์าผู้ลำ

รวมสั.วนสั"งขีองน�กศึ2กษาที่+กคน• ค�านวณห์าค.าเฉลำ��ย

Page 23: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Variable Define• num เป3นจั�านวนเต/มเพิ��อเก/บจั�านวนน�กศึ2กษา• a เป3นจั�านวนเต/มเพิ��อตรวจัต�าแห์น.งต�วแปร

แลำะน�บรอบ• range1=0, range2=0, range3=0,

range4=0 เป3นจั�านวนเต/มสั�าห์ร�บเก/บค.าจั�านวนน�กศึ2กษาแต.ลำะช้.วง

• high[300] เป3นต�วแปรแถวลำ�าด�บช้น�ดที่ศึน�ยมเพิ��อเก/บสั.วนสั"ง

• avg = 0 เป3นจั�านวนที่ศึน�ยมเพิ��อเก/บค.าผู้ลำรวมแลำะค.าเฉลำ��ย

Problem Analysis (cont.)

Page 24: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Process

1 . เร��มต!นที่�างาน2. ที่�าการร�บจั�านวนน�กศึ2กษา3. วนรอบเที่.าก�บจั�านวนน�กศึ2กษาที่��ป6อนเขี!ามาเพิ��อร�บสั.วน

สั"งขีองน�กศึ2กษาแต.ลำะคน4. วนรอบเพิ��อตรวจัสัอบจั�านวนขีองน�กศึ2กษาที่��ม�ความสั"ง

ตรงก�บแต.ลำะช้.วงความสั"งแลำะห์าค.าผู้ลำรวมความสั"งขีองน�กศึ2กษาที่�0งห์มด

5. ห์าค.าเฉลำ��ยความสั"ง6. จับการที่�างาน

Page 25: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Pseudo Code

1. Begin2. Read amount of students3. Repeat 4. {5. Read each height of student6. Check and increase amount of

each range7. Calculate the summation of

height8. }9. Calculate the average height10. End

Page 26: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

True

num,arange1=0,range2=0range3=0,range4=0

high[300],avg=0

high[a]

a=0

START

num

a<num

a++

False

(2)

Page 27: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

False True

a++

<=160

<=170

<=180

range4++

range1++

range2++

range3++

F

T

F

T

F

T

a<num

avg=avg+high[a]

avg=avg/num

a=0

(3)

(2)

Page 28: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

END

(3)

Truea<num

a++

a=0

high[a]

range1range2range3range4

avg

False

Page 29: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

#include<stdio.h>#include<conio.h>

int main(){

int num,a,range1=0,range2=0,range3=0,range4=0;float high[300],avg=0;printf ("Please enter number of student : ");scanf ("%d",&num);

for (a=0; a<num; a++){

printf ("Student %2d : ",a+1);scanf ("%f",&high[a]);

}

Page 30: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

for (a=0; a<num; a++){

if (high[a]<=160)range1++;

else if (high[a]<=170)range2++;

else if (high[a]<=180)range3++;

elserange4++;

avg = avg + high[a];}

avg = avg/num;

Page 31: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

printf ("\n 0 - 160 : %3d",range1);printf ("\n161 - 170 : %3d",range2);printf ("\n171 - 180 : %3d",range3);printf ("\n181 - 200 : %3d",range4);printf ("\n\nAverage : %f ",avg);

for (a=0; a<num; a++){

printf ("%.2f ",high[a]);}return 0;

}

Page 32: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

String & Array variable

char subject[11] = {"C language"};or

char subject[11] = {'C', ' ', 'l', 'a', 'n', 'g', 'u', 'a', 'g', 'e', '\0'};

Csubject

[0] [1]

l

[2] [3] [4] [5]

a n g

[6] [7] [8] [9] [10]

u a g e \0

char name[9] = {"Engineer"};

Ename

[0]

n

[1]

g

[2] [3] [4] [5]

i n e

[6] [7]

e r

[8]

\0

Page 33: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example 3

#include<stdio.h>#include<conio.h>

int main (){

char sentence[22]="Welcome to my country";char word[9]={'T','h','a','i','l','a','n','d','\0'};char not_word[4]={'l','o','v','e'};printf ("Message1 = %s\n",sentence);printf ("Message2 = %s\n",word);printf ("Message3 = %s\n",not_word);return 0;

}

Page 34: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Outline

Objective1

pp2

One-Dimensional Array One-Dimensional Array3

Two-Dimensional Array Two-Dimensional Array4

Assignment #5 5

Why an Array is Essential?

Page 35: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Multi-dimensional Array Variable Declaration

type array_name[size_1][size_2];

type• ช้น�ดขีองต�วแปร เช้.น int, float, char array_name• ช้��อขีองต�วแปรแถวลำ�าด�บที่��ประกาศึห์ร�อต�0งโดยผู้"!เขี�ยนโปรแกรม size_1 or m• ขีนาดขีองต�วแปรแถวลำ�าด�บที่��จัะใช้!ม�ต�ที่�� 1 (ปกต�จัะจัองไว!สั�าห์ร�บ

ด�ช้น�ขีองแถว (row)) size_2 or n• ขีนาดขีองต�วแปรแถวลำ�าด�บที่��จัะใช้!ม�ต�ที่�� 2 (ปกต�จัะจัองไว!สั�าห์ร�บ

ด�ช้น�ขีองสัดมภ� (column))

type array_name[m][n];

Page 36: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Assignment Value to Multi-dimensional Array

type array-name[m][n]={value-1-1,value-1-2,…,value-1-m,value-2-1,value-2-2,…,value-2-m,…,value-n-1,value-n-2,…,value-n-m};

type• ช้น�ดขีองต�วแปร เช้.น int, float, char array_name• ช้��อขีองต�วแปรแถวลำ�าด�บที่��ประกาศึห์ร�อต�0งโดยผู้"!เขี�ยนโปรแกรม m• ขีนาดขีองต�วแปรแถวลำ�าด�บที่��จัะใช้!ม�ต�ที่�� 1 (ปกต�จัะจัองไว!สั�าห์ร�บด�ช้น�

ขีองแถว (row)) n• ขีนาดขีองต�วแปรแถวลำ�าด�บที่��จัะใช้!ม�ต�ที่�� 2 (ปกต�จัะจัองไว!สั�าห์ร�บด�ช้น�

ขีองสัดมภ� (column)) value-1-1, value-1-2, …, value-1-n, …, …, value-n-

m• เป3นขี!อม"ลำที่��จัะที่�าการก�าห์นดให์!ก�บต�วแปรแถวลำ�าด�บ โดยจัะต!องเป3น

ขี!อม"ลำช้น�ดเด�ยวก�บ type ที่��ก�าห์นด

Page 37: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Declaration & Assignment Examples

int matrix [2][2];

matrix [0][0] = ‘2’; matrix [0][1] = ‘7’;

matrix [1][0] = ‘9’; matrix [1][1] = ‘5’

double data_array [2][2];

data_array[2][2] = {1.0,2.0,3.0,4.0};

or data_array[2][2] = {{1.0,2.0},{3.0,4.0}}; or data_array[ ][2] = {{1.0,2.0},{3.0,4.0}};

2 7

9 5

1.0 2.0

3.0 4.0

Row [0]

Row [1]

Column

[0] [1]

Column

[0] [1]

[0]

[1]

Page 38: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

double data_array [2][2];

data_array[0][0] = 1.0;

data_array[0][1] = 2.0;

data_array[1][0] = 3.0;

data_array[1][1] = 4.0;

1.0 2.0

3.0 4.0

Declaration & Assignment Examples (cont.)

Row [0]

Row [1]

Column

[0] [1]

Page 39: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

int num[3][4] = { 11, 12, 13, 14,21, 22, 23, 24,31, 32, 33, 34 };

int num[3][4] = { 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34 };

31

32

33

34

21

22

23

24

11

12

13

14

31

32

21

22

11

12

num[0][0]

num[1][0]

num[2][0]

num[0][1]

num[1][1]

num[2][1] 34

24

14

num[0][3]

num[1][3]

num[2][3]33

23

13

num[0][2]

num[1][2]

num[2][2]

Declaration & Assignment Examples (cont.)

Page 40: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

float matrix[2][4] = { 0.19, -0.01, -0.23, 4.44,-4.44, 0.26, -0.09, -0.22 };

float matrix[2][4] = { 0.19, -0.01, -0.23, 4.44, -4.44, 0.26, -0.09, -0.22 };

-4.44

0.19

0.26

-0.01

-0.09

-0.23

-0.22

4.44

matrix[0][0] matrix[0][1] matrix[0][2] matrix[0][3]

matrix[1][0] matrix[1][1] matrix[1][2] matrix[1][3]

Declaration & Assignment Examples (cont.)

Page 41: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

char letter[2][4] = {'G', 'o', 'o', 'D','T', 'i', 'm', 'E'};

char letter[2][4] = {'G', 'o', 'o', 'D', 'T', 'i', 'm', 'E'};

G

letter[0][0]

o o D

letter[0][1] letter[0][2] letter[0][3]

T

letter[1][0]

i m E

letter[1][1] letter[1][2] letter[1][3]

Declaration & Assignment Examples (cont.)

Page 42: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

char str[2][10] = {"Computer", "Engineer" };

E n g i n e e r \0

C o m p u t e r \0

char str[2][10] = {"Computer", "Engineer" };

[0][8]

[1][8]

str[0]

str[1]

Declaration & Assignment Examples (cont.)

Page 43: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Multi-dimensional Example

#include <stdio.h>#define WIDTH 5#define HEIGHT 3

int square [HEIGHT][WIDTH];int n,m;

int main (){ for (n=0;n<HEIGHT;n++)

for (m=0;m<WIDTH;m++) { square[n][m]=(n+1)*(m+1); printf("square [%d][%d] = %d\n", n, m,square[n][m] ); }

return 0;}

Page 44: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example 4

Example 4 : จังเขี�ยนโปรแกรมเพิ��อร�บค.าจั�านวนเต/มในร"ปแบบเมตร�กซี�โดยเก/บค.าไว!ในต�วแปร แถวลำ�าด�บ ขีนาด 3 3 แลำ!วแสัดงผู้ลำเมตร�กซี�

Enter numbers [0][0] : 1Enter numbers [0][1] : 2Enter numbers [0][2] : 3Enter numbers [1][0] : 4Enter numbers [1][1] : 5Enter numbers [1][2] : 6Enter numbers [2][0] : 7Enter numbers [2][1] : 8Enter numbers [2][2] : 9

***Matrix*** 1 2 3 4 5 6 7 8 9

Page 45: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

#include<stdio.h>#include<conio.h>int main(){

int matrix[3][3],r,c;for (r=0; r<3; r++){

for(c=0; c<3; c++){

printf ("Enter numbers [%d][%d] : ",r,c);scanf ("%d",&matrix[r][c]);

}}printf ("\n*** Matrix ***\n");for (r=0; r<3; r++){

for(c=0; c<3; c++){

printf ("%5d ",matrix[r][c]);}printf ("\n");

}return 0;

}

Page 46: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example 5

Example 5 : จัากต�วแปรแถวลำ�าด�บที่��ก�าห์นดให์! จังเขี�ยนโปรแกรมห์าผู้ลำรวมขีองจั�านวนในแต.ลำะห์ลำ�ก แลำะผู้ลำรวมขีองจั�านวนในแต.ลำะแถว โดยเก/บค.าผู้ลำรวมไว!ในต�วแปร row [ ], column [ ]

int num[3][4] = { 1, 2, 3, 4,

2, 3, 4, 5,

3, 4, 5, 6 };

Page 47: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

*** Show Matrix *** 1 2 3 4 2 3 4 5 3 4 5 6

Sum of row[0] = 10Sum of row[1] = 14Sum of row[2] = 18Sum of column[0] = 6Sum of column[1] = 9Sum of column[2] = 12Sum of column[3] = 15

Page 48: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

#include<stdio.h>#include<conio.h>

int main(){

int num[3][4] = { 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6 };

int r,c,row[3]={0,0,0},column[4]={0,0,0,0};

/* Display Matrix */printf ("\n*** Show Matrix ***\n\n");

for (r=0; r<3; r++){

for(c=0; c<4; c++)printf ("%5d ",num[r][c]);

printf ("\n\n");}

Page 49: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

/* Summation Matric */for (r=0; r<3; r++)

for(c=0; c<4; c++){

row[r] = row[r] + num[r][c];column[c] = column[c] + num[r][c];

}

/* Display Summation */printf ("\n\n");for (r=0; r<3; r++)

printf ("sum of row [%d] = %d\n",r,row[r]);for (c=0; c<4; c++)

printf ("sum of column [%d] = %d\n",c,column[c]);return 0;

}

Page 50: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example 6

Example 6 : จังเขี�ยนผู้�งงานแลำะโปรแกรม เพิ��อร�บขี!อความเขี!ามาแลำ!วตรวจัสัอบว.าม�ที่�0งห์มดก��ประโยค

ว�เคราะห์�โจัที่ย� เขี�ยนขี�0นตอนการที่�างานอย.างลำะเอ�ยด เขี�ยนรห์�สัเที่�ยม เขี�ยนแผู้นภาพิการไห์ลำขีองขี!อม"ลำ เขี�ยนโค!ด

Page 51: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

START

END

count=0, c=0, message[100]

message

message[c]!=\0

c++

count

False

True

message[c]==POINT || message[c]==QU

count++

False

True

Page 52: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

#include<stdio.h>#include<conio.h>

int main(){

char message[100]; int count=0,c=0;printf ("******************************\n");printf ("* Program for count SENTENCE *\n");printf ("******************************\n");printf ("\n(One sentence must have '.' or '?')\n");printf ("Enter Message : ");gets(message);

while (message[c]!='\0'){

if (message[c]=='.' || message[c]=='?')count++;

c++;}

printf ("\n\nYour message has %d sentence(s).\n",count);

return 0;}

Page 53: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Exchange Value in Array

99 19 1 23 0 15 10 23-

26-9num[10]

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

99 19 1 23 0 15 10 23-

26-9num[10]

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

temp = num [0];num[0] = num [9];

num[9] = temp;

99 -9

num[0] num[9]

?

Temp

99-9 99

Page 54: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example 7

Example 7 : จังเขี�ยนโปรแกรมเพิ��อร�บจั�านวน 10 จั�านวน เพิ��อห์าค.ามากที่��สั+ดในจั�านวนที่��ร�บ โดยการย!ายค.ามากที่��สั+ดไปไว! Array ที่��ต�าแห์น.งสั+ดที่!าย

ว�เคราะห์�โจัที่ย� เขี�ยนขี�0นตอนการที่�างานอย.างลำะเอ�ยด เขี�ยนรห์�สัเที่�ยม เขี�ยนแผู้นภาพิการไห์ลำขีองขี!อม"ลำ เขี�ยนโค!ด

Page 55: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

0 19 1 23 99 15 10 23-

26-9num[10]

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

0 19 1 23 99 15 10 23-

26-9num[10]

0 1 19 23 99 15 10 23-

26-9num[10]

temp = num[1]; num[1]=num[2]; num[2] = temp;

0 1 19 23 15 99 10 23-

26-9num[10]

temp = num[4]; num[4]=num[5]; num[5] = temp;

0 1 19 23 15 10 23-

26-9 99num[10]

Page 56: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

#include<stdio.h>#include<conio.h>#define SIZE 10

int main(){

int num[SIZE],temp,n;for (n=0; n<SIZE; n++){

printf ("Enter num[%d] : ",n+1);scanf ("%d",&num[n]);

}

for (n=0; n<SIZE-1; n++){

if (num[n]>num[n+1]){

temp = num[n+1];num[n+1] = num[n];num[n] = temp;

}}printf ("The maximum number = %d",num[SIZE-1]);return 0;

}

Page 57: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Dynamic Array Allocation (malloc method

http://www.phy225.dept.shef.ac.uk/mediawiki/index.php/Arrays,_dynamic_array_allocation

Page 58: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

#include <stdio.h> #include <stdlib.h> int main(void) {

int i; int* i_array; // this will be the array int j;

// find out how many integers are required printf("How many integers? "); scanf("%d",&i);

// Now allocate memory space i_array = (int*)malloc(i*sizeof(int)); // allocate storage for the array

// code that uses the new array for (j=0;j<i;j++) { i_array[j]=j; // the pointer can be used as an array

printf("%d\n",i_array[j]); }

free((void*) i_array); // free up memory for reuse.system("pause");return 0;

}

One-dimensional arrays

Page 59: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Multi-dimensional arrays#include <stdio.h>#include <stdlib.h>

int main(void) {

int i,nrows,ncols;

// Define the size of the array at run time printf("Enter number of rows and number of columns ");

scanf("%d %d",&nrows,&ncols);

int** array; array=(int**)malloc(nrows*sizeof(int*));

for (i=0; i<nrows; i++) array[i]=(int*)malloc(ncols*sizeof(int));

func(array, nrows, ncols); // some function that uses the array ....

for (i=0; i<nrows; i++) free((void*)array[i]);

free((void*)array); system("pause"); return 0;

}

Page 60: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Outline

Objective1

pp2

One-Dimensional Array One-Dimensional Array3

Two-Dimensional Array Two-Dimensional Array4

Assignment #5 5

Why an Array is Essential?

Page 61: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

1. ก�าห์นดให์! Matrix A ม�ขีนาด 3x3 ด�งน�0

จังเขี�ยนโปรแกรมห์า Diagonal matrix ขีอง A โดยให์!น�าผู้ลำที่��ได!ใสั.ลำงไปใน Array A

Page 62: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

2. จัากโจัที่ย�ขี!อที่�� 1) จังห์า Transpose ขีอง Matrix A โดยให์!น�าผู้ลำมาใสั.ใน Matrix A

Page 63: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

3. จัาก Matrix A ในขี!อที่�� 1 จังเขี�ยนโปรแกรมเพิ��อแสัดงผู้ลำค"ณขีอง A x A