method of work
play

Method of work Divide and conqer! Start with the core - PDF document

2013-09-10 Method of work Divide and conqer! Start with the core part of the program. Lecture 1 Add variables and libraries only


  1. 2013-­‑09-­‑10 ¡ Method ¡of ¡work ¡ • Divide ¡and ¡conqer! ¡ • Start ¡with ¡the ¡core ¡part ¡of ¡the ¡program. ¡ Lecture ¡1 ¡ • Add ¡variables ¡and ¡libraries ¡only ¡when ¡they ¡ become ¡needed ¡(as ¡opposed ¡to ¡”just ¡in ¡case”). ¡ • Compile ¡aMer ¡each ¡line ¡(oMen!) ¡ Basic ¡C++ ¡ • Solve ¡compilaNon ¡problems ¡before ¡you ¡add ¡ more ¡code. ¡ Terminal ¡ Tool: ¡Emacs ¡ • Provide ¡a ¡command ¡line ¡where ¡you ¡can ¡enter ¡text ¡ • Invoked ¡from ¡command ¡line ¡as ¡”emacs” ¡ commands, ¡right-­‑click ¡on ¡desktop ¡to ¡get ¡it ¡ • Emacs ¡is ¡the ¡extensible, ¡customizable, ¡self-­‑ • Stops ¡and ¡wait ¡for ¡programs ¡to ¡finish ¡unless ¡you ¡add ¡ documenNng ¡real-­‑Nme ¡display ¡editor. ¡ an ¡& ¡last ¡on ¡line ¡to ¡start ¡it ¡in ¡background ¡ • Emacs ¡can ¡do ¡anything, ¡everything ¡you ¡want ¡but ¡ • Up-­‑arrow ¡retrieve ¡previuos ¡command ¡ one ¡thing: ¡ it ¡will ¡not ¡tell ¡you ¡how ¡to ¡do ¡it ¡ • Ctrl-­‑a ¡goes ¡to ¡beginning ¡of ¡line ¡ • Ctrl-­‑e ¡goes ¡to ¡end ¡of ¡line ¡ • Emacs ¡work ¡without ¡using ¡graphical ¡menus ¡or ¡ • Ctrl-­‑c ¡terminates ¡currently ¡running ¡program ¡ mouse, ¡most ¡is ¡hidden ¡”under ¡the ¡hood” ¡ • Ctrl-­‑z ¡suspends ¡currently ¡running ¡program ¡ • Only ¡a ¡few ¡basic ¡things ¡are ¡available ¡in ¡menus ¡ • ”fg” ¡resume ¡suspended ¡programs ¡in ¡foreground ¡ • Be ¡curious ¡and ¡ask: ¡”How ¡do ¡I ¡... ¡in ¡emacs.” ¡ • ”bg” ¡resume ¡suspended ¡programs ¡in ¡background ¡ Tool: ¡Compiler ¡ Comment ¡ • Wri]en ¡along ¡with ¡C++ ¡code ¡ • Invoked ¡from ¡command ¡line ¡as ¡”g++11” ¡ • Ignored ¡by ¡compiler ¡ • Invokes ¡a ¡ preprocessing ¡program ¡that ¡can ¡generate ¡ errors ¡ • Tell ¡humans ¡something ¡useful ¡about ¡the ¡code ¡ // comment to end of line • Translates ¡C++ ¡text ¡to ¡executable ¡binary ¡code ¡ /* multi-line – Issues ¡warnings ¡when ¡something ¡is ¡ wrong ¡ or ¡weird ¡ comment */ – Issues ¡errors ¡when ¡you ¡do ¡have ¡syntax ¡error(s) ¡ #if 0 – Correct ¡errors ¡and ¡warnings ¡in ¡order! ¡ not a real comment but a useful • Invokes ¡a ¡postprocessing ¡ linker ¡that ¡may ¡generate ¡ preprocessing directive to get further ¡errors ¡ the same effect • ”a.out” ¡is ¡the ¡default ¡name ¡of ¡generated ¡programs ¡ #end 1 ¡

  2. 2013-­‑09-­‑10 ¡ Sequence ¡and ¡block ¡ Standard ¡skeleton ¡ • A ¡sequenNal ¡list ¡of ¡statements ¡ // A minimal C++ program • Each ¡statement ¡is ¡terminated ¡with ¡a ¡semicolon ¡ int main() • All ¡statements ¡are ¡inside ¡some ¡block ¡ { • EnNre ¡block ¡form ¡a ¡compound ¡statement ¡ // your statements • Statements ¡are ¡executed ¡one ¡by ¡one ¡in ¡order: ¡ // ... { // beginning of block statement1; return 0; statement2; } statement3; } // end of block Variable ¡(1 ¡of ¡2) ¡ Variable ¡(2 ¡of ¡2) ¡ Consists ¡of: ¡ Three ¡kinds ¡of ¡variables: ¡ • Name ¡ • Fundamental ¡(basic) ¡ – stores ¡a ¡value ¡of ¡fundamental ¡type, ¡nothing ¡more ¡ – so ¡we ¡can ¡refer ¡to ¡it’s ¡storage ¡locaNon ¡ • Object ¡ – at ¡lower ¡level ¡converted ¡to ¡an ¡adress ¡in ¡memory ¡ – stores ¡values ¡Ned ¡to ¡an ¡derived ¡type ¡(struct, ¡class, ¡union, ¡enum) ¡ – can ¡be ¡aliased ¡by ¡way ¡of ¡reference ¡ – operaNons ¡associated ¡to ¡the ¡type ¡are ¡provided ¡ • Value ¡ – more ¡later ¡in ¡the ¡course ¡ – what ¡we ¡store ¡in ¡the ¡locaNon ¡ • Pointer ¡ – will ¡ never ¡be ¡empty, ¡not ¡even ¡before ¡we ¡fill ¡it ¡ – stores ¡just ¡the ¡adress ¡of ¡some ¡other ¡variable ¡ • Type ¡ – requires ¡cauNon: ¡what ¡if ¡the ¡adress ¡ does ¡not ¡contain ¡said ¡ – size ¡of ¡storage ¡locaNon ¡ variable? ¡ – interpretaNon ¡of ¡stored ¡value ¡ – more ¡later ¡in ¡the ¡course ¡ Naming ¡of ¡variables ¡ Variable ¡declaraNon ¡and ¡definiNon ¡ • The ¡name ¡should ¡inform ¡humans ¡of ¡the ¡purpose ¡of ¡the ¡ • You ¡specify ¡the ¡type ¡of ¡the ¡variable ¡followed ¡by ¡ variable. ¡ the ¡name ¡of ¡the ¡variable. ¡ • Good ¡names ¡are ¡typically ¡5-­‑20 ¡characters. ¡ – Both ¡declares ¡and ¡defines ¡the ¡variable ¡in ¡most ¡cases. ¡ • Choosen ¡name ¡become ¡an ¡ iden4fier . ¡ – The ¡type ¡must ¡be ¡one ¡in ¡a ¡predefined ¡set. ¡ • Some ¡idenNfiers ¡are ¡ reserved ¡for ¡use ¡by ¡the ¡language ¡ – The ¡name ¡is ¡yours ¡to ¡choose ¡ wisely . ¡ itself. ¡You ¡can ¡not ¡use ¡those ¡keywords. ¡ – The ¡variable ¡may ¡opNonally ¡be ¡iniNalized ¡( wise ). ¡ • Name ¡must ¡begin ¡with ¡a ¡le]er ¡(or ¡underscore) ¡and ¡ – IniNalizaNon ¡use ¡curly ¡braces ¡{} ¡ contain ¡only ¡le]ers ¡and ¡underscore ¡ • DeclaraNon ¡ • Names ¡are ¡case ¡sensiNve ¡ – Tells ¡the ¡compiler ¡the ¡variable ¡exists ¡somewhere. ¡ • Case ¡is ¡by ¡convenNon ¡used ¡to ¡carry ¡informaNon ¡for ¡the ¡ • DefiniNon ¡ programmer ¡according ¡to ¡coosen ¡style ¡rules ¡ – Creates ¡a ¡variable ¡in ¡program ¡memory. ¡ 2 ¡

  3. 2013-­‑09-­‑10 ¡ Constants ¡ Fundamental ¡type: ¡bool ¡ • A ¡variable ¡can ¡be ¡declared ¡ const ¡ • stores ¡either ¡1 ¡or ¡0 ¡ • ModificaNon ¡of ¡a ¡const ¡variable ¡will ¡give ¡compilaNon ¡ • literal ¡true ¡alias ¡for ¡1 ¡ error. ¡ • literal ¡false ¡alias ¡for ¡0 ¡ • The ¡compiler ¡can ¡treat ¡constant ¡variables ¡more ¡ • any ¡integer ¡is ¡automaNcally ¡converted ¡ efficiently. ¡ • integer ¡with ¡zero ¡value ¡becomes ¡ false ¡ • The ¡programmer ¡have ¡less ¡worries ¡with ¡constant ¡ variables ¡than ¡other. ¡ Big ¡benefit! ¡ • integers ¡not ¡zero ¡becomes ¡ true ¡ • A ¡const ¡variable ¡is ¡ much ¡be:er ¡than ¡a ¡literal ¡because ¡ bool i_am_legend; you ¡refer ¡to ¡it ¡by ¡ name , ¡and ¡change ¡it ¡at ¡ one ¡place. ¡ bool i_am_a_programmer{true}; • Constants ¡use ¡upper ¡case ¡le]ers ¡by ¡convenNon. ¡ bool awful{4711}; // will be true const int SIZE{1000}; Variable ¡lifeNme, ¡scope ¡ Fundamental ¡type: ¡int ¡ Each ¡variable ¡have ¡a ¡limited ¡scope. ¡It ¡is ¡only ¡usable ¡in ¡it’s ¡scope. ¡ • usually ¡4 ¡byte ¡ • • Scope ¡is ¡determined ¡by ¡placement ¡in ¡code. ¡ signed ¡or ¡ unsigned ¡ • • Variables ¡start ¡to ¡exist ¡when ¡defined. ¡ short ¡or ¡ long ¡or ¡ long ¡long ¡ • Variables ¡cease ¡to ¡exist ¡at ¡the ¡end ¡of ¡the ¡block ¡it ¡was ¡defined ¡inside. ¡ • { • stored ¡as ¡two’s ¡complement ¡ // no variables exist bool b; • limited ¡range ¡ // b exists • stores ¡exact ¡representaNon ¡of ¡any ¡integer ¡in ¡range ¡ { // b exists wraps ¡around ¡when ¡overflow ¡or ¡underflow ¡ • bool c; int x; // both b and c exist } int y{-4711}; // only b exists unsigned long long int big{9876543210L}; } Fundamental ¡type: ¡char ¡ Fundamental ¡type: ¡float ¡ • very ¡limited ¡range ¡ • stored ¡as ¡sign, ¡exponent ¡(8 ¡bit) ¡ ¡ • stores ¡any ¡integer ¡in ¡range ¡ and ¡fracNon ¡(23 ¡bit) ¡ signed ¡or ¡ unsigned ¡ • • displayed ¡as ¡symbol ¡in ¡ascii ¡table ¡( ’ ’, ’0’, ’A’, ’a’ ) ¡ • limited ¡precision ¡(only ¡6-­‑9 ¡significant ¡digits) ¡ • a ¡character ¡literal ¡fetch ¡corresponding ¡integer ¡from ¡ ¡ ascii ¡table ¡(32, ¡48, ¡65, ¡97) ¡ • large ¡but ¡limited ¡range ¡ • some ¡character ¡literals ¡must ¡be ¡escaped ¡with ¡backslash ¡ • one ¡byte ¡in ¡memory ¡ float f, g, h; char c; float rounded{1.1f}; char first_letter{’A’}; char digit_zero{48}; // ’0’ char single_quote{’\’’}; 3 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend