25
269102 Basic Computer Programming for ISNE Santi Phithakkitnukoon ผศ.ดร.สันติ พิทักษ์กิจนุกูร

Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

269102BasicComputerProgrammingforISNE

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

Page 2: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

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%

Page 3: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

WhatisComputerEngineering?

• Engineering– designingandbuildingsystems

• Computer Engineering– designingandbuildingComputer systems

• ISNE– designingandbuildingInformationandNetworksystems

• Todesignandbuildacomputersystem,weneedtobeabletoinstruct thecomputers.

Page 4: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

WhatisaComputer?• Acomputer isadevicecapableofperformingcomputations andmakinglogicaldecisions underthecontrolofsetsofinstructionscalledcomputerprograms.

• Theseprogramsguidethecomputerthroughorderlysetsofactionswithinstructionsspecifiedbypeoplecalledcomputerprogrammers.

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

• Thecomputerprogramsthatrunonacomputerarereferredtoassoftware.

Page 5: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

ComputerOrganization

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

Page 6: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

ComputerOrganization

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

–Mostinformationisenteredintocomputersthroughkeyboards,touchscreensandmousedevices.

Page 7: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

ComputerOrganization

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

–Mostinformationthat’soutputfromcomputerstodayisdisplayedonscreens,printedonpaper,playedasaudioorvideoonportablemediaplayers,andtransmittedovertheInternet.

Page 8: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

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

– Itistheareainacomputerinwhichdataisstoredforquickaccessbythecomputerprocessor(CPU)

– Thememoryunitisoftencalledeithermemory orprimarymemory.ThetermRandomAccessMemory(RAM)isalsoassociatedwithit.

– Typicalmainmemoriesondesktopandnotebookcomputerscontainbetween1GBand8GB(GBstandsforgigabytes;agigabyteisapproximatelyonebillionbytes).

Page 9: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

ComputerOrganization

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

– Itcontainsthedecisionmechanismsthatallowthecomputer,forexample,tocomparetwoitemsfromthememoryunittodeterminewhetherornottheyareequal.

Page 10: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

ComputerOrganization

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

– Itisthecomputer’scoordinatorandisresponsibleforsupervisingtheoperationoftheothersections.

Page 11: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

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

– Programsordatanotactivelybeingusedbytheotherunitsarenormallyplacedonsecondarystoragedevices(suchdisks)untiltheyareagainneededlater.

– Informationinsecondarystoragetakesmuchlongertoaccessthaninformationinprimarymemory.

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

Page 12: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

ComputerLanguages

• Programmerswriteinstructionsinvariousprogramminglanguages,somedirectlyunderstandablebycomputersandothersrequiringintermediatetranslation steps.

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

Page 13: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

ComputerLanguages• Anycomputercandirectlyunderstandonlyitsown

machinelanguage,definedbyitshardwaredesign.• Machinelanguages(ormachinecodes)generallyconsistof

stringsofnumbers(ultimatelyreducedto1sand0s)thatinstructcomputerstoperformtheirmostelementaryoperationsoneatatime.

• Forexample,here’sasectionofanearlymachine-languageprogramthataddsovertimepay tobasepay andstorestheresultingrosspay:

+1300042774 +1400593419 +1200274027

Page 14: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

ComputerLanguages• Programminginmachinelanguagewassimplytooslowandtedious

formostprogrammers.• Insteadofusingthemachinecodethatcomputerscoulddirectly

understand,programmersbeganusingEnglish-likeabbreviationstorepresentelementaryoperations.Theseabbreviationsformedthebasisofassemblylanguages.

• Translatorprogramscalledassemblers weredevelopedtoconvertearlyassembly-languageprogramstomachinelanguage.

• Thefollowingsectionofanassembly-languageprogramalsoaddsovertimepaytobasepayandstorestheresultingrosspay:

load basepayadd overpay store grosspay

Page 15: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

ComputerLanguages• Althoughtheassemblylanguageisclearertohumans,it’sstill

difficulttowriteinstructionstoaccomplishaneasytask.• Tospeeduptheprogrammingprocess,high-levellanguages were

developedinwhichsinglestatementscouldbewrittentoaccomplishsubstantialtasks.

• Translatorprogramscalledcompilers converthigh-levellanguageprogramsintomachinelanguage.

• High-levellanguagesallowyoutowriteinstructionsthatlookalmostlikeeverydayEnglishandcontaincommonlyusedmathematicalnotations.

• Apayrollprogramwritteninahigh-levellanguagemightcontainasinglestatementsuchas

grossPay = basePay + overTimePay

Page 16: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

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

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

Page 17: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

ElementsofComputerProgramming

1. Editor– sourcecodeeditor

2. Compiler– aprogramthattranslates ahigh-levellanguageintomachinecode

3. Debugger– a programthatassistsinthedetectionandcorrectionoferrors incomputerprograms.

Page 18: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

IntegratedDevelopmentEnvironment(IDE)

• Anintegrateddevelopmentenvironment(IDE)orinteractivedevelopmentenvironmentisasoftwareapplicationthatprovidescomprehensivefacilitiestocomputerprogrammersforsoftwaredevelopment.

• IDE consistsofsourcecodeeditor,compiler,anddebugger.

Page 19: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

ASimpleProgram

Page 20: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

ASimpleProgram

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

iscompiled.§ Thislinenotifiesthepreprocessortoincludeintheprogramthecontentsof

theinput/outputstreamheader<iostream>.§ Thisheadermustbeincludedforanyprogramthatoutputsdatatothescreen

orinputsdatafromthekeyboardusingC++’sstreaminput/output.

Page 21: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

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.

Page 22: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

ASimpleProgram

Line5:– instructsthecomputertoprintthestringofcharacterscontainedbetweenthe

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

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

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

Page 23: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

ASimpleProgram

Line5:– Thestd:: beforecout isrequiredwhenweusenamesthatwe’vebrought

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

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

ofcout andcin.

Page 24: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

ASimpleProgram

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

Page 25: Lect1 Intro Computers n Cpp new - Chiang Mai Universitysanti/cpe102_files/Lect1... · 2018-08-07 · 6. Secondary storage unit – This is the long-term, high-capacity “warehousing”

HelloWorld!

• Runthisprogram.Modifyit.Commentit.Makeanerror.

• Canyouprintyournameononelineand“ChiangMaiUniversity”onthenextline?