Beruflich Dokumente
Kultur Dokumente
1. Codedebugging
a. Downloadandunpackthesourcecodeforthistutorial.
b. Openthefilecalculator.cppandlocatethe10bugsfoundinthecode.
Notedownalltheproblemsandeditthecodetothebugs.
2. Parallelplatecapacitor:Aparallelplatecapacitorhassquareplatesoflateral
dimensions25.4cmonaside,aplateseparationof0.16mmandavacuum
betweentheplates.
a. Writeaprogramtodeterminethecapacitance.
b. Extendtheprogramtocalculatethechargeonthecapacitorifthe
potentialdifferenceacrosstheplatesis50V.
c. Generalisetheprogramtocalculatethecapacitanceandchargeofa
parallelplatecapacitorwhoselength,width,plateseparationand
potentialdifferenceareenteredbytheuserofyourprogram.
3. Probabilityoftossingacoin.
a. Writeaprogramtocalculatetheprobabilityoftossing8headsfrom10
throwsofacoin.
b. Generalise theprogram to allowtheusertofindtheprobability of
tossingiheadsfromjthrows,whereiandjaredeterminedbytheuser.
4. Fibonaccisequence.Theprogramfibonacci.cppattemptstodisplaythefirst10
numbersintheFibonaccisequence.
a. Remove the 4 bugs in the program and confirm that the first 10
numbersinthesequenceare1,1,2,3,5,8,13,21,34,55.
b. Improvethecodebychangingthevariablenames,applyingsuitable
whitespaceandaddingcomments.
c. Addafewstatementstotheprogramtodisplaythesumtotalofthe
first8numbersinthesequence.
d. Modify the program to accept an input value that will decide the
numberofvaluesinthesequence.Confirmthatthesumtotalofthe
sequenceofthefirst20Fibonaccinumbersis17710.
e. Runtheprogramtocalculatethesumtotalforthefirst40Fibonacci
numbers.Confirmthecorrectanswerof267914295.
Notes:
Theemacseditorwillautomaticallyprovideyouwithahelpfulcolourand
indentationschemeifyouopenyoursourcecodefilewiththe.cppsuffix.
Tounpackthesourcecodecontainedinthefiletutorial1.tar.gztypetarxvfz
tutorial1.tar.gz atthecommandprompt.Thesourcecodeisfoundinthe
newlycreateddirectorytutorial1/
HintsforQuestion2:
Beforeyoustartprogrammingyouneedtofindtheequationsforcapacitance.
Becarefulwithhowyoupresenttheprecisionoftheresult.
HintsforQuestion3:
Findtheappropriateprobabilitydistributiontouse.
You will have to calculate a factorial. Try and use a for loop in this
calculation,
especiallyinthegeneralcase.
HintsforQuestion4:
AnumberintheFibonaccisequenceiscalculatedbyFn=Fn1+Fn2.
Theamountofcommentsandthestyleofvariablenamesiscompletelyupto
you,thereisnowronganswer.
For(d),considerthefactthateverynumberinthesequencedoesnothaveto
be stored in anarray for the program to work. Is there afixed minimum
numberofsequencevaluesyoucanstorethatwillallowyoutodefinethesize
ofthesequenceonlywhentheprogramisrun?
For(e),considerthetypedeclarationsforthevariablesinthecode.