{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


discussion3_answ - CS31 Introduction to Computer Science I...

Info iconThis 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 3 April 15, 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 Notes and other resources will be posted on the Discussion Website. If you would like to meet me outside of the posted office hours, please email me. Also, if you’d like for me to cover something specific during discussion section, please send me email. If there is anything I can do to improve Discussion Section, please feel free to let me know. Using The Debugger Let’s use the debugger to step through one of the code examples from project 2. First, we’ll create a project in Visual Studio and paste in the following code to a file called program.cpp : #include <iostream> using namespace std; int main() { int len; cout << "Enter a number: "; cin >> len; for (int i = 0; i < len; i++) { for (int j = i+1; j < len; j++) { cout << ’ ’; } cout << "#" << endl; } } Next, right click on the line for (int i = 0; i < len; i++) and choose Breakpoint >> Insert Breakpoint and hit the green Play button to start the program with debugging . The program will start running. Enter input (e.g. 5) and hit enter. The program will now stop at the breakpoint we created, and we can now step through the program manually, observing how the values of variables change as we go. 1
Background image of page 1

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

View Full Document Right Arrow Icon
Using the debugger is probably best learned by watching somebody else do it. For Visual Studio, check out this video (try to fast forward until 1:30 or so): http://www.youtube.com/user/UCLAChuck#p/c/2775EB517DDA7A95/0/z5gBIizwsY0 The debugger in Xcode is very similar to the one in Visual Studio. Check out this tutorial: http://bit.ly/iecq83 Notes on Project 2 Solutions for Project 2 have been posted on the Course Website. There are a few key points worth noting: Use const for Constants It’s good style to declare const variables for storing constants (as opposed to just leaving numbers like 0.20 in code). Furthermore, to make it very clear that a variable is a constant, we usually use the convention of writing constant variable names using all uppercase characters. For example, here’s a snippet of code from the Project 2 solution: const double BRACKET_CUTOFF_1 = 500; const double BRACKET_CUTOFF_2 = 900; const double RATE_1 = 0.20; const double RATE_2_BASIC = 0.10; Defining constants using the above conventions makes code easier to understand and maintain. For instance, if borrowing rates change from 0.15 to 0.2, we can easily update a single constant declaration rather than find and replace all instances of 0.15 in the code to 0.2. Finally, using const can cause compile errors that will help you uncover logical errors before running a program.
Background 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 ]}