cs31 discussion 1e - cs | computer...
TRANSCRIPT
![Page 1: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/1.jpg)
CS31 Discussion 1E Spring 17’: week 01
TA: Bo-Jhang Ho [email protected]
Credit to former TA Chelsea Ju
![Page 2: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/2.jpg)
Resources } My Office Hour & Location
} Tuesday 12:30pm – 3:30pm } BH2432 } The most efficient way to use time!
} My Email } [email protected]
} Instructor’s & other TAs’ office hours } http://cs.ucla.edu/classes/spring17/cs31/
![Page 3: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/3.jpg)
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
![Page 4: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/4.jpg)
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!!
![Page 5: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/5.jpg)
Today’s Topic } Project #1 } IDE Setup and Configuration } Brief C++ Introduction } Compilation Errors vs Runtime Errors vs Wrong Answer
![Page 6: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/6.jpg)
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.
![Page 7: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/7.jpg)
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++
![Page 8: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/8.jpg)
For Xcode users!
![Page 9: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/9.jpg)
Demo – Let’s rock! } Demo
} Xcode } Old-fashion way to write code
} VIM to write code } Use terminal to compile code and execute binary
![Page 10: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/10.jpg)
Example #1 – A simple program
#include<iostream>usingnamespacestd;intmain(){cout<<"Hey,thisreallyworks!"<<endl;return0;}
} Please just copy and paste this to your code editor
![Page 11: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/11.jpg)
Example #2 – A simple interactive program
} Please just copy and paste this to your code editor #include<iostream>usingnamespacestd;intmain(){intnum;cout<<"Hey,givemeanumber:";cin>>num;intnumSquare=num*num;cout<<"Thesquareofthisnumberis"<<numSquare<<endl;return0;}
![Page 12: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/12.jpg)
Details of build process } What exactly is going on when I click ?
} The hint from Xcode (the IDE)
![Page 13: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/13.jpg)
Details of build process } What exactly is going on when I click ?
} The hint from Xcode (the IDE)
Source code Executable or
Binary
Run the program
![Page 14: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/14.jpg)
Details of build process } What exactly is going on when I click ?
} The hint from Xcode (the IDE)
Source code Executable or
Binary
Run the program
Compile (Build)
Execute a program
![Page 15: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/15.jpg)
Demo how to write a program using a terminal (Linux environment)
} Demo time
![Page 16: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/16.jpg)
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!
![Page 17: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/17.jpg)
What is C++?
} Is a (programming) language so that you can create a valid computer program
} C++
Machine code (Binary)
Assembly
C
C++
![Page 18: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/18.jpg)
What is C++?
} Is a (programming) language so that you can create a valid computer program
} C++
Machine code (Binary)
Assembly
C
C++
![Page 19: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/19.jpg)
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
![Page 20: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/20.jpg)
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
![Page 21: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/21.jpg)
Compilation error
} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!
23 + 5 = ?
![Page 22: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/22.jpg)
Compilation error
} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!
23 + 5 = ?
![Page 23: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/23.jpg)
Compilation error
} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!
23 + 5 = ?
![Page 24: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/24.jpg)
Logic error
} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!
23 + 5 * 7 = ?
![Page 25: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/25.jpg)
Logic error
} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!
23 + 5 * 7 = ?
196
![Page 26: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/26.jpg)
Logic error
} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!
23 + 5 * 7 = ?
58
![Page 27: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/27.jpg)
The skeleton C++ code
} This is the only time in this quarter I ask you to memorize codes!!
#include<iostream>usingnamespacestd;intmain(){return0;}
![Page 28: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/28.jpg)
Basic components in C++
} Block comment } One-line comment } Include header file
} Main function } Variable type } Variable
} String } Operator } Return statement
![Page 29: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/29.jpg)
Example #3 – Another interactive program
} What is cin and cout? } How do you distinguish >> and <<?
#include<iostream>usingnamespacestd;intmain(){inti,j;cout<<"Pleaseenterthevaluefori:"<<endl;cin>>i;cout<<"Pleaseenterthevalueforj:"<<endl;cin>>j;cout<<"i+j="<<i+j<<endl;return0;}
![Page 30: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/30.jpg)
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>usingnamespacestd;intmain(){inti,j;cout<<"Pleaseenterthevaluefori:"<<endl;cin>>i;cout<<"Pleaseenterthevalueforj:"<<endl;cin>>j;cout<<"i+j="<<i+j<<endl;return0;}
![Page 31: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/31.jpg)
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.
![Page 32: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/32.jpg)
Example #4 – Division #include<iostream>usingnamespacestd;intmain(){inti,j;cout<<"Pleaseenterthevaluefori:"<<endl;cin>>i;cout<<"Pleaseenterthevalueforj:"<<endl;cin>>j;cout<<"i/j="<<i/j<<endl;return0;}
![Page 33: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/33.jpg)
Example #4 – Division
} Test i=5 and j=2? } Test i=7 and j=0? } Test i=99999999999999999 and j=1?
#include<iostream>usingnamespacestd;intmain(){inti,j;cout<<"Pleaseenterthevaluefori:"<<endl;cin>>i;cout<<"Pleaseenterthevalueforj:"<<endl;cin>>j;cout<<"i/j="<<i/j<<endl;return0;}
![Page 34: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/34.jpg)
Example #5 – What’s wrong with this code?
} Paste it to your programming editor to find the answers
#include<iostream>usingnamespacestd;intmain()inta=10;cout<<A<<endl;return0;}
![Page 35: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/35.jpg)
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>usingnamespacestd;intmain(){intarr[100];arr[-99999999]=3;return0;}
![Page 36: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/36.jpg)
//CodeforProject1//Reportpollresults#include<iostream>usingnamespacestd;//pp.38-39inSavitch6/eexplainsthislineintmain(){intnumSurveyed;intnumApprove;intnumDisapprove;cout<<"Howmanypeopleweresurveyed?";cin>>numSurveyed;cout<<"Howmanyofthemapproveofthewaythepresidentishandlinghisjob?";cin>>numApprove;cout<<"Howmanyofthemdisapproveofthewaythepresidentishandlinghisjob?";cin>>numDisapprove;doublepctApprove=100.0*numApprove/numSurveyed;doublepctDisapprove=100.0*numDisapprove/numSurveyed;cout.setf(ios::fixed);//seepp.32-33inSavitch6/ecout.precision(1);cout<<endl;cout<<pctApprove<<"%saytheyapprove."<<endl;cout<<pctDisapprove<<"%saytheydisapprove."<<endl;if(numApprove>numDisapprove)cout<<"Morepeopleapprovethandisapprove."<<endl;elsecout<<"Morepeopledisapprovethanapprove."<<endl;}
Project 1 – Generate some bugs
![Page 37: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/37.jpg)
For project 1… } You may want to explore the language a little bit…
} If } Array } For loop / while loop
} Show Xcode project folder
![Page 38: CS31 Discussion 1E - CS | Computer Scienceweb.cs.ucla.edu/~tsenghy/class/cs31/slides/week01_bo.pdfGrading } Homework: 40% } Around 7 projects } The work should be submitted electronically](https://reader036.vdocuments.net/reader036/viewer/2022071409/6103541d09e789301341c094/html5/thumbnails/38.jpg)
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