Title: COMP102
1COMP102 Programming Fundamentals I
- LA2B (Mon 5-7pm)
- LA2E (Fri 3-5pm)
- LA2F (Fri 5-7pm)
- TA Jackie Lo
2Lecture Review
- 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 i0 iltarray_size i)
- array2i array1i
3Lecture Review
- 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)
4Lecture Review
- Declare functions with array as parameter
- 1-D
- int func (int array, int row)
- int func (int arrayltconstant row sizegt)
- 2-D
- int func (int arrayltconst col sizegt, int
row) - int func (int arrayltconst row sizegtltconst col
sizegt) - E.g.
- int func1(int a) // invalid!
- int func2(int a3) // valid!
5Lecture Review
void fun1(int a) void main() int
lab_score1035 fun1(lab_score01)
void fun1(int a) void main() int
midterm35 fun1(midterm)
void fun1(int lab_score10) void main()
int lab_score3510 fun1(lab_score)
6Lecture Review
- More on Char array
- String
- A sequence of characters
- Stored in an array of type char
- Ending
- With the null character
- '\0'
7Lecture Review
- A character string is a sequence of characters
enclosed in double quotes () - E.g.
- char s12 a
- s10 a
- s11 \0
- char b abcdef
- char b10 abcdef
- char str
- It is called an empty string
- Uses one byte to store the null character (\0)
- char str101234567890 // invalid
8Lecture Review
- cin stream operation
- char s10
- cin gtgt 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
9Lecture Review
- What is the output of str in each case if the
input is COMP102 is i-chi-ban?
char str30 cin.getline(str, 10)
COMP102 i
char str30 cin.getline(str, 10, 2)
COMP10
char str30 cin.getline(str, 16, -)
COMP102 is i
char str30 cin.getline(str, 25, i)
COMP102
char str30 cin.getline(str, 25)
COMP102 is i-chi-ban
10Lecture Review
- String Copy
- NEVER use , except assign real val when
declare - char str16 Hello World
- char str116 str
- Copy source string into destination string
- void strcpy(char dest, const char src)
- E.g.
- strcpy(str1, str)
11Lecture Review
- String Length
- int strlen(const char)
- Returns length of string, NOT include \0
- E.g.
- int str_length strlen(abcde)
- char str10 abcde
- int str_length strlen(str)
12Lecture Review
- String Compare
- NEVER use (, lt, lt, gt, gt)
- Compares strings string1 and string2
- int strcmp(char string1, char string2)
- E.g.
- int result strcmp(s1, s2)
- if (s1 lt s2) gt result -1 (true)
- if (s1 s2) gt result 0 (false)
- if (s1 gt s2) gt result 1 (true)
13Common Error
- The operator doesn't test two strings for
equality - E.g.
- if (string1 string2) // Invalid!
- Use strcmp function instead
- if (!strcmp(string1, string2))
14SUMMARY
- By the end of this lab, you should be able to
- Pass array to function
- Declare and manipulate char array as string
15Lab11
- Download the template from website