{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

discussion6_ans

discussion6_ans - CS31 Introduction to Computer Science I...

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

View Full Document Right Arrow Icon
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] ) Office Hours: Thursdays 11:30am - 1:30pm in Boelter Hall 2432 Discusion Website: http://www.cs.ucla.edu/ ˜ pwais 1 Midterm: Common Problems Let’s briefly 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
Image of page 1

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

View Full Document Right Arrow Icon
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 &y) 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
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern