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

cs31 discussion 1e
SMART_READER_LITE
LIVE PREVIEW

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 }


slide-1
SLIDE 1

CS31 Discussion 1E

Spring 17’: week 01

TA: Bo-Jhang Ho

bojhang@cs.ucla.edu

Credit to former TA Chelsea Ju

slide-2
SLIDE 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/

slide-3
SLIDE 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

slide-4
SLIDE 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!!

slide-5
SLIDE 5

Today’s Topic

} Project #1 } IDE Setup and Configuration } Brief C++ Introduction } Compilation Errors vs Runtime Errors vs Wrong Answer

slide-6
SLIDE 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.

slide-7
SLIDE 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, …)

} More on Class website

} http://cs.ucla.edu/classes/spring17/cs31/

Mac Linux Windows

  • Xcode
  • Command Line
  • Terminal
  • Gnu toolchain
  • Visual C++
slide-8
SLIDE 8

For Xcode users!

slide-9
SLIDE 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

slide-10
SLIDE 10

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

slide-11
SLIDE 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; }

slide-12
SLIDE 12

Details of build process

} What exactly is going on when I click ?

} The hint from Xcode (the IDE)

slide-13
SLIDE 13

Details of build process

} What exactly is going on when I click ?

} The hint from Xcode (the IDE)

Source code Executable

  • r

Binary Run the program

slide-14
SLIDE 14

Details of build process

} What exactly is going on when I click ?

} The hint from Xcode (the IDE)

Source code Executable

  • r

Binary Run the program Compile (Build) Execute a program

slide-15
SLIDE 15

Demo how to write a program using a terminal (Linux environment)

} Demo time

slide-16
SLIDE 16

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!

slide-17
SLIDE 17

What is C++?

} Is a (programming) language so that you can create a valid

computer program

} C++

Machine code (Binary) Assembly C C++

slide-18
SLIDE 18

What is C++?

} Is a (programming) language so that you can create a valid

computer program

} C++

Machine code (Binary) Assembly C C++

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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

slide-21
SLIDE 21

Compilation error

} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!

23 + 5 = ?

slide-22
SLIDE 22

Compilation error

} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!

23 + 5 = ?

slide-23
SLIDE 23

Compilation error

} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!

23 + 5 = ?

slide-24
SLIDE 24

Logic error

} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!

23 + 5 * 7 = ?

slide-25
SLIDE 25

Logic error

} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!

23 + 5 * 7 = ?

196

slide-26
SLIDE 26

Logic error

} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!

23 + 5 * 7 = ?

58

slide-27
SLIDE 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; }

slide-28
SLIDE 28

Basic components in C++

} Block comment } One-line comment } Include header file } Main function } Variable type } Variable } String } Operator } Return statement

slide-29
SLIDE 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; }

slide-30
SLIDE 30

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; }

slide-31
SLIDE 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.

slide-32
SLIDE 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; }

slide-33
SLIDE 33

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; }

slide-34
SLIDE 34

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; }

slide-35
SLIDE 35

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; }

slide-36
SLIDE 36

// 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

slide-37
SLIDE 37

For project 1…

} You may want to explore the language a little bit…

} If } Array } For loop / while loop

} Show Xcode project folder

slide-38
SLIDE 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