CS 31 Discussion, Week 6 Faisal Alquaddoomi, faisal@cs.ucla.edufaisal@cs.ucla.edu Office Hours: BH...

Preview:

Citation preview

CS 31 Discussion, Week 6

Faisal Alquaddoomi, faisal@cs.ucla.edu

Office Hours: BH 2432,W 4:30-6:30pm,F 12:30-1:30pm

Notes and Agenda

• Slides are now online:http://cs.ucla.edu/~faisal/• Arrays Recap• C-Strings• 2D Arrays• Memory and the Stack–(optional, on board)

Arrays Review

• Arrays are useful for dealing with large numbers of values

• Create an array like so, specifying the type, name and size:int myArray[200];

• You can also statically allocate arrays, with or without setting the size (it’s set automatically if you leave it out):int myOtherArray[] = { 1, 3, 5, 7, 9, 11 };

• Array slots are numbed 0 to length-1, e.g.int someArray[10]; // size 10, slots 0-9

Arrays Review #2

• Arrays can be passed to functions– They’re always passed by reference

• Can prevent modification using “const” modifier

– Syntax for taking an array as a parameter:void printAll(const int list[], int len){

for (int i = 0; i < len; i++) {cout << “#” << (i+1);cout << “: “ << list[i] << endl;

} }

• Don’t go past the end of an array or before the beginning!

C-Strings

• A “C-String” is just an array of characters! For instance:char text[10]; // 10 characters

• As we’ve mentioned, arrays can be of any type, including chars

• Convenient for representing text when the string class wasn’t around

• Can be used with cout, can be assigned to strings, etc. (as we’ll describe)

• But remember that we have trouble with array sizes– e.g. we have to pass them around all the time and be sure not

to run off the end or else…

Nul-Termination• The solution to the array size problem is nul-termination• By convention, a special element that indicates the end of the text

in a C-String is a literal 0– (not the character ‘0’, which is 48)– Referred to in the literature as a ‘nul-terminator’

• the ‘nul’ part because it’s 0, and the ‘terminator’ part because it ends the string– You may also see the 0 represented as ‘\0’, which is an escape code that

also has the literal value 0• Chosen to allow the length of a C-String to be determined by just

examining the elements– Unlike your project, we don’t have to pass around the length of C-Strings

(at least if they’re well-formed)– Also implies that a 0 cannot occur within the text itself

• Also, if the nul-terminator is missing, things will go badly in your life (more on this later)

Creating a C-String

• Just like any other array, but with some shortcuts (that we’ll see later)

• Creating a C-String with “Hi!” in it, the hard way:char str[4]; // size 4, slots 0-3str[0] = ‘H’;str[1] = ‘i’;str[2] = ‘!’;str[3] = 0; // the nul-terminator

Creating a C-String, Cont’d

• Marginally easier way:char str[4] = {‘H’, ‘i’, ‘!’, ‘\0’};

• Even easier:char str[4] = “Hi!”;

• The above automatically inserts the nul-terminator for us– which is why the char array is still of length 4– …although it could have been any size >= 4

• All of the literal strings you’ve seen so far have been C-Strings all along

C-Strings as Parameters and Arguments

• Taking a C-String as a parameter:void takesCStr(char str[]) { /* etc */ }

– You can use ‘const’ before the ‘char’ in order to prevent it from being modified

• Passing a C-String as an argument:char myStr[] = “Test”;takeCStr(myStr);

• Largely the same as arrays (since C-strings are arrays)

Operations with C-Strings• Unlike regular strings, you can’t do much with C-Strings

without help from some library functions• You can do a few operations given this array:

char str[] = “Hi!”;• Print it:

cout << str << endl;• Read it:

cin >> str; // ‘str’ must be big enough

• Get a character out of it, or change a character:char p = str[2]; // p == ‘!’str[0] = ‘P’; // str is now “Pi!”

• Aside from the printing/reading, pretty much things you’d do with normal arrays…

Things You Can’t do With C-Strings

• Compare two C-strings:char a[] = “Hello!”;char b[] ={‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’, ‘\0’};if (a == b) { /* nope */ }

• Concatenate two C-Strings:cout << a + b << endl; // nope

• Assign one C-String to another:a = b; // doesn’t work

• Check the size of a C-String:a.size(); // won’t work, sadly

• Thankfully, there are easy ways to do these things…

Library Functions for Strings

• #include <cstring> in these key functions:

Function Signature What it does

int strlen(const char str[]);

Returns the length of str, ex:strlen(“Hi”) == 2

char *strcpy(char dest[], const char src[]);

Copies src into dest, returns dest

char *strcat(char dest[], const char src[]);

Sticks src onto the end of dest, returns dest

int strcmp(char str1[],char str2[])

Compares two strings, returning 0 if they’re equal

A Brief Notation Note

• You may occasionally see a character array (or any array) taken as a parameter like so:

int strlen(const char *str);– That star means “pointer” in C/C++, but this won’t make

any sense if you’re not familiar with memory and addressing

– You can simply ignore this and pass an array when you see something that asks for a “char *” (or any other type, e.g. “int *”) for now…

• I intend to discuss pointers at the end of this lecture, time permitting and interest withstanding

2D Arrays

• The arrays we discussed previously are technically called ‘1D arrays’– Have a single ‘dimension’, e.g. myArray[10] has

one dimension from 0 to 9• A 2D array is exactly the same as a 1D array in

terms of use, except there are two dimensions• Used for making tables, grids, matrices, etc.• Project 3 used one to hold all the dots that

appear on the screen

Creating and Accessing a 2D Array

• Similar to a 1D array, creating a 2D array:int myArray[10][10];

• // ^ has 10*10 == 100 slots

• Accessing and setting an element:int p = myArray[3][7]; // row 3, col 7myArray[9][9] = 9000;

• // ^ sets the last row, col

• By convention, the first dimension is the row and the second is the column

Statically Allocating a 2D Array

• Pretty similar to 1D arrays:int multTable[4][4] = {{ 1, 2, 3, 4 },{ 2, 4, 6, 8 },{ 3, 6, 9, 12 },{ 4, 8, 12, 16 }};// multTable[(3-1)][(2-1)] == 6

• Unlike 1D arrays, we can only leave out the first dimension if we like; the second is required:

multTable[][3] = { { 1, 2, 3 } };

2D Arrays as Params, Arguments

• Take a 2D array as an argument like so:void myTwoDeeFunc(char input[][5]){ /* etc */ }

• Note that the second dimension must be specified (like in static allocation of 2D arrays earlier)– There’s a funny reason for this, which you’ll learn if you stick

around for memory and the stack (optional)

• Passing a 2D array is the same as a 1D array:char stuff[3][5];myTwoDeeFunc(stuff);

Processing an Entire 2D Array

• Typically uses an outer ‘for’ to go through the rows (#1)…• …And an inner ‘for’ to go through the columns for each row (#2)

int myArray[10][15];

for (int i = 0; i < 10; i++) { // #1for (int j = 0; j < 15; j++) { // #2

myArray[i][j] = 0; // row i, col j}

}

// at the end, all slots are 0

Recap

• C-Strings are just char arrays, with some convenient functions for manipulating them– End in a ‘nul-terminator’ that signifies the end of the string– Leaving off the nul-terminator is disastrous

• C-Strings have special manipulation functions in the <cstring> library, e.g. #include <cstring>

• Whenever you use “Hello!” in your code, you’re using a C-String– (but thankfully std::string is good at converting from C-

Strings to std::strings when needed)

Recap, Part 2• 2D arrays are like 1D arrays, except they have an extra

dimension– by convention, the first dimension is the row, the second is the column– declare:

int myArray[5][10]; // 5 rows, 10 columns

– access/change:myArray[2][3] = 888;// ^ item at row 2, col 3 is now 888

• 2D arrays can be statically allocated, but the second dimension (columns) must be specified

• Similarly, when writing a function that takes a 2D array as a parameter, the second dimension must also be specified:

void myFunc(char inArray[][5]){ /* body of function here… */ }