CS31 Discussion 1E
Spring 17’: week 01
TA: Bo-Jhang Ho
bojhang@cs.ucla.edu
Credit to former TA Chelsea Ju
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 }
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
} bojhang@cs.ucla.edu
} Instructor’s & other TAs’ office hours
} http://cs.ucla.edu/classes/spring17/cs31/
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
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!!
Today’s Topic
} Project #1 } IDE Setup and Configuration } Brief C++ Introduction } Compilation Errors vs Runtime Errors vs Wrong Answer
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.
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, …)
} More on Class website
} http://cs.ucla.edu/classes/spring17/cs31/
Mac Linux Windows
For Xcode users!
Demo – Let’s rock!
} Demo
} Xcode } Old-fashion way to write code
} VIM to write code } Use terminal to compile code and execute binary
Example #1 – A simple program
#include <iostream> using namespace std; int main() { cout << "Hey, this really works!" << endl; return 0; }
} Please just copy and paste this to your code editor
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; }
Details of build process
} What exactly is going on when I click ?
} The hint from Xcode (the IDE)
Details of build process
} What exactly is going on when I click ?
} The hint from Xcode (the IDE)
Source code Executable
Binary Run the program
Details of build process
} What exactly is going on when I click ?
} The hint from Xcode (the IDE)
Source code Executable
Binary Run the program Compile (Build) Execute a program
Demo how to write a program using a terminal (Linux environment)
} Demo time
Why do we need to compile a program?
Source code (human readable) Compile the source code by a compiler Executable/binary (machine readable)
} Demo time again!
What is C++?
} Is a (programming) language so that you can create a valid
computer program
} C++
Machine code (Binary) Assembly C C++
What is C++?
} Is a (programming) language so that you can create a valid
computer program
} C++
Machine code (Binary) Assembly C C++
What is C++?
} Is a (programming) language so that you can create a valid
computer program
} C++
Machine code (Binary) Assembly C C++ Procedural language Object-oriented language
3 types of errors Compilation error Runtime error Logic error
} Syntax error
} Program crashes
(Segmentation fault)
} The program runs into an invalid
state
} Incorrect memory access
} Wrong answer
Compilation error
} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!
23 + 5 = ?
Compilation error
} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!
23 + 5 = ?
Compilation error
} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!
23 + 5 = ?
Logic error
} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!
23 + 5 * 7 = ?
Logic error
} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!
23 + 5 * 7 = ?
Logic error
} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!
23 + 5 * 7 = ?
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; }
Basic components in C++
} Block comment } One-line comment } Include header file } Main function } Variable type } Variable } String } Operator } Return statement
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; }
Example #3 – Another interactive program (Cont’d)
} There must be a reason that the language is designed like this } Delete one character/word, what error messages can you get?
#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; }
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.
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; }
Example #4 – Division
} Test i=5 and j=2? } Test i=7 and j=0? } Test i=99999999999999999 and j=1?
#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; }
Example #5 – What’s wrong with this code?
} Paste it to your programming editor to find the answers
#include <iostream> using namespace std; int main() int a = 10; cout << A << endl; return 0; }
Example #6 – Access invalid index in an array (advanced)
} 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
#include <iostream> using namespace std; int main() { int arr[100]; arr[-99999999] = 3; return 0; }
// 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; }
Project 1 – Generate some bugs
For project 1…
} You may want to explore the language a little bit…
} If } Array } For loop / while loop
} Show Xcode project folder
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