Download presentation
Presentation is loading. Please wait.
1
CS31 Discussion 1H Fall18: week 6
TA: Behnam Shahbazi Credit to former TAs: Chelsea Ju and Bo-Jhang Ho, Ricky Lee, Sepideh Mazrouee
2
Overview Review: Multi-dimensional Arrays C-Strings
Pseudocode & Test Cases Project 5 Multi-dimensional Arrays C-Strings Midterm 2 - Practice Problems
3
Pass a 2D array to a function
Can we pass a 2-dimensional array to a function? Yes, but with restriction Have to provide the size of the array int fillInArray(int params[3][5]) { for (int i = 0; i < 3; i++) for (int j = 0; j < 5; j++) params[i][j] = i * 10 + j; } int main() { int arr[3][5]; fillInArray(arr); return 0; } Cannot omit the two numbers like one-dimensional array Have to match the caller
4
Pass an array to a function – Option 2
Can we pass a 2-dimensional array to a function? Yes, but with restriction int fillInArray(int params[][5], int row_num) { for (int i = 0; i < row_num; i++) for (int j = 0; j < 5; j++) params[i][j] = i * 10 + j; } int main() { int arr[3][5]; fillInArray(arr); return 0; } You can leave off the first bound, but you must supply the second bound as a compile-time constant expression
5
Pass a 2D array to a function
main() { const int NUM_SECTIONS = 4; const int MAX_STUDENTS = 5; int scores[NUM_SECTIONS][MAX_STUDENTS]; scores[0][0] = 97; scores[1][3] = 83; scores[3][4] = 69; } 1 2 3 97 83 69
6
2D Arrays void main(void) { int table[5][5]; int r,c; for (r=0;r<5;r++) for (c=0;c<5;c++) table[r][c] = (r+1) * (c+1); cout << table[4][3] << endl; cout << table[2][1] << endl; } When you use two nested loops as above, the outer loop iterates over the rows, and the inner loop iterates over the columns. 1 2 3 4 1 2 2 6 3 12 20 4 20
7
Why learn C Strings? If you ever need to program in C
Ex: embedded firmware Many legacy libraries are written in C, so there is no #include <string> library. Better performance.
8
What makes C strings different than C++ strings?
C strings can be viewed as an array of chars. C strings have a null-terminating character at the end.
9
Null-terminated character
\0 Allows us to know when the end of the string is.
10
Declaring a C string Template: char <name>[size];
char myString[10] = {‘h’,’e’,’l’,’l’,’o’,’\0’}; char myString[10] = “hello”; //does same as above char myString[10]; // same as char myString[10] = { '\0' } char myString[] = “hhheeellllooooooo”;
11
Example use of utilizing the null character
While I is not \0, enter loop
12
Will this compile? No, no room for the null character that compiler will automatically put in
13
Will it compile? What if we try to put a null character at the end?
No, with double quotes, compiler will put in a null character, making it too big.
14
Undefined, need the null character if you wont put the number in the braces
15
Now the compiler knows when the end is
16
Will this compile? If so what is the output?
-first print will read until null terminating char -second print will access 3rd char directly
17
Will it compile? -cant reassign C strings like this after initializing it as empty.
18
One element at a time
19
Does this program not compile or have undefined behavior
Does this program not compile or have undefined behavior? If not, what does this program output? i = 0, j = 0 Do{ print s2[0], increment 1 } while j < 5 && s2[j] isnt null && s2[j] == s1[j]
20
C String Library #include <cstring>
strlen “string length” returns the number of characters before the null character. strcpy “string copy” copies the second argument into the first argument strcat “string concatenate” adds the second argument to the first argument strcmp “string compare” compares two strings.
21
strlen -syntax is a little different instead of the .length() in c++
22
Write the strlen() function
Const means you wont modify it in this function
23
Would this program compile?
They don’t work on c++ strings
24
strcpy -copies all characters including null character
-when source array is larger than destination, undefined behavior. It tries copying past the allocated memory of the destination.
25
strcat -appends source to destination
-destination’s null character is replaced by the first character of source -The behavior is undefined if the destination array is not large enough for the contents of both src and dest and the terminating null character. Output: C strings are C strings are
26
strcmp -the # is determined by the difference of the values of the stopping point <0 the first character that does not match has a lower value in ptr1 than in ptr2 0 the contents of both strings are equal >0 the first character that does not match has a greater value in ptr1 than in ptr2
27
strcmp Prints 0
28
Will it compile? If so, what is the output?
3
29
Will it compile? If so, what is the output?
No, cant do line 7
30
Will it compile? If so, what is the output?
lol LOL
31
Will it compile? If so, what is the output?
10
32
What is the output? 10 01234! 6 012! 4 01! 3 0! 2
33
Summary Are stored in standard char arrays.
C strings: Are stored in standard char arrays. An array of size n may hold n-1 characters. Must contain an ASCII 0 character at the end of the string to be valid. You may have an empty string too. It has an ASCII 0 in the first slot. Void main(void) { char name[5]; // holds up to a 4 char name[0] = ‘H’; name[1] = ‘i’; name[2] = 0; // or ‘\0’ }
34
Summary (Cont’d) C strings:
5. Always make sure when you copy one C string into another that there’s enough room! 6. It’s a good idea to initialize each string immediately before using it! 7. Remember, an uninitialized array contains a random, possibly invalid string! void main(void) { char name[5]; strcpy ( name , ”Adele” ); name[0] = ‘\0’; strcat(name,”Hi”); }
35
In class exercise Please write two versions of a function that replaces the existing all chars in a C string with the char ‘X'. 1. Uses the strlen function 2. Can’t use strlen void fillX(char s[]) { for (int i = 0; i < strlen(s); i++) s[i] = 'X'; //2nd version: replace for loop with // for (int i = 0; s[i] != '\0'; i++) }
36
- s and t represent strings.
Don't use C++ functions on C strings! - s and t represent strings.
37
Array of char arrays Row 1 (a[0]) Row 2 (a[1]) Row m (a[m]) A matrix
That means, an array of char arrays! Row 1 (a[0]) Row 2 (a[1]) Row m (a[m]) A matrix
38
Array of char arrays char a[10][20];
Expression with only one subscript, like a[k], can be treated like a C string. (assuming that row has a zero byte marking the end) -- strlen(a[k]) is fine, for example. Strlen(a[k]) strcpy(a[0],"hello”) strcmp(a[0],a[1]) …
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.