cme211 lec 02. c++ intro
TRANSCRIPT
-
7/28/2019 Cme211 Lec 02. C++ Intro
1/29
CME/EARTHSCI211
ProgramminginC++forScien:stsandEngineers
Fall2011
Lecture2
GeBngstartedwithC++
Tools
Numbers
-
7/28/2019 Cme211 Lec 02. C++ Intro
2/29
Today
WhatisC++ ThemostbasicC++program LessbasicC++program Programmingworkflow Versioncontrol Numbers Somewarmuptutorials
-
7/28/2019 Cme211 Lec 02. C++ Intro
3/29
WhatisC++
Thelow-levellanguage,inheritedfromC Advancedfeaturesfordefiningdatatypesandorganizinglargescaleprojects
Thestandardlibrary,asetofusefuldatastructuresandalgorithms
-
7/28/2019 Cme211 Lec 02. C++ Intro
4/29
ThemostbasicC++program
int main()!{!
return 0;!
}!
1. Uponexecu:on,opera:ngsystemcallsmain()func:on
2. Statementsinthemain()func:onblockareexecutedinorder
3. Thecurlybraces{}defineablockofcode
4. Allstatementsareterminatedwithasemicolon;
5. Thereturn0;statementendsthemain()func:onandthustheprogram
-
7/28/2019 Cme211 Lec 02. C++ Intro
5/29
AslightlylessbasicC++program
#include !int main()!{!std::cout
-
7/28/2019 Cme211 Lec 02. C++ Intro
6/29
Letsseeitinac:on
$emacs-nwprog1.cc
$g++prog1.ccoprog1
$./prog1
$emacsnwprog2.cc
$g++prog2.ccoprog2$./prog2
-
7/28/2019 Cme211 Lec 02. C++ Intro
7/29
$ g++ prog2.cc o prog2! Iusethe$symboltoindicateashellcommand
g++isthenamefortheGNUC++compiler prog2.ccisthenameofthetextfilecontainingtheC++code
-oisthecompileroutputflag prog2isthedesirednameoftheexecutable
-
7/28/2019 Cme211 Lec 02. C++ Intro
8/29
prog2inC#include !int main()!{!
printf("Hello from C!\n");!printf("It's not that different.\n");return 0;!
}!#include grantsaccesstoCsiofunc:ons.
printfisafunc:onthatwritestostandardout.
-
7/28/2019 Cme211 Lec 02. C++ Intro
9/29
Programmingworkflow
1. EditatextfilewithC++code2. Compilethecodewithg++
1. Ifthereareerrors,goto13. Executethecode
1. Ifthereareerrors,(usedebugger)goto14.
Happyhour!
Notebuildyourcodesincrementally.Compile
oen!
-
7/28/2019 Cme211 Lec 02. C++ Intro
10/29
Revisioncontrol
1. Accidentrecoveryohs**t,Ijustdeletedmythesis
2. Experimenta:onIwanttotryX,butIamworriedaboutbreakingY3. Collabora:onilly,whatdidyoudotothe
code?
4. RecordofhistoryCustomerXwantsversion291xfrom2005!
Wewillteach1and2andgivereferences.
-
7/28/2019 Cme211 Lec 02. C++ Intro
11/29
Revisioncontrolhistory
RCSrevisioncontrolsystem,oldfilesystembased(1982)
CVSconcurrentversioningsystem,adapta:onofRCStoclient-servermodel(1990)
SVNsubversion,anewerbeerimplementa:onofCVS(2000)
Git,Bzr,Mecurialdistributedversioncontrolsystems(2005)
WewilluseGit
-
7/28/2019 Cme211 Lec 02. C++ Intro
12/29
WhyGit?
Freeandopensourcesowareavailableonallmajorplaorms
Usedinmanylargeprojects(linuxkernel) Goodtoolsanddocumenta:on Easytogetstartedwithbasics Powerfulfeatures
-
7/28/2019 Cme211 Lec 02. C++ Intro
13/29
Texteditors
Iuseemacs,Alexusesvim,youcanusewhateveryouwant
nanoiseasy(hitControl-G(^G)forhelp) lessallowsyoutolookatafilewithoutedi:ng(hithtoseehelp)
Keyuseoneeditoranduseitwell-unknown
Irecommendemacs,becausevimscaresme
-
7/28/2019 Cme211 Lec 02. C++ Intro
14/29
Themostbasiccommands
Editor Exitcommand Helpcommand
emacs C-xC-c C-h?,C-hi
vim q help
nano ^X(C-xinemacs-speak) ^G(C-g)
less(viewer) q h
-
7/28/2019 Cme211 Lec 02. C++ Intro
15/29
Numbersystemsand
Represen:ons
Numbersystemsbase-ndecimal,binary,octal,hexadecimal
Representa:onofintegers,nega:ve,andfrac:onalnumbers
Mo:vatetheno:onoftype.
Outline
-
7/28/2019 Cme211 Lec 02. C++ Intro
16/29
ases
Symbolsrepresen:ngvaluesfrom0ton-1areusedinweightedcolumns.
Everycolumnsweightisn:mestheweightofthecolumnonitsright.
In base-n (or radix-n) systems:
Name n Digits Ex. Columnweights
Decimal 10 0-9 13.25 101 100 . 10-1
inary 2 0-1 1101.01 21 20 . 2-1
Octal 8 0-7 15.20 81 80 . 8-1
Hexadecimal 16 0-9,A-F D.40 161 160 . 16-1
-
7/28/2019 Cme211 Lec 02. C++ Intro
17/29
ase16example
Numericsymbol Numberrepresented
Example
-
7/28/2019 Cme211 Lec 02. C++ Intro
18/29
SignedandUnsignedIntegers
Howcanwerepresentnega:veintegers?DifferentSchemes
Scheme #bits Range Example
Unsigned n 0to(2n-1)
1310=0000110128 0to25510
16 0to65,53510
32 0to4,294,967,29510
Sign/maginitude n (-2n-1+1)to(2n-1-1)
-1310=1000110128 -12710to12710
1scomplement n (-2n-1+1)to(2n-1-1)
-1310=1111001028 -12710to12710
2scomplement n (-2n-1)to(2n-1-1)
-1310=1111001128 -12810to12710
-
7/28/2019 Cme211 Lec 02. C++ Intro
19/29
RealNumbers
Allra:onalapproxima:ons Twobasickinds
Fixed-pointfixthebinarypointintheword Floa:ng-pointlikescien:ficnota:on
floa:ng-pointisdominantScheme #bits ManFssa Exp
Halfprecision(2008) 16 10+1 5Singleprecision(1985) 32 23+1 8
Doubleprecision(1985) 64 52+1 11
Quadrupleprecision(2008) 128 112+1 15
-
7/28/2019 Cme211 Lec 02. C++ Intro
20/29
Interpreta:onof82E6000716
Theexactsamevaluecanhaveverydifferentmeanings
dependingonhowitisinterpreted.Takeforexamplethe32-bit
valueof0x82e6000716
InterpretaFon Value
32bunsignedinteger 2,196,111,36710
32btwoscomplement -2,098,855,92910
More information is needed type is used to disambiguate.
-
7/28/2019 Cme211 Lec 02. C++ Intro
21/29
Whenthingsgowrong
ThePatriotMissilefailure,inDharan,SaudiArabia,onFebruary25,1991whichresultedin28deaths,is
ul:matelyaributabletopoorhandlingofrounding
errors.
TheexplosionoftheAriane5rocketjustaerli-offonitsmaidenvoyageoffFrenchGuiana,onJune4,
1996,wasul:matelytheconsequenceofasimple
overflow.Cost>$500million.
Referencehp//www.ima.umn.edu/~arnold/disasters/
disasters.html
-
7/28/2019 Cme211 Lec 02. C++ Intro
22/29
Somehomework
Linuxcommandlinetutorialhp//code.google.com/edu/tools101/linux/basics.html
Emacstutorial$emacs
$emacs-nw
keycommandsequenceC-ht
Gittutorialhp//schacon.github.com/git/giutorial.html$mangiutorial
(uptoandincludingViewingProjectHistory)
-
7/28/2019 Cme211 Lec 02. C++ Intro
23/29
References
ClassresourceforC++hp//cplusplus.com/
GoodonlineC++referencehp//en.cppreference.com/w/cpp Gitwebsite,manylinkstodocumenta:onhp//git-scm.com/
OnlineprogrammingresourcesfromGooglehp//code.google.com/edu/
-
7/28/2019 Cme211 Lec 02. C++ Intro
24/29
Appendix
-
7/28/2019 Cme211 Lec 02. C++ Intro
25/29
Gitsetup
1. Tellgityournameandemail$gitconfig--globaluser.nameNickHenderson
2. Tellgityourpreferrededitor$gitconfig--globalcore.editoremacs-nw
(maybeusenanoastheeditor)
-
7/28/2019 Cme211 Lec 02. C++ Intro
26/29
Gitbasics
1. Ini:alizetherepository$gitinit.
2. Stagetheimportantfiles$gitaddprog1.ccprog2.cc3. Commitstagedfilestorepository
$gitcommitmfirstcommit
4. Notethatthehidden.gitdirectoryiscreated.DONOTmodifythisdirectory.
-
7/28/2019 Cme211 Lec 02. C++ Intro
27/29
Gitexample
1. Accidentallydeleteafile$rmprog2.cc
2. Recoverthefilewithgit$gitcheckoutprog2.cc
-
7/28/2019 Cme211 Lec 02. C++ Intro
28/29
GeBngtocorn.stanford.edu
WindowsgetpuyorSecureCRT hp//www.chiark.greenend.org.uk/~sgtatham/puy/ hps//itservices.stanford.edu/service/ess/pc
MacTerminal.app,includedinOSX Connectwithsshprotocol,port22 Servercorn.stanford.edu SUNetIDforusernameandpassword
-
7/28/2019 Cme211 Lec 02. C++ Intro
29/29
GeBngtocorn.stanford.edu
sshistheSecureShellclient
Notetheredirec:ontocorn##.stanford.edu AFSfilesareavailableonallnodes.Youcanlogintonodesdirectly.Some:mesyouwanttodothis.