2969208 Introduction to Computer Programming

download 2969208 Introduction to Computer Programming

of 53

Transcript of 2969208 Introduction to Computer Programming

  • 7/31/2019 2969208 Introduction to Computer Programming

    1/53

    1

    Introduction

    IntroductiontoProgramming1

    1

  • 7/31/2019 2969208 Introduction to Computer Programming

    2/53

    ObjectivesAttheendofthelesson,thestudentshouldbeableto:

    IdentifythedifferentcomponentsofacomputerKnowaboutprogramminglanguagesandtheircategoriesUnderstandtheprogramdevelopmentlifecycleandapplyitinproblemsolvingLearnthedifferentnumbersystemsandtheirconversions

    IntroductiontoProgramming1

    2

  • 7/31/2019 2969208 Introduction to Computer Programming

    3/53

    Introduction

    Computer

    amachinethatperformsavarietyoftasksaccordingtospecificinstructions

    Twomajorcomponents:

    Hardware

    tangiblepartofthecomputercomposedofelectronicandmechanicalpartsintangiblepartofacomputerconsistsofdataandthecomputerprograms

    Software

    IntroductiontoProgramming1

    3

  • 7/31/2019 2969208 Introduction to Computer Programming

    4/53

    BasicComponentsofaComputer:Hardware

    CPU

    CentralprocessingunitTheprocessoristhebrainofthecomputerItdoesthefundamentalcomputingwithinthesystemExamples:Pentium,AthlonandSPARC.

    IntroductiontoProgramming1

    4

  • 7/31/2019 2969208 Introduction to Computer Programming

    5/53

    BasicComponentsofaComputer:Hardware

    Memory

    wheredataandinstructionsneededbytheCPUtodoitsappointedtaskscanbefound2Types:

    MainMemorySecondaryMemory

    IntroductiontoProgramming1

    5

  • 7/31/2019 2969208 Introduction to Computer Programming

    6/53

    BasicComponentsofaComputer:Hardware

    MainMemory

    usedtoholdprogramsanddata,thattheprocessorisactivelyworkingwith.notusedforlong-termstoragesometimescalledtheRAM(RandomAccessMemory).consideredasvolatilestoragemeansoncethecomputeristurnedoff,allinformationresidinginthemainmemoryiserased

    IntroductiontoProgramming1

    6

  • 7/31/2019 2969208 Introduction to Computer Programming

    7/53

    BasicComponentsofaComputer:Hardware

    SecondaryMemory

    usedtoholdprogramsanddataforlongtermuse.Examplesofsecondarymemoryareharddisksandcd-rom.consideredasnon-volatilestorage

    IntroductiontoProgramming1

    7

  • 7/31/2019 2969208 Introduction to Computer Programming

    8/53

    BasicComponentsofaComputer:Hardware

    Comparisonbetweenmainmemoryandsecondarymemory

    IntroductiontoProgramming1

    8

  • 7/31/2019 2969208 Introduction to Computer Programming

    9/53

    BasicComponentsofaComputer:Hardware

    InputandOutputDevices

    allowsacomputersystemtointeractwiththeoutsideworldbymovingdataintoandoutofthesystemExamples:

    inputdevices:keyboards,miceandmicrophonesoutputdevices:monitors,printersandspeakers

    IntroductiontoProgramming1

    9

  • 7/31/2019 2969208 Introduction to Computer Programming

    10/53

    BasicComponentsofaComputer:Software

    Software

    aprogramthatacomputerusesinordertofunctionkeptonsomehardwaredevicelikeaharddisk,butititselfisintangibledatathatthecomputerusescanbeanythingthataprogramneeds

    Programs

    actlikeinstructionsfortheprocessor.

    IntroductiontoProgramming1

    10

  • 7/31/2019 2969208 Introduction to Computer Programming

    11/53

    BasicComponentsofaComputer:Software

    SomeTypesofComputerPrograms

    SystemsProgramsApplicationProgramsCompilers

    IntroductiontoProgramming1

    11

  • 7/31/2019 2969208 Introduction to Computer Programming

    12/53

    BasicComponentsofaComputer:Software

    SystemsPrograms

    ProgramsthatareneededtokeepallthehardwareandsoftwaresystemsrunningtogethersmoothlyExamples:OperatingSystemslikeLinux,Windows,Unix,Solaris,MacOS

    IntroductiontoProgramming1

    12

  • 7/31/2019 2969208 Introduction to Computer Programming

    13/53

    BasicComponentsofaComputer:Software

    ApplicationPrograms

    ProgramsthatpeopleusetogettheirworkdoneExamples:WordProcessor,Gameprograms,Spreadsheets

    IntroductiontoProgramming1

    13

  • 7/31/2019 2969208 Introduction to Computer Programming

    14/53

    BasicComponentsofaComputer:Software

    Compilers

    TranslatescomputerprogramstomachinelanguageMachinelanguage

    Languagethatyourcomputerunderstands.

    IntroductiontoProgramming1

    14

  • 7/31/2019 2969208 Introduction to Computer Programming

    15/53

    ProgrammingLanguages

    ProgrammingLanguage

    astandardizedcommunicationtechniqueforexpressinginstructionstoacomputerLikehumanlanguages,eachlanguagehasitsownsyntaxandgrammarTherearedifferenttypesofprogramminglanguagesthatcanbeusedtocreateprograms,butregardlessofwhatlanguageyouuse,theseinstructionsaretranslatedintomachinelanguagethatcanbeunderstoodbycomputers.

    IntroductiontoProgramming1

    15

  • 7/31/2019 2969208 Introduction to Computer Programming

    16/53

    CategoriesofProgrammingLanguages

    High-levelProgrammingLanguages

    aprogramminglanguagethatismoreuser-friendly,tosomeextentplatform-independent,andabstractfromlow-levelcomputerprocessoroperationssuchasmemoryaccessesAprogrammingstatementmaybetranslatedintooneorseveralmachineinstructionsbyacompiler.Examples:Java,C,C++,Basic,Fortran

    IntroductiontoProgramming1

    16

  • 7/31/2019 2969208 Introduction to Computer Programming

    17/53

    CategoriesofProgrammingLanguages

    Low-levelAssemblyLanguage

    Assemblylanguagesaresimilartomachinelanguages,buttheyaremucheasiertoprogrambecausetheyallowaprogrammertosubstitutenamesfornumbersAssemblylanguagesareavailableforeachCPUfamily,andeachassemblyinstructionistranslatedintoonemachineinstructionbyanassemblerprogram

    IntroductiontoProgramming1

    17

  • 7/31/2019 2969208 Introduction to Computer Programming

    18/53

    CategoriesofProgrammingLanguages

    NOTE:

    Theterms"high-level"and"low-level"areinherentlyrelative.Originally,assemblylanguagewasconsideredlow-levelandCOBOL,C,etc.wereconsideredhigh-level.Manyprogrammerstodaymightrefertotheselatterlanguagesaslow-level

    IntroductiontoProgramming1

    18

  • 7/31/2019 2969208 Introduction to Computer Programming

    19/53

    ProgramDevelopmentLifeCycle

    Basicstepsintryingtosolveaproblemonthecomputer:1.ProblemDefinition2.ProblemAnalysis3.Algorithmdesignandrepresentation(Pseudocodeorflowchart)4.Codinganddebugging

    IntroductiontoProgramming1

    19

  • 7/31/2019 2969208 Introduction to Computer Programming

    20/53

    1.ProblemDefinition

    Aclearlydefinedproblemisalreadyhalfthesolution.Computerprogrammingrequiresustodefinetheproblemfirstbeforeweeventrytocreateasolution.Letusnowdefineourexampleproblem:Createaprogramthatwilldeterminethenumberoftimesanameoccursinalist.

    IntroductiontoProgramming1

    20

  • 7/31/2019 2969208 Introduction to Computer Programming

    21/53

    2.ProblemAnalysis

    Aftertheproblemhasbeenadequatelydefined,thesimplestandyetthemostefficientandeffectiveapproachtosolvetheproblemmustbeformulated.Usually,thisstepinvolvesbreakinguptheproblemintosmallerandsimplersubproblems.ExampleProblem:

    Determinethenumberoftimesanameoccursinalistlistofnames(let'scallthisnameList)nametolookfor(let'scallthiskeyName)thenumberoftimesthenameoccursinalistIntroductiontoProgramming121

    Inputtotheprogram:

    Outputoftheprogram:

  • 7/31/2019 2969208 Introduction to Computer Programming

    22/53

    3.AlgorithmDesignandrepresentation

    Algorithm

    aclearandunambiguousspecificationofthestepsneededtosolveaproblem.Itmaybeexpressedineither:

    Humanlanguage(English,Tagalog)GraphicalrepresentationlikeaflowchartPseudocode-whichisacrossbetweenhumanlanguageandaprogramminglanguage

    IntroductiontoProgramming1

    22

  • 7/31/2019 2969208 Introduction to Computer Programming

    23/53

    3.AlgorithmDesign&representationHumanLang.

    ExpressingoursolutionthroughHumanlanguage:1.Getthelistofnames,let'scallthisnameList2.Getthenametolookfor,let'scallthisthekeyname3.ComparethekeynametoeachofthenamesinnameList4.Ifthekeynameisthesamewithanameinthelist,add1tothecount5.Ifallthenameshavebeencompared,outputtheresult

    IntroductiontoProgramming1

    23

  • 7/31/2019 2969208 Introduction to Computer Programming

    24/53

    3.AlgorithmDesignandrepresentation-Flowchart

    Expressingoursolutionthroughaflowchart:

    IntroductiontoProgramming1

    24

  • 7/31/2019 2969208 Introduction to Computer Programming

    25/53

    FlowchartSymbols

    IntroductiontoProgramming1

    25

  • 7/31/2019 2969208 Introduction to Computer Programming

    26/53

    FlowchartSymbols

    IntroductiontoProgramming1

    26

  • 7/31/2019 2969208 Introduction to Computer Programming

    27/53

    FlowchartSymbols

    NOTE:

    Thesearejustguidelinesforcommonlyusedsymbolsincreatingflowcharts.Youcanuseanysymbolsincreatingyourflowcharts,aslongasyouareconsistentinusingthem

    IntroductiontoProgramming1

    27

  • 7/31/2019 2969208 Introduction to Computer Programming

    28/53

    3.AlgorithmDesignandrepresentation-Pseudocode

    Expressingoursolutionthroughpseudocode:LetnameList=ListofNamesLetkeyName=thenametobesoughtLetCount=0ForeachnameinNameListdothefollowingifname==keyNameCount=Count+1

    DisplayCount

    IntroductiontoProgramming1

    28

  • 7/31/2019 2969208 Introduction to Computer Programming

    29/53

    4.CodingandDebugging

    Afterconstructingthealgorithm,itisnowpossibletocreatethesourcecode.Usingthealgorithmasbasis,thesourcecodecannowbewrittenusingthechosenprogramminglanguage.Debugging

    Theprocessoffixingsomeerrors(bugs)inyourprogram

    IntroductiontoProgramming1

    29

  • 7/31/2019 2969208 Introduction to Computer Programming

    30/53

    TypesofErrors

    Compile-timeerrororsyntaxerrors

    occurifthereisasyntaxerrorinthecode.Thecompilerwilldetecttheerrorandtheprogramwon'tevencompile.Atthispoint,theprogrammerisunabletoformanexecutableprogramthatausercanrununtiltheerrorisfixed.

    RuntimeErrors

    Compilersaren'tperfectandsocan'tcatchallerrorsatcompiletime.Thisisespeciallytrueforlogicerrorssuchasinfiniteloops.Thistypeoferroriscalledruntimeerror.

    IntroductiontoProgramming1

    30

  • 7/31/2019 2969208 Introduction to Computer Programming

    31/53

    NumberSystems

    Numberscanberepresentedinavarietyofways.TherepresentationdependsonwhatiscalledtheBASE.Youwritethesenumbersas:Numberbase

    IntroductiontoProgramming1

    31

  • 7/31/2019 2969208 Introduction to Computer Programming

    32/53

    NumberSystems

    Thefollowingarethefourmostcommonrepresentations.

    Decimal(base10)

    CommonlyusedValiddigitsarefrom0to9Example:12610(normallywrittenasjust126)Validdigitsare0and1Example:11111102

    Binary(base2)

    IntroductiontoProgramming1

    32

  • 7/31/2019 2969208 Introduction to Computer Programming

    33/53

    NumberSystems

    Thefollowingarethefourmostcommonrepresentations.(continuation)

    Octal(base8)

    Validdigitsarefrom0to7Example:1768Validdigitsarefrom0to9andAtoF(orfromatof)Example:7E16

    Hexadecimal(base16)

    IntroductiontoProgramming1

    33

  • 7/31/2019 2969208 Introduction to Computer Programming

    34/53

    NumberSystems

    Examples:

    IntroductiontoProgramming1

    34

  • 7/31/2019 2969208 Introduction to Computer Programming

    35/53

    Conversion:DecimaltoBinary

    Method:

    continuouslydividethenumberby2gettheremainder(whichiseither0or1)getthatnumberasadigitofthebinaryformofthenumbergetthequotientanddividethatnumberagainby2repeatthewholeprocessuntilthequotientreaches0or1wethengetalltheremaindersstartingfromthelastremainder,andtheresultisthebinaryformofthenumberNOTE:Forthelastdigitwhichisalreadylessthanthedivisor(whichis2)justcopythevaluetotheremainderportion.

    IntroductiontoProgramming1

    35

  • 7/31/2019 2969208 Introduction to Computer Programming

    36/53

    Example:DecimaltoBinary

    IntroductiontoProgramming1

    36

  • 7/31/2019 2969208 Introduction to Computer Programming

    37/53

    Conversion:BinarytoDecimal

    Method:

    wemultiplythebinarydigitto"2raisedtothepositionofthebinarynumber"Wethenaddalltheproductstogettheresultingdecimalnumber.

    IntroductiontoProgramming1

    37

  • 7/31/2019 2969208 Introduction to Computer Programming

    38/53

    Example:BinarytoDecimal

    IntroductiontoProgramming1

    38

  • 7/31/2019 2969208 Introduction to Computer Programming

    39/53

    Conversion:DecimaltoOctal/Hexadecimal

    Method:

    ConvertingdecimalnumberstoOctalorhexadecimalisbasicallythesameasconvertingdecimaltobinary.However,insteadofhaving2asthedivisor,youreplaceitwith8(foroctal)or16(forhexadecimal)

    IntroductiontoProgramming1

    39

  • 7/31/2019 2969208 Introduction to Computer Programming

    40/53

    Example:DecimaltoOctal/Hexadecimal

    IntroductiontoProgramming1

    40

  • 7/31/2019 2969208 Introduction to Computer Programming

    41/53

    Conversion:Octal/HexadecimaltoDecimal

    Method:

    Convertingoctalorhexadecimalnumbersisalsothesameasconvertingbinarynumberstodecimal.Todothat,wewilljustreplacethebasenumber2with8forOctaland16forhexadecimal.

    IntroductiontoProgramming1

    41

  • 7/31/2019 2969208 Introduction to Computer Programming

    42/53

    Example:Octal/HexadecimaltoDecimal

    IntroductiontoProgramming1

    42

  • 7/31/2019 2969208 Introduction to Computer Programming

    43/53

    Conversion:BinarytoOctal

    Method:

    partitionthebinarynumberintogroupsof3digits(fromrighttoleft)paditwithzerosifthenumberofdigitsisnotdivisibleby3converteachpartitionintoitscorrespondingoctaldigitThefollowingisatableshowingthebinaryrepresentationofeachoctaldigit.

    IntroductiontoProgramming1

    43

  • 7/31/2019 2969208 Introduction to Computer Programming

    44/53

    Example:BinarytoOctal

    IntroductiontoProgramming1

    44

  • 7/31/2019 2969208 Introduction to Computer Programming

    45/53

    Conversion:OctaltoBinary

    Method:

    Convertingoctalnumberstobinaryisjusttheoppositeofwhatisgivenpreviously.Simplyconverteachoctaldigitintoitsbinaryrepresentation(giventhetable)andconcatenatethem.Theresultisthebinaryrepresentation.

    IntroductiontoProgramming1

    45

  • 7/31/2019 2969208 Introduction to Computer Programming

    46/53

    Example:OctaltoBinary

    IntroductiontoProgramming1

    46

  • 7/31/2019 2969208 Introduction to Computer Programming

    47/53

    Conversion:BinarytoHexadecimal

    Method:

    partitionthebinarynumberintogroupsof4digits(fromrighttoleft)paditwithzerosifthenumberofdigitsisnotdivisibleby4converteachpartitionintoitscorrespondinghexadecimaldigitThefollowingisatableshowingthebinaryrepresentationofeachhexadecimaldigit

    IntroductiontoProgramming1

    47

  • 7/31/2019 2969208 Introduction to Computer Programming

    48/53

    Example:BinarytoHexadecimal

    IntroductiontoProgramming1

    48

  • 7/31/2019 2969208 Introduction to Computer Programming

    49/53

    Conversion:HexadecimaltoBinary

    Method:

    Convertinghexadecimalnumberstobinaryisjusttheoppositeofwhatisgivenpreviously.Simplyconverteachhexadecimaldigitintoitsbinaryrepresentation(giventhetable)andconcatenatethem.Theresultisthebinaryrepresentation.

    IntroductiontoProgramming1

    49

  • 7/31/2019 2969208 Introduction to Computer Programming

    50/53

    Example:HexadecimaltoBinary

    IntroductiontoProgramming1

    50

  • 7/31/2019 2969208 Introduction to Computer Programming

    51/53

    Summary

    BasicComponentsofaComputer

    HardwareSoftwareWhatisaProgrammingLanguage?CategoriesofProgrammingLanguages1.ProblemDefinition2.ProblemAnalysis3.AlgorithmDesignandrepresentation(humanLang.,Flowchart,Pseudocode)4.CodingandDebuggingIntroductiontoProgramming151

    OverviewofComputerProgrammingLanguages

    ProgramDevelopmentLifeCycle

  • 7/31/2019 2969208 Introduction to Computer Programming

    52/53

    Summary

    TypesofErrors

    Compiletimeerrors/syntaxerrorsRuntimeerrorsDecimal,Hexadecimal,Binary,OctalConversions

    NumberSystems

    IntroductiontoProgramming1

    52

  • 7/31/2019 2969208 Introduction to Computer Programming

    53/53