资源描述:
《arrays - penn state brandywine engineering阵列-宾州白兰地工程》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、Chapter7:ArraysThevariablesusedsofarhaveallhadacommoncharacteristic:Eachvariablecouldonlybeusedtostoreonevalueatatime.Frequently,wemayhaveasetofvalues,allofthesamedatatype,thatformalogicalgroup.Asimplelistcontainingindividualitemsofthesamescalardatatypeiscalledaone-di
2、mensionalarray.Inthischapter,wewillstudyhowone-dimensionalarraysaredeclared,initialized,storedinsideacomputer,andused.7.1One-DimensionalArraysAone-dimensionalarray(oralist,oravector)isagroupofrelatedvalueswiththesamedatatypethatisstoredusingagroupname.Thegroupnameisre
3、ferredtoasthearrayname.Theitemsinthelistcanbedeclaredasasingleunitandstoredunderacommonvariablenamecalledthearrayname.Forexample,supposethatwewouldliketostorefiveexamgradesinanarray,namedgrade,thedeclarationstatementwillbe:intgrade[5];Otherexamples:charcode[4];//anarr
4、ayoffourcharactervaluesfloatprices[6];doubleamount[100];Eachelementinanarrayiscalledanelementorcomponentofthearray.35Chapter7:ArraysTheindividualelementsstoredinthearrayarestoredsequentially,withthefirstarrayelementstoredinthefirstreservedlocation,thesecondelementstor
5、edinthesecondreservedlocation,andsoon.Toaccessindividualelementsinanarray,youusethenameofthearrayandtheelement'sposition.Thispositioniscalledtheelement'ssubscriptorindexvalue.Forasingle-dimensionedarraythefirstelementhasasubscriptof0.Theindicesofanarrayaresaidtobezero
6、relative.Forexample:intgrade[5];grade[0]referstothefirstgradestoredinthegradearray.grade[1]referstothesecondgradestoredinthegradearray.grade[0]grade[1]grade[2]grade[3]grade[4]anintegeranintegeranintegeranintegeranintegerelement1element2element3element4element5Internal
7、ly,thecomputerusestheindexasanoffsetfromthearray'sstartingposition.Theindextellsthecomputerhowmany35Chapter7:Arrayselementstoskipover,startingfromthebeginningofthearray,togettothedesiredelement.35Chapter7:ArraysIn-ClassExercise:1.Writearraydeclarationsforthefollowing:
8、a.alistof100floating-pointvoltagesb.alistof30characters,eachrepresentingacodec.alistof100integersUsingSubscriptedVariablesSu