Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... ·...

Preview:

Citation preview

269102BasicComputerProgrammingforISNE

SantiPhithakkitnukoonผศ.ดร.สนัต ิพิทกัษ์กิจนกุรู

Syllabus• Instructor:

Asst.Prof.Dr.SantiPhithakkitnukoonผศ.ดร.สนัต ิพิทกัษ์กิจนกุรู (อ.เอม็)

• Officeroom:510• Lecturetime:Wed1pm-2pm• Lectureroom:302• Labtime:Mon2pm-9pm• Requiredtextbooks:None• Suggestedtextbook:

– PaulDeitel andHarveyDeitel (2012),C++HowtoProgram(EighthEdition),PrenticeHall.

• Coursecontent:Basiccomputerprogramming(C++)

• Grading:– Midtermexam 30%– Finalexam 30%– Labassignments 20%– Quizzes 20%

WhatisComputerEngineering?

• Engineering– designingandbuildingsystems

• Computer Engineering– designingandbuildingComputer systems

• ISNE– designingandbuildingInformationandNetworksystems

• Todesignandbuildacomputersystem,weneedtobeabletoinstruct thecomputers.

WhatisaComputer?• Acomputer isadevicecapableofperformingcomputations andmakinglogicaldecisions underthecontrolofsetsofinstructionscalledcomputerprograms.

• Theseprogramsguidethecomputerthroughorderlysetsofactionswithinstructionsspecifiedbypeoplecalledcomputerprogrammers.

• Acomputeriscomprisedofvariousdevices(suchaskeyboard,screen,mouse,disks,memory,CD-ROM,andprocessingunits)thatarereferredtoashardware.

• Thecomputerprogramsthatrunonacomputerarereferredtoassoftware.

ComputerOrganization

1. Inputunit2. Outputunit3. Memoryunit4. Arithmeticandlogicunit(ALU)5. Centralprocessingunit(CPU)6. Secondarymemoryunit

ComputerOrganization

1. Inputunit– Thisisthe“receiving”sectionofthecomputer.Itobtainsinformation(dataandcomputerprograms)frominputdevicesandplacesthisinformationatthedisposaloftheotherunitsforprocessing.

–Mostinformationisenteredintocomputersthroughkeyboards,touchscreensandmousedevices.

ComputerOrganization

2.Outputunit– Thisisthe“shipping”sectionofthecomputer.Ittakesinformationthathasbeenprocessedbythecomputerandplacesitonvariousoutputdevicestomaketheinformationavailableforuseoutsidethecomputer.

–Mostinformationthat’soutputfromcomputerstodayisdisplayedonscreens,printedonpaper,playedasaudioorvideoonportablemediaplayers,andtransmittedovertheInternet.

ComputerOrganization3.Memoryunit– Theistherapidaccess,relativelylow-capacity“warehouse”sectionofthecomputer.

– Itistheareainacomputerinwhichdataisstoredforquickaccessbythecomputerprocessor(CPU)

– Thememoryunitisoftencalledeithermemory orprimarymemory.ThetermRandomAccessMemory(RAM)isalsoassociatedwithit.

– Typicalmainmemoriesondesktopandnotebookcomputerscontainbetween1GBand8GB(GBstandsforgigabytes;agigabyteisapproximatelyonebillionbytes).

ComputerOrganization

4.Arithmeticandlogicunit(ALU)– Thisisthe“manufacturing”sectionofthecomputer.Itisresponsibleforperformingcalculationssuchasaddition,subtraction,multiplication,anddivision.

– Itcontainsthedecisionmechanismsthatallowthecomputer,forexample,tocomparetwoitemsfromthememoryunittodeterminewhetherornottheyareequal.

ComputerOrganization

5.Centralprocessingunit(CPU)– Thisisthe“administrative”sectionofthecomputer.

– Itisthecomputer’scoordinatorandisresponsibleforsupervisingtheoperationoftheothersections.

ComputerOrganization6.Secondarystorageunit– Thisisthelong-term,high-capacity“warehousing”sectionofthecomputer.

– Programsordatanotactivelybeingusedbytheotherunitsarenormallyplacedonsecondarystoragedevices(suchdisks)untiltheyareagainneededlater.

– Informationinsecondarystoragetakesmuchlongertoaccessthaninformationinprimarymemory.

– ExamplesofsecondarystoragedevicesincludeCDdrives,DVDdrivesandflashdrives,someofwhichcanholdupto128GB.Typicalharddrivesondesktopandnotebookcomputerscanholdupto2TB(TBstandsforterabytes;aterabyteisapproximatelyonetrillionbytes).

ComputerLanguages

• Programmerswriteinstructionsinvariousprogramminglanguages,somedirectlyunderstandablebycomputersandothersrequiringintermediatetranslation steps.

• Hundredsofsuchlanguagesareinusetoday.Thesemaybedividedintothreegeneraltypes:1. Machinelanguages2. Assemblylanguages3. High-levellanguages

ComputerLanguages• Anycomputercandirectlyunderstandonlyitsown

machinelanguage,definedbyitshardwaredesign.• Machinelanguages(ormachinecodes)generallyconsistof

stringsofnumbers(ultimatelyreducedto1sand0s)thatinstructcomputerstoperformtheirmostelementaryoperationsoneatatime.

• Forexample,here’sasectionofanearlymachine-languageprogramthataddsovertimepay tobasepay andstorestheresultingrosspay:

+1300042774 +1400593419 +1200274027

ComputerLanguages• Programminginmachinelanguagewassimplytooslowandtedious

formostprogrammers.• Insteadofusingthemachinecodethatcomputerscoulddirectly

understand,programmersbeganusingEnglish-likeabbreviationstorepresentelementaryoperations.Theseabbreviationsformedthebasisofassemblylanguages.

• Translatorprogramscalledassemblers weredevelopedtoconvertearlyassembly-languageprogramstomachinelanguage.

• Thefollowingsectionofanassembly-languageprogramalsoaddsovertimepaytobasepayandstorestheresultingrosspay:

load basepayadd overpay store grosspay

ComputerLanguages• Althoughtheassemblylanguageisclearertohumans,it’sstill

difficulttowriteinstructionstoaccomplishaneasytask.• Tospeeduptheprogrammingprocess,high-levellanguages were

developedinwhichsinglestatementscouldbewrittentoaccomplishsubstantialtasks.

• Translatorprogramscalledcompilers converthigh-levellanguageprogramsintomachinelanguage.

• High-levellanguagesallowyoutowriteinstructionsthatlookalmostlikeeverydayEnglishandcontaincommonlyusedmathematicalnotations.

• Apayrollprogramwritteninahigh-levellanguagemightcontainasinglestatementsuchas

grossPay = basePay + overTimePay

High-levelLanguages• Fromtheprogrammer’sstandpoint,high-levellanguagesaremorepreferablethanmachineandassemblylanguages.

• C++,C,Python,PHP,andJavaareamongthemostwidelyusedhigh-levelprogramminglanguages.

ElementsofComputerProgramming

1. Editor– sourcecodeeditor

2. Compiler– aprogramthattranslates ahigh-levellanguageintomachinecode

3. Debugger– a programthatassistsinthedetectionandcorrectionoferrors incomputerprograms.

IntegratedDevelopmentEnvironment(IDE)

• Anintegrateddevelopmentenvironment(IDE)orinteractivedevelopmentenvironmentisasoftwareapplicationthatprovidescomprehensivefacilitiestocomputerprogrammersforsoftwaredevelopment.

• IDE consistsofsourcecodeeditor,compiler,anddebugger.

ASimpleProgram

ASimpleProgram

Line1:§ isapreprocessordirective– amessagetotheC++preprocessor.§ Linesthatbeginwith# areprocessedbythepreprocessorbeforetheprogram

iscompiled.§ Thislinenotifiesthepreprocessortoincludeintheprogramthecontentsof

theinput/outputstreamheader<iostream>.§ Thisheadermustbeincludedforanyprogramthatoutputsdatatothescreen

orinputsdatafromthekeyboardusingC++’sstreaminput/output.

ASimpleProgram

Line3:§ ispartofeveryC++program.§ Theparenthesesaftermain indicatethatmain isaprogrambuildingblockcalleda

function.§ C++programstypicallyconsistofoneormorefunctionsandclasses.Exactlyone

functionineveryprogrammustbenamedmain.§ Thekeywordint indicatesthatmain“returns”aninteger (wholenumber)value.§ Theleftbrace,{,(line4)mustbeginthebodyofeveryfunction.Acorrespondingright

brace,},(line8)mustendeachfunction’sbody.

ASimpleProgram

Line5:– instructsthecomputertoprintthestringofcharacterscontainedbetweenthe

doublequotationmarks(HelloWorld!).– Theentirelineiscalled“statement”.EveryC++statementmustendwitha

semicolon(;)– OutputandinputinC++areaccomplishedwithstreams ofcharacters.Thus,

whenthisstatementisexecuted,itsendsthestreamofcharacters“HelloWorld!”tothestandardoutputstreamobject std::cout—whichisnormally“connected”tothescreen.

ASimpleProgram

Line5:– Thestd:: beforecout isrequiredwhenweusenamesthatwe’vebrought

intotheprogrambythepreprocessordirective#include <iostream>.– Thenotationstd::cout specifiesthatweareusinganame,inthiscase

cout,thatbelongsto“namespace”std.– Fornow,youshouldsimplyremembertoincludestd:: beforeeachmention

ofcout andcin.

ASimpleProgram

Line7:– isoneofseveralmeanswe’llusetoexitafunction.Whenthereturn statementisusedattheendofmain,thevalue0indicatesthattheprogramhasterminatedsuccessfully.

HelloWorld!

• Runthisprogram.Modifyit.Commentit.Makeanerror.

• Canyouprintyournameononelineand“ChiangMaiUniversity”onthenextline?