Download - Oct 29 CS31

Transcript
  • 7/31/2019 Oct 29 CS31

    1/4

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------stringphone;for(;;){cout

  • 7/31/2019 Oct 29 CS31

    2/4

    *****cantreturntwodoubles,soweusedreferencevariables

    intmain(){doubler;r:5doubleangle;angle:0...//getvaluesforrandangledoublex;x:5:xx//onceyoureturntothefunctionyoucantcallx...xxanymore.doubley;y:0:yypolarToCartesian(r,angle,x,y);//nowxandyhavetherihtvalues(wrongg)...doublex2;x2:10:xxpolarToCartesian(2*r,angle,x2,y);//wrong}

    rho:10theta:0xx:yy:0------->doendupwiththerightvalues,butthesefourlocalvariablesgoawaywhenyouleavethefunctionvoidpolarToCartesian(doublerho,doubletheta,doublexx,doubleyy)//shouldbevoidpolarToCartesian(doublerho,doubletheta,double&x,double&y)//canalsobedouble&xx,double&yy

    {x=rho*cos(theta);y=rho*sin(theta);}

    Whydoesntthiswork?*****double&meansreference-doubleoranother-name-for-some-double******************canbedouble&xx,akaspaceasdesired*****cannotputacomputationintoareferencevariable-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    -------------------------k:(copy)3intsquare(intk)//preferablebecauseitismoreflexible{

    returnk*k;}

    voidsquareTheArgument(int&j){j*=j;}

    intmain()

    {intn;cin>>n;n:9intm;m:81...m=square(n);m=square(m);---------->thesquarefunctiondidntchangem,theassignmentdidcout

  • 7/31/2019 Oct 29 CS31

    3/4

    ...}---------------------------------------------------------------------------------------------------------::::FORPROJECT3::::---------------------------------------------------------------------------------------------------------inttraversePath(...,...,...,...,...,int&nsteps);//goalistoimplementtraversePathliketraverseSegment---------------------------------------------------------------------------------------------------------12n3eisavalidpath....12n3e123n3enotavalidpath*****pathsegmentsare1or2digitsfollowedbythedirectionlol*****78n3e78n3e,wellformedpathbutnottraversable12nemptystringisalsoawellformedpath,wontgoanywhere----------------------------------------------------------------------------------------------------------dothefunctionexerciseswootwootdothewriteuponcharactersandstringswootwoot-----------------------------------------------------------------------------------------------------------1n3eisnotapathsegment

    @1ne3eisnotapathsegment----------------------------------------------------------------------------------------------------------::::TABLESMESASMESAS::::----------------------------------------------------------------------------------------------------------

    boolisValidDate(inty,intm,intd){intdaysInMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};

    ...if(m12||dLEAVEEE...opposedtobreak,whichbreaksaloop/swi

    tchstatementandcontinuewithrestoffunctionif(m==2){....return...;}returnd

  • 7/31/2019 Oct 29 CS31

    4/4


Top Related