Download presentation
Presentation is loading. Please wait.
1
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo
2
More on Array CANNOT copy all elements of one array into another with an assignment statement array2 = array1; Must use a loop instead for (int i=0; i<array_size; i++) array2[i] = array1[i]; Lecture Review
3
Passing Arrays to Functions Passing array as parameter = pass by reference NO & sign To ensure no modification inside function i.e. all elements in the array are read-only Add a const modifier before the array parameter double func(const int array[])
4
Lecture Review Declare functions with array as parameter 1-D int func (int array[], int row); int func (int array[ ]); 2-D int func (int array[][ ], int row); int func (int array[ ][ ]); E.g. int func1(int a[][]);// invalid! int func2(int a[][3]);// valid!
5
Lecture Review More E.g. void fun1(int a) {…} void main() { int lab_score[10][35]; fun1(lab_score[0][1]); } void fun1(int a[]) {…} void main() { int midterm[35]; fun1(midterm); } void fun1(int lab_score[][10]) {…} void main() { int lab_score[35][10]; fun1(lab_score); }
6
Lecture Review More on Char array String A sequence of characters Stored in an array of type char Ending With the null character ' \0 '
7
Lecture Review A character string is a sequence of characters enclosed in double quotes ( “ … ” ) E.g. char s1[2] = “a”; s1[0] = ‘a’ s1[1] = ‘\0’ char b[] = “abcdef”; char b[10] = “abcdef”; char str[] = “”; It is called an empty string Uses one byte to store the null character (‘\0’) char str[10]=“1234567890”; // invalid ‘a’‘a’‘b’‘b’‘c’‘c’‘d’‘d’‘e’‘e’‘f’‘f’‘ \0 ’‘a’‘a’‘b’‘b’‘c’‘c’‘d’‘d’‘e’‘e’‘f’‘f’ ???
8
Lecture Review cin stream operation char s[10]; cin >> s; A string with space ( ‘ ’ ) CANNOT be read properly cin.getline Syntax cin.getline(char dst[], int size, char delimiter) When the delimiter character is encountered A string is stored in character string dst With maximum length of size, including the null character ( ‘ \0 ’ ) Note delimiter character is optional, default is ‘ \n ’
9
Lecture Review What is the output of str in each case if the input is “COMP102 is i-chi-ban” ? char str[30]; cin.getline(str, 10); char str[30]; cin.getline(str, 10, ‘2’); char str[30]; cin.getline(str, 16, ‘-’); char str[30]; cin.getline(str, 25); char str[30]; cin.getline(str, 25, ‘i’); COMP102 i COMP10 COMP102 is i COMP102 COMP102 is i-chi-ban
10
Lecture Review String Copy NEVER use ‘=’, except assign real val when declare char str[16] = “Hello World”; char str1[16] = str; Copy source string into destination string void strcpy(char dest[], const char src[]); E.g. strcpy(str1, str);
11
Lecture Review String Length int strlen(const char[]); Returns length of string, NOT include ‘ \0 ’ E.g. 1. int str_length = strlen(“abcde”); 2. char str[10] = “abcde”; int str_length = strlen(str);
12
Lecture Review String Compare NEVER use ( ==,, >= ) Compares strings string1 and string2 int strcmp(char string1[], char string2[]); E.g. int result = strcmp(s1, s2); if (s1 result = -1 (true) if (s1 == s2) => result = 0 (false) if (s1 > s2) => result = 1 (true)
13
Common Error The operator “ == ” doesn't test two strings for equality E.g. if (string1 == string2)// Invalid! Use strcmp function instead if (!strcmp(string1, string2))
14
SUMMARY By the end of this lab, you should be able to: Pass array to function Declare and manipulate char array as string
15
Lab11 Download the template from website
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.