cs31 discussion 1e
play

CS31 Discussion 1E Spring 17: week 01 TA: Bo-Jhang Ho - PowerPoint PPT Presentation

CS31 Discussion 1E Spring 17: week 01 TA: Bo-Jhang Ho bojhang@cs.ucla.edu Credit to former TA Chelsea Ju Resources } My Office Hour & Location } Tuesday 12:30pm 3:30pm } BH2432 } The most efficient way to use time! } My Email }


  1. CS31 Discussion 1E Spring 17’: week 01 TA: Bo-Jhang Ho bojhang@cs.ucla.edu Credit to former TA Chelsea Ju

  2. Resources } My Office Hour & Location } Tuesday 12:30pm – 3:30pm } BH2432 } The most efficient way to use time! } My Email } bojhang@cs.ucla.edu } Instructor’s & other TAs’ office hours } http://cs.ucla.edu/classes/spring17/cs31/

  3. Grading } Homework: 40% } Around 7 projects } The work should be submitted electronically } Late submissions will be penalized by every second } Midterm: 25% } Thursday, April 27, 2017 } Monday, May 22, 2017 } Final exam: 35% } 11:30am – 2:30pm Saturday, June 10, 2017 } Assignment score capping policy

  4. Learning Tips } Keep up with the lecture material } Start early } Problem solving & coding may take longer than you think } Read carefully } Double/Triple check the project requirements } Develop incrementally } Add bits of code at a time, then compile, and run. } It is easier to isolate any bugs, and to understand if the code is doing what you expected } Don’t give up!!

  5. Today’s Topic } Project #1 } IDE Setup and Configuration } Brief C++ Introduction } Compilation Errors vs Runtime Errors vs Wrong Answer

  6. Project #1 } Due 11pm Tuesday April 11, 2017 } Read the specification carefully, don’t get a 0 in any assignment! } What to submit? } C++ scripts } original.cpp } logic_error.cpp } compile_error.cpp } Report in MS Word format or Plain text } report.doc or report.docx or report.txt } Discuss any error messages the compiler reports, and incorrect/unusual results. } Keep it short!!!!!! } Compress everything into one zip file, and submit online.

  7. IDE Configuration } IDE = Integrated Development Environment } A programming environment that has been packaged as an application program } A graphical user interface (GUI), and a code editor } A compiler } A debugger } A lot of different tools (energy analysis, CPU utilization, …) Mac Linux Windows - Xcode - Terminal - Visual C++ - Command Line - Gnu toolchain } More on Class website } http://cs.ucla.edu/classes/spring17/cs31/

  8. For Xcode users!

  9. Demo – Let’s rock! } Demo } Xcode } Old-fashion way to write code } VIM to write code } Use terminal to compile code and execute binary

  10. Example #1 – A simple program } Please just copy and paste this to your code editor #include <iostream> using namespace std; int main() { cout << "Hey, this really works!" << endl; return 0; }

  11. Example #2 – A simple interactive program } Please just copy and paste this to your code editor #include <iostream> using namespace std; int main() { int num; cout << "Hey, give me a number: "; cin >> num; int numSquare = num * num; cout << "The square of this number is " << numSquare << endl; return 0; }

  12. Details of build process } What exactly is going on when I click ? } The hint from Xcode (the IDE)

  13. Details of build process } What exactly is going on when I click ? } The hint from Xcode (the IDE) Source code Run the program Executable or Binary

  14. Details of build process } What exactly is going on when I click ? } The hint from Xcode (the IDE) Compile Execute (Build) a program Source code Run the program Executable or Binary

  15. Demo how to write a program using a terminal (Linux environment) } Demo time

  16. Why do we need to compile a program? Compile the source code by a compiler Executable/binary Source code (machine readable) (human readable) } Demo time again!

  17. What is C++? } Is a (programming) language so that you can create a valid computer program } C++ C++ C Assembly Machine code (Binary)

  18. What is C++? } Is a (programming) language so that you can create a valid computer program } C++ C++ C Assembly Machine code (Binary)

  19. What is C++? } Is a (programming) language so that you can create a valid computer program } C++ Object-oriented C++ language Procedural C language Assembly Machine code (Binary)

  20. 3 types of errors Compilation error } Syntax error } Program crashes (Segmentation fault) Runtime error } The program runs into an invalid state } Incorrect memory access } Wrong answer Logic error

  21. Compilation error } Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!! 23 + 5 = ?

  22. Compilation error } Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!! 23 + 5 = ?

  23. Compilation error } Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!! 23 + 5 = ?

  24. Logic error } Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!! 23 + 5 * 7 = ?

  25. Logic error } Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!! 23 + 5 * 7 = ? 196

  26. Logic error } Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!! 23 + 5 * 7 = ? 58

  27. The skeleton C++ code } This is the only time in this quarter I ask you to memorize codes!! #include <iostream> using namespace std; int main() { return 0; }

  28. Basic components in C++ } Block comment } Main function } String } One-line comment } Variable type } Operator } Include header file } Variable } Return statement

  29. Example #3 – Another interactive program } What is cin and cout ? } How do you distinguish >> and << ? #include <iostream> using namespace std; int main() { int i, j; cout << "Please enter the value for i: " << endl; cin >> i; cout << "Please enter the value for j: " << endl; cin >> j; cout << "i + j = " << i + j << endl; return 0; }

  30. Example #3 – Another interactive program (Cont’d) #include <iostream> using namespace std; int main() { int i, j; cout << "Please enter the value for i: " << endl; cin >> i; cout << "Please enter the value for j: " << endl; cin >> j; cout << "i + j = " << i + j << endl; return 0; } } There must be a reason that the language is designed like this } Delete one character/word, what error messages can you get?

  31. Example #3 – Another interactive program (Cont’d) } Potential errors you can get: } Unbalanced bracket } Missing semicolon } Undefined variable } Undefined operator } No number after return keyword } Library doesn’t exist } Etc.

  32. Example #4 – Division #include <iostream> using namespace std; int main() { int i, j; cout << "Please enter the value for i: " << endl; cin >> i; cout << "Please enter the value for j: " << endl; cin >> j; cout << "i / j = " << i / j << endl; return 0; }

  33. Example #4 – Division #include <iostream> using namespace std; int main() { int i, j; cout << "Please enter the value for i: " << endl; cin >> i; cout << "Please enter the value for j: " << endl; cin >> j; cout << "i / j = " << i / j << endl; return 0; } } Test i=5 and j=2? } Test i=7 and j=0? } Test i=99999999999999999 and j=1?

  34. Example #5 – What’s wrong with this code? #include <iostream> using namespace std; int main() int a = 10; cout << A << endl; return 0; } } Paste it to your programming editor to find the answers

  35. Example #6 – Access invalid index in an array (advanced) #include <iostream> using namespace std; int main() { int arr[100]; arr[-99999999] = 3; return 0; } } Since there are only 100 available spots whose index ranged from 0 to 99, write back to any other index beyond this range will screw the memory and may crash the program

  36. Project 1 – Generate some bugs // Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp. 38-39 in Savitch 6/e explains this line int main() { int numSurveyed; int numApprove; int numDisapprove; cout << "How many people were surveyed? "; cin >> numSurveyed; cout << "How many of them approve of the way the president is handling his job? "; cin >> numApprove; cout << "How many of them disapprove of the way the president is handling his job? "; cin >> numDisapprove; double pctApprove = 100.0 * numApprove / numSurveyed; double pctDisapprove = 100.0 * numDisapprove / numSurveyed; cout.setf(ios::fixed); // see pp. 32-33 in Savitch 6/e cout.precision(1); cout << endl; cout << pctApprove << "% say they approve." << endl; cout << pctDisapprove << "% say they disapprove." << endl; if (numApprove > numDisapprove) cout << "More people approve than disapprove." << endl; else cout << "More people disapprove than approve." << endl; }

  37. For project 1… } You may want to explore the language a little bit… } If } Array } For loop / while loop } Show Xcode project folder

  38. Reminder of my office hour again } 12:30pm – 3:30pm Tuesday } Bring your laptop if you can } I’ll be out of town from 4/13 – 4/19

Recommend


More recommend