Post on 03-Mar-2021
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
- Xcode - Command Line
- Terminal - Gnu toolchain
- Visual C++
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>usingnamespacestd;intmain(){cout<<"Hey,thisreallyworks!"<<endl;return0;}
} 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>usingnamespacestd;intmain(){intnum;cout<<"Hey,givemeanumber:";cin>>num;intnumSquare=num*num;cout<<"Thesquareofthisnumberis"<<numSquare<<endl;return0;}
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 or
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 or
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 = ?
196
Logic error
} Use the language c++ to communicate with machines } Machines cannot understand even they miss a word!!
23 + 5 * 7 = ?
58
The skeleton C++ code
} This is the only time in this quarter I ask you to memorize codes!!
#include<iostream>usingnamespacestd;intmain(){return0;}
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>usingnamespacestd;intmain(){inti,j;cout<<"Pleaseenterthevaluefori:"<<endl;cin>>i;cout<<"Pleaseenterthevalueforj:"<<endl;cin>>j;cout<<"i+j="<<i+j<<endl;return0;}
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;}
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>usingnamespacestd;intmain(){inti,j;cout<<"Pleaseenterthevaluefori:"<<endl;cin>>i;cout<<"Pleaseenterthevalueforj:"<<endl;cin>>j;cout<<"i/j="<<i/j<<endl;return0;}
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;}
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;}
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;}
//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
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