BC 322 Array 2 BC 322 Computer ProgrammingArray
BC 322 ครงท Array 2 มต BC 322 Computer Programming-Array Processing 13 1
เรองทเรยนวนน n n อารเรย 2 มต BC 322 Computer Programming-Array Processing + ฟงกชน 2
จงเกบคาลงในอารเรย ตอไปน [0] Data[0][0]=5 Data[0][1]=0 Data[1][3]=8 Data[2][2]=Data[1][3]/2 Data[3][2]=Data[0][0]+Data[1][3] Data[4][3]=10 Data[4][0]=6 Data[4][1]=Data[4][3]- Data[4][0] BC 322 Computer Programming-Array Processing Data [1] [2] [3] [0] [1] [2] [3] [4] 5
Ex 2 จงแสดงคาคาตวเลขทงหมดทเกบในอารเร ยใน Ex 1 เพอใหไดผลลพธดงน ตวอยางผลรน #include<stdio. h> Section 1 : 20 18 15 30 17 void main() Section 2 : 19 18 21 22 24 { int age[3][5]; Section 3 : 18 18 21 20 19 for (………………………. ) BC 322 Computer Programming-Array Processing 7
Solution Ex 3 BC 322 Computer Programming-Array Processing 9
BC 322 Computer Programming-Array Processing 10
Ex 4 จงเขยนโปรแกรมเพอรบคายอดขายของรานคา รานเกบในอารเรย price และคำนวณหายอดขายรวมของรานคาแตละราน Price Total 10 25 15 50 10 30 25 65 ตวอยางการรบคาและแส ดงผล Shop 1 enter price 1 : 10 enter price 2 : 25 enter price 3 : 15 ======= Shop 2 enter price 1 : 10 enter price 2 : 30 enter price 3 : 25 BC 322 Computer Programming-Array Processing 2 ผลลพธ Shop 1 has 50 baht. Shop 2 has 65 baht. 11
Solution Ex 4 BC 322 Computer Programming-Array Processing 12
Ex 5 : จงหาผลลพธของโปรแกรม #include<stdio. h> ผลลพธ #include<string. h> Enter a string 1 : Computer void main() Enter a string 1 : COMPUTER { s 1 Not Equal s 2 char s 1[20], s 2[20]; printf(“Enter a string 1 : ”); gets(s 1); printf(“Enter a string 2 : ”); gets(s 2); if(strcmp(s 1, s 2) == 0) printf(“s 1 Equal s 2n”); else printf(“s 1 Not equal s 2n”); } BC 322 Computer Programming-Array Processing 14
Ex 6: จงหาผลลพธของโปรแกรมตอไปน #include<stdio. h> #include<string. h> void main() { char s 1[20], s 2[10]; strcpy(s 1, ”Hi!!”); strcpy(s 2, ” Student”); strcat(s 1, s 2) printf(“%sn”, s 1); } BC 322 Computer Programming-Array Processing ผลลพธ Hi!! Student 16
การใชอารเรย 2 มต ฟงกชน BC 322 Computer Programming-Array Processing + 17
Ex 7 จงเขยนโปรแกรมเพอรบอายเพอน 2 กลม ๆ3 คนและแสดงผลโดยกำหนดใหมโปรแกรมย อย ดงน n n โปรแกรมยอยชอ Inputdata ใชสำหรบรบคาเกบในอารเรย โปรแกรมยอยชอ Displaydata ใชสำหรบแสดงผลทเกบอยในอารเรย ตวอยางการรบคาและแสดงผล Group 1 Please enter age 1: 17 Please enter age 2: 15 Please enter age 3: 24 Group 2 Please enter age 1: 19 Please enter age 2: 17 BC 322 Please Computer Programming-Array enter age 3: 17 Processing Group 1 : Friend 1 = 17 years Group 1: Friend 2 = 15 years Group 1: Friend 3 = 24 years ========= Group 1: Friend 1 = 19 years Group 1: Friend 1 = 17 years 18
#include<stdio. h< void Inputdata(int a[2][3; ([ void Displaydata(int a[2][3; ([ void main() }int age[2][5; [ Inputdata(age; ( printf("-----------n; (" Displaydata(age; ( { /***************/ void Inputdata(int a[2][3([ }int i, j; BC 322 Computer Programming-Array Processing Solution Ex 7 19
void Displaydata)int a[2][5([ }int i, j; BC 322 Computer Programming-Array Processing Solution Ex 7 20
Student in Section 1 Enter student weight 1 : 45. 5 Enter student weight 2 : 50 Enter student weight 3 : 60 Enter student weight 4 : 70. 5 Enter student weitht 5 : 45. 8 ======= Student in Section 2 Enter student weight 1 : 90. 5 Enter student weight 2 : 50 Enter student weight 3 : 60 Enter student weight 4 : 70. 5 Enter student weitht 5 : 42. 3 ======= Student in Section 3 Enter student weight 1 : 45. 5 Enter student weight 2 : 50 Enter student weight 3 : 43. 2 Enter student weight 4 : 70. 5 Enter student weitht 5 : 84. 5 BC 322 Computer Programming-Array Processing ==== ตวอยางการรบคาและแสดงผล Ex 8 Maximum weight of section 1 is 70. 50 kg. Maximum weight of section 2 is 90. 50 kg. Maximum weight of section 3 is 84. 50 kg. 22
+++Department 1+++ Customer 1 Enter amount : 10 Enter price : 2 Customer 2 Enter amount : 15 Enter price : 1 Customer 3 Enter amount : 20 Enter price : 3 ========= +++Department 2+++ Customer 1 Enter amount : 5 Enter price : 2 Customer 2 Enter amount : 15 Enter price : 10 Customer 3 Enter amount : 30 Enter price : 3 ========= BC 322 Computer Programming-Array Processing ตวอยางผลรน average of Department 1 is 31. 67 average of Department 2 is 83. 33 Minimum total is department 1 24
- Slides: 24