Department of Computer Engineering Faculty of Engineering Prince

  • Slides: 61
Download presentation
Department of Computer Engineering Faculty of Engineering Prince of Songkla University 240 -101 Introduction

Department of Computer Engineering Faculty of Engineering Prince of Songkla University 240 -101 Introduction to Computer Programming ������� Email: introcom@. coe. psu. ac. th ���� 240 -101 Introduction to Computer Programming 1

Department of Computer Engineering Faculty of Engineering Prince of Songkla University ����� 5 ����

Department of Computer Engineering Faculty of Engineering Prince of Songkla University ����� 5 ���� (Array) 240 -101 Introduction to Computer Programming 2

���������� #include �<iostream. h> int main() } int ages[10; [ char name[50; [ double

���������� #include �<iostream. 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; return 0; ������� } Size of ages = 40 Size of name = 50 Size of scores = 160 240 -101 Introduction to Computer Programming 7

������ for �������� #include <iostream. h> int main() { int a[i], i; for (int

������ for �������� #include <iostream. h> int main() { int a[i], i; for (int i = 0; i < 5; i++) a[i] = 2*i; return 0; } 0 2 4 6 8 a[0[ a[1[ a[2[ a[3[ a[4[ 240 -101 Introduction to Computer Programming 9

���������� //ex 1. cpp ������� : #include <iostream. h> ������ int main() Enter num[0]:

���������� //ex 1. cpp ������� : #include <iostream. h> ������ int main() Enter num[0]: 2 { Enter num[1]: 5 int num[4], i; Enter num[2]: 7 for(i=0; i<4; i++) { Enter num[3]: 3 cout << "Enter num [" << i num[0] = 4 << "]: "; num[1] = 10 cin >> num[i]; num[2] = 14 } for(i=0; i<4; i++) num[3] = 6 num[i] = 2*num[i]; for(i=0; i<4; i++) cout << "num[" << i << "] = " << num[i] << endl; 10 14 24 5 7 3 6 return 0; num[0] num[1[ num[2[ num[3[ } 240 -101 Introduction to Computer Programming 13

�������� (Array Initialization( int nums[3] = {4, 1, 8; { 4 nums[0] 1 nums[1[

�������� (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[ 240 -101 Introduction to Computer Programming E ch[2[ 14

§ ������������������� float gallons[9] = { 19. 5, 16. 2, 14. 14, , 10.

§ ������������������� float gallons[9] = { 19. 5, 16. 2, 14. 14, , 10. 1 , 22. 3 , 18. 17 8. 15 , 15. 2 , 22. 50 ; { char name[8] = { 'C', 'o', 'm', 'p, ' ' u', 't', 'e', 'r; { ' 240 -101 Introduction to Computer Programming 15

�������� //max. cpp #include <iostream. h> int main() { int a[4] = {-1, 6,

�������� //max. cpp #include <iostream. h> int main() { int a[4] = {-1, 6, 9, 2}; ������� : int i, max = a[0]; Maximum value is 9 for(i=1; i<4; i++) { if(a[i] > max) max = a[i]; } cout << "Maximum value is " << max << endl ; return 0; } 16 9 2 19 6 a[0] a[1[ a[2[ a[3[ max 240 -101 Introduction to Computer Programming 18

��������� ���� //sum. cpp #include <iostream. h> ������� : #define SIZE 4 Sum of

��������� ���� //sum. cpp #include <iostream. h> ������� : #define SIZE 4 Sum of all elements = 17 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; return 0; } 17 10 0 5 1 1 4 5 7 num[0] num[1[ num[2[ num[3[ total 240 -101 Introduction to Computer Programming 19

�������� String //name. cpp #include <iostream. h> int main() { char name[5] = {'J',

�������� String //name. cpp #include <iostream. h> int main() { char name[5] = {'J', 'o', 'h', 'n', ''} ; char surname[8] = {'F', '', 'K', 'E', 'N'}; cout << name << endl; cout << surname << endl; return 0; ������� : } John F 240 -101 Introduction to Computer Programming 21

����� String § ����� String �������������������� ��� คำสง cin char str[20]; cin >> str;

����� String § ����� String �������������������� ��� คำสง cin char str[20]; cin >> str; 240 -101 Introduction to Computer Programming 22

�������� String //name 2. cpp #include <iostream. h> ������� : int main() { Enter

�������� String //name 2. cpp #include <iostream. 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; 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] 240 -101 Introduction to Computer Programming 23

�������� String § ���������� char ควร�� '�' ����� char name[4] = {'S', 'u', 'n',

�������� String § ���������� char ควร�� '' ����� char name[4] = {'S', 'u', 'n', '; {' § ������� ". . . " 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[ 240 -101 Introduction to Computer Programming o str[2[ d str[3[ 0 str[4[ 24

§ ���������� C++ ���������� String ������ strcpy ���������� include ���� string. h ���� §

§ ���������� C++ ���������� String ������ strcpy ���������� include ���� string. h ���� § ������ char n[6]; J i m strcpy(n, "Jim"); n[0[ n[1[ n[2[ 240 -101 Introduction to Computer Programming 0 n[3[ n[4[ n[5[ 26

������� String § ���������� ��� String �������� strlen ���������� include ���� string. h ����

������� String § ���������� ��� String �������� strlen ���������� include ���� string. h ���� § ����� String ������ '' char name[] = "Somchai ; " int len; len = strlen(name); //this line, len = 7 len = strlen("Cat"); //this line, len = 3 240 -101 Introduction to Computer Programming 27

�������� String //len. cpp #include <iostream. h> #include <string. h> ������� : int main()

�������� 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; } 240 -101 Introduction to Computer Programming 28

��������� �� //len 2. cpp #include <iostream. h> #include <string. h> int main() {

��������� �� //len 2. 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; } C D str[0] B A str[1[ 240 -101 Introduction to Computer Programming N O str[2[ 3 ������ �: DBO 0 str[3[ 29

������������ //ex 2. cpp #include <iostream. h> void check_val(int x); int main() { int

������������ //ex 2. 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 << ": Positiven" ; else cout << x << ": Negativen" ; } 240 -101 Introduction to Computer Programming 31

����������� #include <iostream. h> void check_val(int x); int main() { int i, a[3] =

����������� #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 << ": Positiven" ; else cout << x << ": Negativen“ } 240 -101 Introduction to Computer Programming 32

���������� #include <iostream. h> void print_arr(int a[4]); int main() { int num[4] = {5,

���������� #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 18 } 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 } 240 -101 Introduction to Computer Programming 35

#include <iostream. h> int count_factor(int x[10; ([ int main() } int num[10], num_factor; for(int

#include <iostream. h> int count_factor(int x[10; ([ int main() } int num[10], num_factor; for(int i =0; i<10; i++) { cout << "Enter integer " << i+1 << " : "; cin >> num[i]; } num_factor = count_factor(num; ( cout << "Found factor of 40: " << num_factor return 0; ; { 240 -101 Introduction to Computer Programming 37

int count_factor(int x[10([ } int count = 0; for(int i=0; i<10; i++) { if(40%x[i]

int count_factor(int x[10([ } int count = 0; for(int i=0; i<10; i++) { if(40%x[i] (0 == count; ++ { return count; { 240 -101 Introduction to Computer Programming 38

#include <iostream. h> float average(float num; ([] int main() } int i; float score[10],

#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; { 240 -101 Introduction to Computer Programming 40

float average(float num([] } float total, avg; total = 0; for(int i=0; i<10; i++)

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; { 240 -101 Introduction to Computer Programming 41

��������� �� #include <iostream. h> void edit_arr(int a[]); int main() { int i, num[4]

��������� �� #include <iostream. h> void edit_arr(int a[]); int main() { int i, num[4] = {2, 5}; 2 5 0 0 4 25 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]; } 240 -101 Introduction to Computer Programming 43

���������� � //ex 4. cpp ������� #include <iostream. h> Size of x = 40

���������� � //ex 4. cpp ������� #include <iostream. h> Size of x = 40 int main() Size of names = 12 { Size of nums = 16 int x[2][5]; char names[3][4]; float nums[2][2]; cout << "Size of x = " << sizeof(x)<< endl ; cout << "Size of names = " << sizeof(names)<< endl; cout << "Size of nums = " << sizeof(nums) << endl; return 0; } 240 -101 Introduction to Computer Programming 45

���������� �� 2 ���� //ex 5. cpp #include <iostream. h> ������� : int main()

���������� �� 2 ���� //ex 5. cpp #include <iostream. h> ������� : int main() { int i, j, a[2][3]; for(i=0; i<2; i++) { for(j=0; j<3; j++) { cout << "Enter a[" << i << "]" <<"[" << j << "] : " ; cin >> a[i][j]; } } for(i=0; i<2; i++) { for(j=0; j<3; j++) 0 cout << a[i][j]<< " " ; cout << "n" ; 0 1 } 1 9 return 0; } 240 -101 Introduction to Computer Programming Enter Enter 7 5 1 2 3 9 a[0][0]: a[0][1]: a[0][2]: a[1][0]: a[1][1]: a[1][2]: 1 2 5 7 3 2 1 5 7 9 3 2 47

�������� 2 ���� § ������� {} ��� , ���������� int val[3][4] = { {8,

�������� 2 ���� § ������� {} ��� , ���������� int val[3][4] = { {8, 16, 9, 52}, {3, 15, 27, 6 , { ; { {10 , 25 , 14} 0 1 2 3 0 8 16 9 52 1 3 15 27 6 2 14 25 2 10 240 -101 Introduction to Computer Programming 48

§ ������������������� ����� 0 1 int a[][2] = {{5, 8}, {9}, {-1}}; 0 5

§ ������������������� ����� 0 1 int a[][2] = {{5, 8}, {9}, {-1}}; 0 5 8 240 -101 Introduction to Computer Programming 1 9 0 2 1 - 0 49

//ex 6. cpp #include <iostream. h> int main() { int A[2][2], B[2][2], i, j;

//ex 6. cpp #include <iostream. h> int main() { int A[2][2], B[2][2], i, j; cout << "Enter matrix A: n" ; for(i=0; i<2; i++) { for(j=0; j<2; j++) cin >> A[i][j]; } cout << "Enter matrix B: n" ; for(i=0; i<2; i++) { for(j=0; j<2; j++) cin >> B[i][j]; } 240 -101 Introduction to Computer Programming 52

cout << "Matrix A+B: n"; for(i=0; i<2; i++) { for(j=0; j<2; j++) cout <<

cout << "Matrix A+B: n"; for(i=0; i<2; i++) { for(j=0; j<2; j++) cout << A[i][j] + B[i][j]<< " " ; cout << "n"; } return 0; } // end of main 240 -101 Introduction to Computer Programming 53

������ String § ����� String ������� 1 ���� char name[6] = "Nesta; " §

������ String § ����� String ������� 1 ���� char name[6] = "Nesta; " § ��������� String ���������� 2 ���� names[0[ N e s t a 0 0 0 char names[2][8] names[1[ M a ={l "Nesta", "Maldini; { d i n i 0 " 240 -101 Introduction to Computer Programming 54

������ 5 ���������� 4 �������������� 20 ���� Enter text 1: Maths ���������� Enter text

������ 5 ���������� 4 �������������� 20 ���� Enter text 1: Maths ���������� Enter text 2: Physics ��������� Enter text 3: Chemistry Enter text 4: Biology )���������� Maths: 5 strlen ) ��������� Physics: 7 Chemistry: 9 Biology: 7 240 -101 Introduction to Computer Programming 55

//ex 7. cpp #include <iostream. h> #include <string. h> int main() { char texts[4][20];

//ex 7. cpp #include <iostream. h> #include <string. h> int main() { char texts[4][20]; int i; for(i=0; i<4; i++) { cout << "Enter text " << i+1 << " : " ; cin >> texts[i]; } for(i=0; i<4; i++) cout << texts[i] << ": " << strlen(texts[i]) << endl ; return 0; } 240 -101 Introduction to Computer Programming 56

//display 1. cpp #include <iostream. h> const int ROW = 2; const int COL

//display 1. cpp #include <iostream. h> const int ROW = 2; const int COL = 3; void display(int a[ROW][COL]); int main() { int val[ROW][COL] = { {1, 5, 8}, {3, 9, 6} }; display(val); return 0; ������� : } 8 5 1 void display(int a[ROW][COL]) 6 9 3 { for(int i=0; i < ROW; i++) { for(int j=0; j < COL; j++) cout << a[i][j] << " "; cout << "n"; } } 58 240 -101 Introduction to Computer Programming

�������� 2 ���� § �������� 3 ���� int val[3][4][2]; int i, j, k; for(i=0;

�������� 2 ���� § �������� 3 ���� int val[3][4][2]; int i, j, k; for(i=0; i<3; i++) for(j=0; j<4; j++) for(k=0; k<2; k++) val[i][j][k] = 0; 2 3 § �� ������ val[0][0][0] § �� ������� val[2][3][1] 4 240 -101 Introduction to Computer Programming 59