discussion6_ans

# Discussion6_ans - CS31 Introduction to Computer Science I Discussion 6 May 6 2011 TA Paul Wais([email protected]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */ Discussion Section 1L Information

This preview shows pages 1–3. Sign up to view the full content.

CS31: Introduction to Computer Science I Discussion 6 May 6, 2011 TA: Paul Wais ( [email protected] ) Discussion Section 1L Information TA: Paul Wais ( [email protected] ) Ofﬁce Hours: Thursdays 11:30am - 1:30pm in Boelter Hall 2432 Discusion Website: http://www.cs.ucla.edu/ ˜ pwais 1 Midterm: Common Problems Let’s brieﬂy review a couple concepts related to midterm problems that people had trouble with. For a more complete list, see http://cs31.org/10-basic-mistakes 1.1 Return vs Break vs Continue vs cout return exits a function, possibly yielding a value (i.e. if the function is not void ) to the code invoking the function. cout will print something to a terminal. break will exit a loop or a switch statement. If there are several nested loops and/or switch statements, break always exits the body of the loop or switch where the break appears. continue immediately advances a loop to the condition check that occurs after an iteration. What does the example code below print? 1 int findx( int a[], int n, int x) 2 { 3 int i = 0; 4 while (i < n) 5 { 6 if (a[i] != x) 7 { 8 i++; 9 continue ; 10 } 11 else 12 break ; // We could also return i here. Why? 13 } 14 return i; 15 } 16 17 int main() 18 { 19 int arr[5] = {1, 2, 3, 4, 5}; 20 int q = findx(arr, 5, 1); 21 q = findx(arr, 5, 5); 22 findx(arr, 5, 2); 23 cout << findx(arr, 5, 3); 24 } Answer: 2 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
1.2 Pass by Value, Pass by Reference, and Array Parameters Pass by Value means that a function gets a copy of a variable’s value. Pass by Reference means that a function gets direct access to a variable. Array Parameters are always Pass by Reference . What does the example code below print? 1 void pbv( int x) 2 { 3 x * = 10; 4 } 5 6 void pbr( int 7 { 8 y * = 10; 9 } 10 11 void arr( int x[]) 12 { 13 x[0] * = 10; 14 } 15 16 int main() 17 { 18 int x = 1, y = 1; 19 int z[1] = {1}; 20 pbv(x); 21 pbv(z[0]); 22 pbr(z[0]); 23 pbv(y); 24 arr(z); 25 pbr(z[0]); 26 27 cout << "x is " << x << " y is " << y << " z[0] is " << z[0] << endl; 28 } Answer: x is 1 y is 1 z[0] is 1000 1.3 Multi-dimensional Arrays: Pascal’s Triangle Often it will be useful useful to create an array of arrays (of arrays of arrays. ..). Let’s discuss how to create a 2-dimensional array (i.e. a grid ) in C++. In Project 3, the graphing library used a 2-dimensional array of char s to store grid data. In particular, the grid was declared as: char grid[NROWS][NCOLS]; Which is an array of NROWS elements, where each element is an array of NCOLS char s. Let’s see how mult-dimensional arrays work in the context of a program that prints Pascal’s triangle. For example, Pascal’s
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 07/06/2011 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

### Page1 / 7

Discussion6_ans - CS31 Introduction to Computer Science I Discussion 6 May 6 2011 TA Paul Wais([email protected] Discussion Section 1L Information

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online