Weitere ähnliche Inhalte Ähnlich wie ตัวแปรชุดและตัวแปรกลุ่มอักขระ (20) Mehr von Jiraporn Chaijaroen (12) Kürzlich hochgeladen (20) ตัวแปรชุดและตัวแปรกลุ่มอักขระ3. 1. 1 (One
Dimensional Array)
[]
subscript
type array-name[n];
type
array-name
n
4. char number[5];
int time[3];
float interest[2];
number, time interest
subscript
scanf(“%d”,&time[1]);
printf(“%fn”,interest[0]);
2(
time[1]) time
1( interest[0])
interest
7. int time[3];
3x2 = 6
int
3 time[0], time[1] time[2]
time
2000
time[0] 2000+(0x2) = 2000
time[1] 2000+(1x2) = 2002
time[2] 2000+(2x2) = 2004
8. float interest[2];
2x4 = 8
float
2 interest[0] interest[1]
interest
3000
interest[0] 3000+(0x4) =
3000
interest[1]
3000+(1x4) = 3004
9. 2. 2 (Two
Dimensional Array)
[]2
Matrix
2
11. int score[3][2];
3x2 = 6
score
score[0][0] score[0][1]
score[1][0] score[1][1]
score[2][0] score[2][1]
char person[10][5][80];
int incomes[3][5][2];
float marks[2][4][20];
12. 3.
(Array Initialization)
declare
1
char msg1[ ] = “Give value for x”;
int x1[ ] = {10,2,8};
float y1[ ] = {12.8,11.3,9.2,6.35};
13. 2
char msg2[ ][40] = {{“SOONTORN”},
“BANGKOK”},
{“AMORN”},
{“CHONBURI”},
{“PRAPAI”},
{“KHONKAEN”}};
int x2[ ][3] = {{10,2,8},{5,15,7}};
float y2[ ][4] = {12.8,11.3,9.2,6.35,
2.28,31.3,19.2,86.5,
12.0,17.43,7.12,62.3};
14. 2
1
1 5
#include<stdio.h>
void main( ) {
int room_no[ ] = {22, 18, 20, 24, 21};
int total = 0, n = 0;
do {
total = total + room_no[n];
printf(“Room %d has %3d pupilsn”,n+1,room_no[n]);
n++;
} while(n<5);
printf(“ = = = n”);
printf(“Total no is %3d pupilsn”,total);
16. 1)
100
100
int k1, k2, k3, …, k100; /*
k1, k2, k3, …, k100 100 */
100
subscript
int k[100]; /* 1 k
*/
2) (table)
2
3)
17. 1 (arrays variables)
(subscript)
18. 1.1
1) 1 (one dimension
arrays single dimension arrays)
1
(subscript) 1 a[20], b[100], na
me[30] salary[20]
19. 2) (multi-
dimension arrays)
(subscript) 2
2 3
-
2 (two dimension arrays)
(subscript) 2
a[2][4], b[3][4], name[5][30]
-
3 (three dimension arrays)
(subscript) 3
a[2][3][4], b[3][4][5]
20. 2 (declaration of
arrays)
1)
1 (declaration of one dimension
arrays)
type
int, float, char, double
arrayname
size
21. 1
5.1 int s[20];
2 bytes 20
40 bytes
24. 2) 2
(declaration of two dimension arrays)
type
int, float, char, double
arrayname
n row)
0, 1, 2, …, n-1
m column)
25. 2
int r[3][2];
2
(table) n = 0, 1, 2
m = 0, 1
2 bytes 6
26. 1 2
1
=
(size)
2
= n*m
int r[3] [2]; n =
3, m = 2
= 3*2 = 6
r[0][0], r[0][1], r[n-1][m-1]
6
27. 3) 3
(declaration of three dimension arrays)
type arrayname [n] [m] [p];
type
int, float, char, double
arrayname
n 1 0, 1, 2, …., n-1
m 2 0, 1, 2, …., m-
1
p 3 0, 1, 2, …., p-1
28. 3
= n*m*p
3 float a[2][2][3]; n = 2 , m = 2, p = 3
= 2*2*3 = 12
29. Column 0 Column 1
1 2 3 1 2 3
Row 0 a[0][0][0] a[0][0][1] a[0][0][2] a[0][1][0] a[0][1][1] a[0][1][2]
Row 1 a[1][0][0] a[1][0][1] a[1][0][2] a[1][1][0] a[1][1][1] a[1][1][2]
4 bytes 12
4*12 = 48 bytes
31. 2( 2
type arrayname[n][m] = { value list };
value list
, (comma)
{ }
5.6 int a[10] =
{10, 20, 30, 40,50, 60, 70, 80, 90, 100};
33. 2)
1( 1
char arrayname[size] = “string constant”;
2( 2
char arrayname[n][m] = {“string constant”};
string constants
, (comma)
5.8 char s[12] = “ASIAN GAME”
;
35. 2
province [0] 1
NAKHONPANOM
province [1] 2
SAKON NAKHON province [2]
3 MOOKDAHAN
province [0][0] province [0] 1
N
province [1][2] province [1] 3
K
36. for while do while
for