21
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang [email protected] www.cs.ucla.edu/~swyang

CS31: Introduction to Computer Science I

Embed Size (px)

DESCRIPTION

CS31: Introduction to Computer Science I. Discussion 1A 5/7/2010 Sungwon Yang [email protected] www.cs.ucla.edu/~swyang. Quick Review. What did we learn last week? Arrays create the same type of multiple variables at one time index numbers start from 0 - PowerPoint PPT Presentation

Citation preview

Page 1: CS31: Introduction to  Computer Science I

CS31: Introduction to Computer Science I

Discussion 1A 5/7/2010

Sungwon [email protected]

www.cs.ucla.edu/~swyang

Page 2: CS31: Introduction to  Computer Science I

Quick Review• What did we learn last week?

• Arrays• create the same type of multiple variables at one time• index numbers start from 0• each item of an array is a regular variable

• array declaration• specify the length of the array

– the number must be a constant positive integer– value itself or const type of int variable

• initialization of an array• values enclosed in curly braces• number of values can be less than the length of an array• if omit the length of an array, it is determined based on the number of values

• out-of-bound index• c++ compiler does not check the bound of index

– accessing out-of-bound items does not cause a compile-error– be careful not to access out-of-bound items in loop

• arrays in functions• can pass individual items either by value or reference• can pass the entire array

– can be regarded as passing by reference• const keyword

– protects the items of the array from accidentally being changed

Page 3: CS31: Introduction to  Computer Science I

C-strings

• C++ is enhanced from C Language– The C Language is a proper subset of C++

• C does not have string type– it has its own way of representing strings– we can still use the older way in C++– called C-strings

Page 4: CS31: Introduction to  Computer Science I

C-strings

• Can recall what string is?– a sequence of zero or more characters

• we have learned arrays last week– a sequence of variables of the type we define

• a string can be represented by an array of characters!

Page 5: CS31: Introduction to  Computer Science I

C-strings

• let’s define an array of characters– char s[10];– 10 character variables created.

• Now, which strings can be stored in the array?– “” : empty string– “abcde” : the length is 5– “abcdefghi” : the length is 9– “abcdefghij” : the length is 10

“abcdefghijklmnopqrstuvwxyz” : the length is 26(X)

(X)

Page 6: CS31: Introduction to  Computer Science I

zero byte/null character

• C-string must end with a marker– the marker indicates “this is the end of the string”– without this marker, it is a character array, not a

string– this marker is called zero byte or null character• ‘\0’ (ASCII number is 0)

– the null character is automatically filled in the last slot

Page 7: CS31: Introduction to  Computer Science I

C-strings

C String Exampleschar st1[10] = "";

char st2[10] = "a";

char st3[10] = "abc 123";

a

0

\0

1

?

2

?

3

?

4

?

5

?

6

?

7

?

8

?

9

\0

0

?

1

?

2

?

3

?

4

?

5

?

6

?

7

?

8

?

9

a

0

b

1

c

2 3

1

4

2

5

3

6

\0

7

?

8

?

9

Page 8: CS31: Introduction to  Computer Science I

C-strings

More C String Exampleschar st1[] = "abcde";

char st2[] = "abcdefghi";a

0

b

1

c

2

d

3

e

4

f

5

g

6

h

7

i

8

\0

9

a

0

b

1

c

2

d

3

e

4

\0

5

Page 9: CS31: Introduction to  Computer Science I

c-string output

int main (){ char s[] = "Hello, how are you?"; cout << s <<endl;}

Hello, how are you?

• can use cout << to print out– will print characters until it meets ‘\0’

int main (){ char s[] = "Hello, how are you?"; s[5] = '\0'; cout << s <<endl;}

Hello

Page 10: CS31: Introduction to  Computer Science I

c-string output

int main (){ char s1[10]="abcde"; char s2[10]="12345"; char c[10]={'1' ,'2','3','4','5'}; int i[10]={1 , 2 , 3 , 4 , 5}; double d[10]={1.0 , 2.0 , 3.0 , 4.0 , 5.0}; bool b[10]={true,false,true,false,true};

cout << s1 << endl; cout << s2 << endl; cout << c << endl; cout << i << endl; cout << d << endl; cout << b << endl;}

abcde12345123450028F8100028F8380028F88C

• cout works with character arrays– for other array types, it will print something different

Page 11: CS31: Introduction to  Computer Science I

C-string input• cin

– read one word

• cin.getline(C-string variable, max)– read up to (max number-1) of characters in one line– ‘\0’ is stored in the last

int main (){

char s1[20];char s2[20];

cin >> s1;cin.ignore(10000,'\n');cin.getline(s2,10);

cout << s1 << endl;cout << s2 << endl;

}

HiHi Sungwo

Hi SungwonHi Sungwon

Page 12: CS31: Introduction to  Computer Science I

initialization vs. assignment

int main (){ char s[] = "Hello"; cout << s <<endl;}

int main (){

char s[10];s = "Hello";cout << s <<endl;

}OK!

Compile Error

Page 13: CS31: Introduction to  Computer Science I

Quick question

• what will be displayed?

int main (){ char s[10] = {'a','b','c','d','e','f','g','h','i'}; cout << s << endl;}

abcdefghi

int main (){ char s[10] = {'a','b','c','d','e','f','g','h','i', 'j'}; cout << s << endl;}

abcdefghijts 잔 O 憬 7

Page 14: CS31: Introduction to  Computer Science I

More quick question

• what will be displayed?

int main (){ char s[10]; s[0] = 'a'; s[1] = 'b'; s[2] = 'c'; s[3] = 'd'; s[4] = 'e'; cout << s << endl;}

abcde05"ts?5

int main (){ char s[10]; s[0] = 'a'; s[1] = 'b'; s[2] = 'c'; s[3] = 'd'; s[4] = 'e'; s[5] = '\0'; cout << s << endl;}

abcde

Page 15: CS31: Introduction to  Computer Science I

C-string to C++ strings

• convert C-string into C++ string type– The C++ string operator = works• but, not vice versa

int main (){ char s[] = "How are you?"; string str = s; cout << str << endl;}

How are you?

int main (){ string str = "How are you?"; char s[] = str; cout << s << endl;}

Compile Error

Page 16: CS31: Introduction to  Computer Science I

Functions for C-strings

• basically, C-string is an array of characters– can manipulate characters with index number

• There are pre-defined functions for C-strings– #include <cstring> required

Page 17: CS31: Introduction to  Computer Science I

C-string libraries

• strlen(s)– returns the length of s, not counting ‘\0’

int main (){ char s[] = "How are you?"; cout << strlen(s) << endl;}

12

Page 18: CS31: Introduction to  Computer Science I

C-string libraries

• strcpy(t, s)– copies the string s to t• t = s does not work• does not check the array size

– make sure there is enough space in t

int main (){ char s[10] = "Hello"; char t[10]; strcpy(t, s); cout << t << endl;}

Hello

Page 19: CS31: Introduction to  Computer Science I

C-string libraries

• strcat(t, s)– concatenate (append) s to the end of t• does not check the array size

– make sure there is enough space in t

int main (){ char s[10] = "Hello"; char t[20] = "Sungwon,"; strcat(t, s); cout << t << endl;}

Sungwon,Hello

Page 20: CS31: Introduction to  Computer Science I

C-string libraries

• strcmp(t, s)– compare t and s

• return 0 if they are equal• return something greater than 0 if t > s• return something less than 0 if t < s

int main (){ char s[10] = "abcde"; char t[10] = "abcde"; cout << strcmp(t,s) << endl;}

0

int main (){ char s[10] = "abcde"; char t[10] = "abced"; cout << strcmp(t,s) << endl;}

1

int main (){ char s[10] = "abcde"; char t[10] = "abc"; cout << strcmp(t,s) << endl;}

-1

Page 21: CS31: Introduction to  Computer Science I

array of C-strings• A C string is essentially an array of characters.

– an array of C strings is an array of arrays of characters. An array of arrays is simply a 2D array

• char s[10][20];– s[3] : can be used to refer to the string in position 3– s[3][5] : can be used to refer to the letter in position 5 of the string in

position 3.

int main (){ char s[3][6]; strcpy(s[0], "hello"); strcpy(s[1], "apple"); strcpy(s[2], "world");

cout << s[0] << endl; cout << s[1] << endl; cout << s[2][2] << endl;}

helloappler