Department of Computer Engineering Faculty of Engineering Prince

  • Slides: 31
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 ����� 6 �����

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

������ 6 -1 #include <iostream. h< int main() a 2. 5 3. 5 b

������ 6 -1 #include <iostream. h< int main() a 2. 5 3. 5 b 8. 6 5. 6 } float a = 2. 5, b = 5. 6; float *p, *q; p q p = &a; q = &b; cout << "*p = " << *p << endl; ������� cout << "*q = " << *q << endl; * p = *p + 1; *p = 2. 5 *q = 5. 6 * q = *q + 3; a = 3. 5 cout << "a = " << a << endl; b = 8. 6 cout << "b = " << b << endl; return 0; } 9 240 -101 Introduction to Computer Programming

������� Call-by-Reference § ���������� 2 ��� Call-by -Value ��� Call-by-Reference § Call-by-Value – ��������������������

������� Call-by-Reference § ���������� 2 ��� Call-by -Value ��� Call-by-Reference § Call-by-Value – �������������������� �� ������� – �������� ������ (int, float, char, . . . ) 10 § 240 -101 Call-by-Reference Introduction to Computer Programming

������ 6 -2 #include <iostream. h< int square. By. Value(int n; ( int main()

������ 6 -2 #include <iostream. h< int square. By. Value(int n; ( int main() } int number = 5; cout << "The original value of number is " << number <<endl; number = square. By. Value(number; ( cout << "The new value of number is " << number << endl; return 0; main number { 5 25 int square. By. Value(int n( } n = n*n; return n; } 240 -101 Introduction to Computer Programming n 25 5 square. By. Value 11

������ 6 -3 #include <iostream. h< void square. By. Reference(int *n. Ptr; ( int

������ 6 -3 #include <iostream. h< void square. By. Reference(int *n. Ptr; ( int main() } int number = 5; cout << "The original value of number is " << number <<endl; square. By. Reference(&number; ( cout << "The new value of number is " << number << endl; return 0; main number { 5 25 void square. By. Reference(int *n. Ptr( } * n. Ptr = *n. Ptr * *n. Ptr; { n. Ptr square. By. Reference 240 -101 Introduction to Computer Programming 12

������ � int v[5; [ int *v. Ptr; vptr = v; //or v. Ptr

������ � int v[5; [ int *v. Ptr; vptr = v; //or v. Ptr = &v[0]; v. Ptr = v. Ptr + 2; 240 -101 Introduction to Computer Programming 14

#include <iostream. h< #include <string. h< int main() } char msg[10; [ char *ptr;

#include <iostream. h< #include <string. h< int main() } char msg[10; [ char *ptr; cout << "Enter text to reverse; " : cin >> msg; int len = strlen(msg); ����� 6 -4 msg[9] msg[0] msg[1] msg[2] msg[3] ptr = &msg[len-1; [ while(ptr >= &msg[0([ } cout << *ptr; -{ return 0; { 240 -101 Introduction to Computer Programming 'C' 'o' 'e' '0' . . . ptr Enter text to reverse: Coe eo. C 15

������ 6 -5 #include <iostream. h< b[0] b[1] b[2] b[3] b[4] int main() 6

������ 6 -5 #include <iostream. h< b[0] b[1] b[2] b[3] b[4] int main() 6 2 7 5 10 4 } int b[5] = {2, 7, 5, 4, 6; { int *b. Ptr; b. Ptr = b; cout << "b. Ptr[0] = " << b. Ptr[0] << endl; cout << "b. Ptr[1] = " << b. Ptr[1] << endl; b. Ptr = &b[2; [ b. Ptr[1] = 10; cout << "b[1] = " << b[1] << endl; b. Ptr[0] = 2 cout << "b[3] = " << b[3] << endl; b. Ptr[1] = 7 b[1] = 7 return 0; b[3] = 10 } 240 -101 Introduction to Computer Programming 19

������ 6 -6 #include <iostream. h< int main() } int num_std; cout << "Enter

������ 6 -6 #include <iostream. h< int main() } int num_std; cout << "Enter number of students; " : cin >> num_std; float *scores = new float[num_std]; int i; for(i=0; i<num_std; i(++ } cout << "Enter score of student " << i+1 : " >> ; " cin >> scores[i; [ { 240 -101 Introduction to Computer Programming 24

������ 6 -6 (��� ( cout << "n. The scores are; " float total

������ 6 -6 (��� ( cout << "n. The scores are; " float total = 0; for(i=0; i<num_std; i(++ } cout << scores[i; " " >> [ total = total + scores[i]; { cout << "n. Total scores of all students is "<< total; delete []scores; return 0; { 240 -101 Introduction to Computer Programming 25

����� § ���������� ��� § ������ char *name_arr[3; [ name_arr[0] = "Somchai; " name_arr[1]

����� § ���������� ��� § ������ char *name_arr[3; [ name_arr[0] = "Somchai; " name_arr[1] = "Sombat; " name_arr[2] = "Sawat; " 240 -101 Introduction to Computer Programming 31