Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)

Similar presentations


Presentation on theme: "CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)"— Presentation transcript:

1 CS 31 Discussion, Week 5 Faisal Alquaddoomi, faisal@cs.ucla.edufaisal@cs.ucla.edu Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)

2 Reflections on Project 3 Get started early Develop incrementally and with tests – write a test case for a function before you’ve implemented it so that you know when it works Functions will save you time and effort – “rule of three”: if you repeat the same large block of code more than twice, it should probably be in a function Passing by reference is useful

3 Common Errors in Project 3 Misunderstandings about how functions are defined – Functions cannot be nested inside of other functions – Functions that are not main() must be called from main() or they won’t run at all Trouble with loops – For loops are good for stepping over items one at a time, not so great for stepping over an unknown number of items – Use while loops when you don’t know how far you’re going to be stepping forward Strings – Before you attempt to access a character in a string, you must be sure that the character exists if (k < text.size()) /* do something with text[k]… */

4 How do we store lots of data? So far we’ve stored data in variables, either as input from the user, hardcoded values, etc. – int x = 32; // ‘x’ is hardcoded as 32 One variable, one value Today we’re going to learn how to store lots of data in a structure called an array

5 The Problem Let’s say you want to store ten values – One variable, one value so far… Could create variables int x1; int x2; int x3; etc. – This is clearly cumbersome, but it’s the best we can do so far – We also can’t iterate over these; we’d have to write an expression to deal with them one at a time You could also stick them in a string – but then they’d be text and you’d have to extract them when you want to use them (also burdensome)

6 The Solution An array is a collection of variables that all have the same type and are declared at the same time Declaring one looks like this: int vals[10]; You can set and read from the array like any other variable – e.g. vals[0] = 12; We’ll go into more depth on that in a bit…

7 Declaring Arrays int vals[10]; The above declares an array of type int and of size 10 Arrays can be any type we’ve learned so far – ints, doubles, strings, booleans… – All of the entries in the array are of the same type The number in the brackets is the number of slots and must be a positive integer – That is, > 0 and a whole number

8 Using Arrays You can put a value into an array (after it’s been declared) like so: vals[3] = 12; The number in the brackets is the slot into which you’re storing – The [3] is called the subscript operator; it’s used to set and retrieve values from an array – Like strings, the slot numbers start at 0 and go up to length-1 In the above, we’re storing to slot #4 out of 10 – It’s illegal to access a slot number past the maximum (your program will crash) You can also get a value out of an array like so: cout << vals[3] << endl;

9 Types and Declaration Variations Static allocation: int myArray[5] = { 5, 3, 6, 7, 9 }; Length can also be implied: int myArray[] = { 8, 8, 8, 9 }; Arrays of strings: string cities[] = { “Glendale”, “Pasadena”, “West Covina” }; As usual, you can have newlines anywhere you like

10 Array Pitfalls There is no “size()” function for arrays – You just have to remember how large they are – Or better yet, use a constant that you can refer to later You can’t print an entire array using cout cout << myArray << endl; // this won’t work Again, attempting to access a slot past the end of the array is the most common (and most disastrous) mistake people make with arrays

11 Arrays and Functions Just like any other variable, you can declare arrays as parameters to functions – Which implies that you can pass them as arguments Unlike other parameters, arrays are always passed by reference – Changes to the array that you pass in the function will be reflected in the caller’s array – If you want to avoid this, use the “const” modifier Let’s look at an example…

12 Summary Arrays are useful for dealing with large numbers of values Arrays can be created like so; size must be > 0 and an integer: int myArray[200]; Arrays can be set to default values: int myOtherArray[] = { 1, 3, 5, 7, 9, 11 }; Array slots are set and retrieved via the subscript (square brackets) operator: – Set: myArray[3] = 12; – Retrieve: for (int i = 0; i < 200; i++) cout << myArray[i] << endl;

13 Summary #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!


Download ppt "CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)"

Similar presentations


Ads by Google