(Array)
1 + &)% *( , -&)% * )% . (*
(String) %&'% ()% * /0& 1 ( 2 34. 4 % 2
)5 6 )5 7 7,34. 8 (- 9( :5 . * + ; 7,1( - -4 ( , < : (6- 7,= +7 = - ; 7%*( ,; & (% ) =8 -4 ( , 4 = - 4 & 7, %, % (5. & (6 7, %, 4 % 4,4 ( (Element) + & %, 4,4 ( +*1 % (6- %= *1 , - 1(4 (Index) E( & &
1 *1 , - 1(4*( & & :4 & 1 )% 7 : 1( - 7, 15. [ ( - & ]; ( - & & E()% )&34. 3% ( (9 E( 5 ( :=( 6 %6- % &: int ages[10]; char name[50];
( - & ( & (*( (% 6 ) + ( - & *( (% 6 6- J- *1 sizeof 5 == *1 )8 ( O ;6- 1%( 4 - &(49 int numbers[10];
( - & numbers = 10 * 4 = 40 6
int
char name[50]; ( - name = 50 * 1 = 50 6
=& 4 (',, :Y &J % 6 (49 #include <iostream.h> #include <conio.h> int main() { int ages[10]; char name[50]; double scores[20]; cout << "Size of ages = " << sizeof(ages) << endl; cout << "Size of name = " << sizeof(name) << endl; cout << "Size of scores = " << sizeof(scores) << endl; getch(); return 0; } Size of ages = 40 Size of name = 50 Size of scores = 160
+ &)% *( 7 & &+ & 4,4 ( *( : 15. [ , - 1(4] /* , - 1(4 . 34. 0*/ , - 1(4= E()% )&34. 5 ( :=( ;6- % & 1%( int scores[5]; scores[0] = 82; ; scores[1] = 49;
82 49 ??? ??? ??? scores[0] scores[1] scores[2] scores[3] scores[4]
*1 )8 .& for *( + &)% *( #include <iostream.h> #include <conio.h> int main() { int a[i],i; for (int i = 0; i < 5; i++) a[i] = 2 * i; getch(); return 0; }
0
2
4
6
8
a[0]
a[1]
A[2]
A[3]
A[4]
) ' -:, -34.: 6- % ) 9& *1 ( 34. 6 1%( int a[4]; a[4] = 2; a[5] = 10; for(int i=0;i<=4;i++) a[i] = i;
5. (J = = ) % Segmentation Fault 8 (-)% * J- & int x[10]; x = 11;
)% * )% = '7 *1 :5. ; *( +38 6- J- *1 cin 1%( -4 1( - 5.( % & 4 \ % = (8 )% 34. ( 9( ; 6 34. 4,4 ( * % & 1%( float num[2]; cin >> num[0]; cin >> num[1];
5 == *1 )8 .& for *( O434. & , < )% int a[10]; for(int i=0;i<10;i++) cin >> a[i];
-&)% & -&)% & ; +38 6- J- *1 cout 1%( -4 1( - 5.( % & \ 4,4 ( 34. & - 1%( char ch[3]; ch[0] = 'A'; ch[1] = 'B'; ch[2] = 'C'; cout << ch[0] << ch[1] << ch[2];
5 == *1 )8 .& for *( O434. & -& , 4,4 ( for(int i=0;i<3;i++) cout << ch[i] << endl ;
=& 4 (',, :Y % (34 . ,5 &J % 6 (49 //ex1.cpp
',, :Y :
#include <iostream.h> #include <conio.h> int main() { int num[4],i;
)%
for(i=0;i<4;i++) { cout << "Enter num [" << i << "]: "; cin >> num[i]; }
-&)%
for(i=0;i<4;i++) num[i] = 2 * num[i];
Enter num[0]: Enter num[1]: Enter num[2]: Enter num[3]: num[0] = 4 num[1] = 10 num[2] = 14 num[3] = 6
2 5 7 3
for(i=0;i<4;i++) cout << "num[" << i << "] = " << num[i] << endl; getch(); return 0; }
24
105
147
63
num[0]
num[1]
num[2]
num[3]
* )% . ( (Array Initialization) int nums[3] = {4,1,8};
4 nums[0]
1 nums[1]
8 nums[2]
char ch[3] = {'C', 'O', 'E'}; C ch[0]
O ch[1]
E ch[2]
*( O434. 4 ( -* ]% * )% . ( ; == 4 9( 3 -* %6- float gallons[9] =
{ 19.5, 16.2, 14.14, 18.17, 22.3, 10.1, 22.50, 15.2, 8.15 };
char name[8] = { 'C', 'o', 'm', 'p', 'u', 't', 'e', 'r' };
8 (-)% . * 6 %) 4,4 ( 34. ,5 = 4)% E( 0 int a[5] = {2,9,5}; 2 a[0]
9 a[1]
5 a[2]
0 a[3]
0 a[4]
8 1( - char == *1 ], O '\0' 3( 0 ;6- char ch[4] = {'C', 'A', 'T'}; C
A
T
\0
ch[0]
ch[1]
ch[2]
ch[3]
*( 8 (-)% . (6 %6- 4 8 (- ( - & ) 6: , = 8 (- ( - & * 3% =8 ( ( 4,4 ( 34.6- 4 8 (-)% . ( 1%( int x[] = {15,8,7,10};
( - & x = 6- 3% 4 15
8
7
10
x[0]
x[1]
x[2]
x[3]
%+ 6 % 4 8 (-)% . ( = & 4 8 ( ( - int x[];
J 8 )% 7& \-*( //max.cpp #include <iostream.h> #include <conio.h> int main() { : int a[4] = {-1,6,9,2}; Maximum value is 9 int i,max = a[0]; for(i=1;i<4;i++) { if(a[i] > max) max = a[i]; } cout << "Maximum value is " << max << endl ; getch(); -1 6 9 2 -1 9 6 return 0; a[0] a[1] a[2] a[3] max }
J 8 )% ', &3\ 4,4 ( *( //sum.cpp : #include <iostream.h> Sum of all elements = 17 #include <conio.h> #define SIZE 4 // 8 (-)% )&34. int main() { int num[SIZE] = {1,4,5,7}; int i,total = 0; for(i=0;i<SIZE;i++) total = total + num[i]; cout << "Sum of all elements = " << total; getch(); return 0; }
10 17 0 5 1 total
1 num[0]
4 num[1]
5 num[2]
7 num[3]
(String) String )5 7 7,34.*1 ; ) ^ .& - , String E( & char 34. 4 '\0' ( 5 34. 4 % null character) E( %& + &=\- 9( \- 1%( char name[4] = {'B', 'A', 'T', '\0'};
, -&)% & String 1%( cout << "My name is " << name;
% & -&)% & String //name.cpp #include <iostream.h> #include <conio.h> int main() { char name[5] = {'J','o','h','n','\0'} ; char surname[8] = {'F','\0','K','E','N'}; cout << name << endl; cout << surname << endl; getch(); return 0; : } John F
)% String )% String ;)5 ) = '7 *1 ^ .& ) 34 . ( 9( 4 6- , *1 -. / 0 cin char str[20]; cin >> str;
% & )% String
//name2.cpp #include <iostream.h> #include <conio.h> int main() : { Enter name: Somchai char n[10],s[10]; Enter surname: Jaidee cout << "Enter name: "; Hello Somchai Jaidee cin >> n; cout << "Enter surname: "; cin >> s; cout << "Hello " << n << " " << s << endl; getch(); return 0; }
S
o
m
c
h
a
i
\0
n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
J
a
i
d
e
e
\0
s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9]
* )% . ( String * )% . ( 5 ( & char - 4 '\0' `3 char name[4] = {'S','u','n','\0'};
*1 ) 5. & "..." char name[4] = "Sun"; S u n name[0] name[1] name[2]
\0 name[3]
char str[] = "Good"; G str[0]
o str[1]
o str[2]
d str[3]
\0 str[4]
8 (-)% * String 8 (-)% * String J- *1 ) 5. & = 38 6- a: ( 8 (-)% . ( 3% ( (9 char name[8] = "Somchai";
%= 8 (-)% J- *1 ) 5. & = *(b , &6 %6- char name[8]; name = "Somchai";
/0& 1 ( c (*(b C++ 34.1% *( 8 (-)% String )5 /0& 1 ( strcpy ^ .&*( *1 /0& 1 ((4=9 & include 6/, string.h - % & 1%( char n[6]; strcpy(n,"Jim"); J n[0]
i n[1]
m n[2]
\0 n[3]
n[4]
n[5]
) & String /0& 1 ( c (34.*1 *( ) String )5 /0& 1 ( strlen ^ .&*( *1 /0& 1 ((49= & include 6/, string.h - ) & String 34.6- (49= 6 % '\0' char name[] = "Somchai"; int len; len = strlen(name); //this line, len = 7 len = strlen("Cat"); //this line, len = 3
% & *1 /0& 1 ( 4. String //len.cpp #include <iostream.h> #include <string.h> : int main() { Length of David is 5 char name[20]; int len; strcpy(name,"David"); len = strlen(name); cout << "Length of " << name << " is " << len; cout << endl; return 0; }
=& 4 (',, :Y &J % 6 (49 //len2.cpp #include <iostream.h> #include <string.h> int main() { char str[] = "CAN"; int i,len = strlen(str); for(i=0;i<len;i++) str[i] = str[i] + 1; cout << str << endl; return 0; } D C str[0]
B A str[1]
O N str[2]
3 : DBO
\0 str[3]
%&'% ()% * /0& 1 ( /0& 1 ( my_func 4 ( &/0& 1 (- &(49 void my_func(int x);
, *( main 6- 4 15. % num int num[10];
%& 4,4 ( 34. 0 & num 6 E( ( &/0& 1 ( my_func + 4 (6- - &(49 my_func(num[0]);
% & %&)% %, 4,4 ( *( * /0& 1 ( //ex2.cpp #include <iostream.h> void check_val(int x); int main() { int a[3] = {2,-1,5}; check_val(a[0]); return 0; : } 2: Positive void check_val(int x) { if(x >= 0) cout << x << ": Positive\n" ; else cout << x << ": Negative\n" ; }
1 2 3 4 5 , 6 27 #include <iostream.h> void check_val(int x); int main() { int i,a[3] = {2,-1,5}; for(i=0;i<3;i++) check_val(a[i]); : return 0; 2: Positive } -1: Negative void check_val(int x) 5: Positive { if(x >= 0) cout << x << ": Positive\n" ; else cout << x << ": Negative\ni }
/0& 1 (34. 4 )% E( /0& 1 ( +34.= )% E( 6- ^ .& 7 & 4 ( ( &/0& 1 ( E(- &(49 1( - 7, 15. /0& 1 ((1( - 7, 15. [ ( - ]); *( O4/0& 1 ( 4 )% E( 1 == 6 % & 8 (- ( - & ;6- % & 1%( int sum_arr(int num[10]); void print_arr(int a[5]); float average(int num[]);
%& 3 \ 4,4 ( * /0& 1( %& *( O4(49 *1 )%15. 3% ( (9 1%( *( main 4 - &(49 int num[10];
, /0& 1 ( print_arr 4 ( /0& 1 (- &(49 void print_arr(int a[10]);
%& num 3\ 4,4 ( 6 * /0& 1 ( print_arr + 4 (6- - &(49 print_arr(num);
% & %& 3 \ 4,4 ( * /0& 1( #include <iostream.h> void print_arr(int a[4]); int main() { int num[4] = {5,2,-1,8}; print_arr(num); return 0; 5 2 -1 8 } num[0] num[1] num[2] num[3] a[0] a[1] a[2] a[3] void print_arr(int a[4]) {
: for(int i =0;i<4;i++) cout << a[i]; }
5 2 -1 8
% &34. 1 =& 4 (J 8 )% ) (( &( =8 ( ( 10 )( , * : : ) % a,4. &) ((3 9& J- 8 (-* % ( ) ((= '7 *1 , % (34. -& )% a,4. 7*% ( main 8 % (34.) 8 ( O)% a,4. * 7% /0& 1 (15. average J- * %& 34. ; ) ((3 &9 & 10 )(= main * /0& 1 ( average () (( + E(3 ( 6- ) -8 main
average
-, 9 20
#include <iostream.h> float average(float num[]); int main() { int i; float score[10],avg_score; for(i=0;i<10;i++) { cout << "Enter score " << i+1 << " : "; cin >> score[i]; } avg_score = average(score); cout << "Average score is " << avg_score; return 0; }
float average(float num[]) { float total,avg; total = 0; for(int i=0;i<10;i++) total = total + num[i]; avg = total/10; return avg; }
6 )% & b *(/0& 1 ( /0& 1 (34. 4 : E( 4 6 )% b *( - & ,% = %&38 * 34.+7 %& E( ( &/0& 1 ( 4 ,4. ( ,&-
=& 4 (',, :Y &J % 6 (49 #include <iostream.h> void edit_arr(int a[]); int main() { int i,num[4] = {2,5}; 4 2 25 5 0 0 edit_arr(num); num[0] num[1] num[2] num[3] for(i=0;i<4;i++) a[0] a[1] a[2] a[3] cout << num[i]; : return 0; } 4 25 0 0 void edit_arr(int a[]) { int i; for(i=0;i<4;i++) a[i] = a[i]*a[i]; }
QUIZ 1.=& 4 (/0& 1 (15. sum_one_to_n ^ .&/0& 1 ((49= 4 : E(=8 ( ( ; 1 , = %&)% , E()% ', 9& % 1 =(+ & =8 ( ( ; - & ,% 2.=& 4 (J *( )% =8 ( ( ; = '7 *1 1 = ( 9( J = -&)% &', 9& % 1 =(+ &=8 ( ( ; ( 9( 8 (-* % (34.)8 ( O)% ', 7%*(/0& 1 ( sum_one_to_n 8 % (34. )% = '7 *1 , % (34. -&)% ', * 7%*(/0& 1 ( Main(6 % & = )% = '7 *1 % % 1 5 6 %)
: . =& 4 (J :5 . )% 7& \- & Array 2 ( - 7 x 8 J- 4 *1 & ( Function Max int maxarray(int arr[ ]) { Int i,j,max; max = arr[0,0]; for (i=0;i<7;i++) for(j=0;j<8;j++) if (max<arr[i,j]) max = arr[i,j]; return max; }