资源预览内容
第1页 / 共54页
第2页 / 共54页
第3页 / 共54页
第4页 / 共54页
第5页 / 共54页
第6页 / 共54页
第7页 / 共54页
第8页 / 共54页
第9页 / 共54页
第10页 / 共54页
亲,该文档总共54页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
A First Book of ANSI CFourth EditionChapter 1Introduction to Computer ProgrammingObjectivesHistory and HardwareProgramming LanguagesAlgorithmsThe Software Development ProcessCase Study: Design and DevelopmentCommon Programming Errors2A First Book of ANSI C, Fourth EditionHistory and HardwareBabbages analytical engine (1822)Atanasoff-Berry Computer (ABC, 1937)Human operator manipulated external wiringElectrical Numerical Integrator and Computer (ENIAC, 1946)Vacuum tubesMark I (1944)Mechanical relay switchesElectronic Delayed Storage Automatic Computer (EDSAC, 1949)Incorporated a form of memory3A First Book of ANSI C, Fourth EditionHistory and Hardware (continued)4A First Book of ANSI C, Fourth EditionComputer HardwareComputers are constructed from physical components referred to as hardwareHardware facilitates the storage and processing of data under the direction of a stored programComputer hardware does not store data using the same symbols that humans do5A First Book of ANSI C, Fourth EditionBits and BytesThe smallest and most basic data item in a computer is a bitOpen or closed switch0 or 1The grouping of 8 bits to form a larger unit is referred to as a byteCan represent any one of 256 distinct patternsThe collections of patterns consisting of 0s and 1s used to represent letters, single digits, and other single characters are called character codes6A First Book of ANSI C, Fourth EditionComponents7A First Book of ANSI C, Fourth EditionMain Memory UnitStores data and instructions as sequence of bytesA program must reside in main memory if it is to operate on the computerCombines 1 or more bytes into a single unit, referred to as a wordConstructed as random access memory, or RAMEvery section of memory can be accessed randomly as quickly as any other sectionVolatile: data is lost when power is turned offSize is usually specified in bytes (MB or GB)8A First Book of ANSI C, Fourth EditionCentral Processing Unit (CPU)Control unit: directs and monitors the overall operation of the computerKeeps track of where the next instruction residesIssues the signals needed to both read data from and write data to other units in the systemExecutes all instructionsArithmetic and Logic Unit (ALU): performs all of the computations, such as addition, subtraction, comparisons, and so on, that a computer providesCPUs are constructed as a single microchip, which is referred to as a microprocessor9A First Book of ANSI C, Fourth EditionMicroprocessor10A First Book of ANSI C, Fourth EditionInput/Output UnitThe input/output (I/O) unit provides access to the computer, allowing it to input and output dataIt is the interface to which peripheral devices, such as keyboards, console screens, and printers, are attached11A First Book of ANSI C, Fourth EditionSecondary StorageUsed as permanent storage for programs and dataMagnetic tape, magnetic disks, and CD-ROMsDirect access storage device (DASD): allows a computer to read or write any one file or program independent of its position on the storage mediumMagnetic hard disk consists of rigid platters that spin together on a common spindleInitially, the most common magnetic disk storage device was the removable floppy disk12A First Book of ANSI C, Fourth EditionMagnetic Hard Disk13A First Book of ANSI C, Fourth EditionProgramming LanguagesComputer program: data and instructions used to operate a computer and produce a specific resultA program or set of programs is called softwareProgramming: writing instructions in a language that the computer can respond to and that other programmers can understandProgramming language: set of instructions that can be used to construct a program14A First Book of ANSI C, Fourth EditionMachine LanguageExecutable program: program that can operate a computerExecutable programs are written with binary numbers, which is a computers internal language (machine language)An example of a simple machine language program containing two instructions is:1100000000000000000100000000001011110000000000000010000000000011Opcode is short for operation code; tells the computer the operation to be performed15A First Book of ANSI C, Fourth EditionAssembly LanguageAssembly language: uses the substitution of word-like symbols for the opcodes, and decimal numbers and labels for memory addresses LOAD firstADD secondMUL factorSTORE answer16A First Book of ANSI C, Fourth EditionAssembly Language (continued)17A First Book of ANSI C, Fourth EditionLow- and High-Level LanguagesMachine and assembly languages are low-level languages because they both use instructions that are directly tied to one type of computerPrograms written in a computer language are referred to as source programs and source codeWhen each statement in a high-level source program is translated individually and executed immediately upon translation, the programming language is called an interpreted languageInterpreter: program that translates each statement in a high-level source program and executes it immediately upon translation18A First Book of ANSI C, Fourth EditionLow- and High-Level Languages (continued)Compiled language: the statements in a high-level source program are translated as a complete unit before any individual statement is executedCompiler: translates a high-level source program as a complete unit before any statement is executedThe output produced by the compiler is called an object program (machine language version of the source code)Linker: combines additional machine language code with the object program to create a final executable program19A First Book of ANSI C, Fourth EditionLow- and High-Level Languages (continued)20A First Book of ANSI C, Fourth EditionProcedural and Object-Oriented LanguagesProcedural language: instructions are used to create self-contained units, called proceduresProcedure: accepts data as input and transforms it in some manner to produce a specific result as outputAlso called function or methodProcedures conforming to structure guidelines are known as structured procedures21A First Book of ANSI C, Fourth EditionProcedural and Object-Oriented Languages (continued)Structured language: high-level procedural language (e.g., C) that enforces structured proceduresObject-oriented languages: languages with object orientation such as C+, Java, Visual Basic, and C#22A First Book of ANSI C, Fourth EditionProcedural and Object-Oriented Languages (continued)23A First Book of ANSI C, Fourth EditionApplication and System SoftwareApplication software: programs written to perform particular tasks required by usersSystem software: collection of programs that must be readily available to any computer system to enable the computer to operateThe bootstrap loader is internally contained in ROM and is a permanent, automatically executed component of the computers system software24A First Book of ANSI C, Fourth EditionApplication and System Software (continued)Operating system: set of system programs used to operate and control a computerMultiuser system: handles multiple users concurrentlyOperating systems that permit each user to run multiple programs are referred to as both multiprogrammed and multitasking systems25A First Book of ANSI C, Fourth EditionThe Development of CDeveloped in the 1970s at AT&T Bell Laboratories by K. Thompson, D. Ritchie, and B. KernighanHigh-level structured languageCan also access the internal hardware of a computerC permits a programmer to “see into” a computers memory and directly alter data stored in itStandard maintained by the American National Standards Institute (ANSI)In the 1980s, Bjarne Stroustrup (working at AT&T) developed C+C with object-oriented capabilities26A First Book of ANSI C, Fourth EditionAlgorithmsAlgorithm: specific steps required to produce a desired resultSet n equal to 100Set a equal to 1Set b equal to 100Calculate sum = n(a+ b)/2Display the sumWhen English phrases are used to describe an algorithm, the description is called pseudocodeInput the three numbers into the computerCalculate the average by adding the numbers and dividing the sum by threeDisplay the average27A First Book of ANSI C, Fourth EditionAlgorithms (continued)28A First Book of ANSI C, Fourth EditionAlgorithms (continued)When mathematical equations are used to describe an algorithm, the description is called a formulaFlowchart: provides a pictorial representation of an algorithm using specifically defined shapes29A First Book of ANSI C, Fourth EditionAlgorithms (continued)30A First Book of ANSI C, Fourth EditionAlgorithms (continued)31A First Book of ANSI C, Fourth EditionAlgorithms (continued)Converting an algorithm into a computer program, using a language such as C, is called coding the algorithmThe program instructions resulting from coding an algorithm are called program code, or simply code32A First Book of ANSI C, Fourth EditionAlgorithms (continued)33A First Book of ANSI C, Fourth EditionThe Software Development ProcessEach field of study has a name for the systematic method used to design solutions to problemsIn science: called the scientific methodIn engineering: called the systems approachThe technique used by professional software developers for understanding the problem that is being solved and for creating an effective and appropriate software solution is called the software development process34A First Book of ANSI C, Fourth EditionThe Software Development Process (continued)35A First Book of ANSI C, Fourth EditionPhase I: Specify the Programs RequirementsBegins with a problem statement or a specific request for a program, which is called a program requirementSuppose you receive an e-mail from your supervisor that says: We need a program to provide information about circlesThis is not a clearly defined requirementTo clarify and define the problem statement, your first step would be to contact your supervisor to define exactly what information is to be produced (its outputs) and what data is to be provided (the inputs)36A First Book of ANSI C, Fourth EditionPhase II: Design and DevelopmentStep 1: Analyze the problem. You must understand:The outputs that must be producedThe input data required to create the desired outputsThe formulas relating the inputs to the outputsStep 2: Select an overall solution algorithm37A First Book of ANSI C, Fourth EditionPhase II: Design and Development (continued)38A First Book of ANSI C, Fourth EditionPhase II: Design and Development (continued)For larger programs you will have to refine the initial algorithm and organize it into smaller algorithms, with specifications for how these smaller algorithms will interface with each otherFirst-level structure diagram for an algorithm is the first attempt at a structure for a solution algorithmTop-down algorithm development starts at the topmost level and proceeds to develop more and more detailed algorithms as it proceeds to the final set of algorithms39A First Book of ANSI C, Fourth EditionPhase II: Design and Development (continued)40A First Book of ANSI C, Fourth EditionPhase II: Design and Development (continued)41A First Book of ANSI C, Fourth EditionPhase II: Design and Development (continued)Step 3: Write the program (or code the algorithm)Sequence structure defines the order in which instructions are executed by the programSelection structure provides the capability to make a choice between different instructions, depending on the result of some conditionRepetition structure, also called looping or iteration, provides the ability for the same operation to be repeated based on the value of a conditionInvocation structure involves invoking specific sections of code as they are needed42A First Book of ANSI C, Fourth EditionPhase II: Design and Development (continued)Step 4: Test and correct the programA program error is called a bugTesting attempts to ensure that a program works correctly and produces meaningful resultsIf you find an error, initiate debugging: locating, correcting, and verifying the correctionDevelop a set of test data that determines whether the program gives correct answersThe tests should examine every possible situation under which a program will be used43A First Book of ANSI C, Fourth EditionPhase III: DocumentationSix documents for every problem solution:1.The requirements statement2.A description of the algorithms that were coded3.Comments within the code itself4.A description of modification and changes made over time5.Sample test runs, which include the inputs used for each run and the output obtained from the run6.A users manual, which is a detailed explanation of how to use the program44A First Book of ANSI C, Fourth EditionPhase IV: MaintenanceHow easily a program can be maintained (corrected, modified, or enhanced) is related to the ease with which the program can be read and understood45A First Book of ANSI C, Fourth EditionPhase IV: Maintenance (continued)46A First Book of ANSI C, Fourth EditionBackupMaking and keeping backup copies of your work when writing a program is criticalNot part of the formal software development processBackup is unimportant if you dont mind starting all over againMany organizations keep at least one backup on site where it can be easily retrieved, and another backup copy either in a fireproof safe or at a remote location47A First Book of ANSI C, Fourth EditionCase Study: Design and DevelopmentThe circumference, C, of a circle is given by the formula C = 2r, where is the constant 3.1416, and r is the radius of the circle. Using this information, write a C program to calculate the circumference of a circle that has a 2-inch radius.Step 1: Analyze the problemDetermine the desired outputsDetermine the input itemsList the formulas relating the inputs to the outputsPerform a hand calculation48A First Book of ANSI C, Fourth EditionCase Study: Design and Development (continued)Step 2: Select an overall solution algorithmSet the radius value to 2Calculate the circumference, C, using the formula C = 2 rDisplay the calculated value for CStep 3: Write the program (see next slide)Step 4: Test and correct the programThe circumference of the circle is 12.566400Because only one calculation is performed by the program, testing Program 1.1 really means verifying that the single output is correct49A First Book of ANSI C, Fourth EditionCase Study: Design and Development (continued)50A First Book of ANSI C, Fourth EditionCommon Programming ErrorsRushing to write and execute a program without spending sufficient time learning about the problem or designing an appropriate algorithmForgetting to back up a programNot understanding that computers respond only to explicitly defined algorithms51A First Book of ANSI C, Fourth EditionSummaryThe first attempt at creating a self-operating computational machine was by Charles Babbage in 1822The physical components used in constructing a computer are called hardwareThe programs used to operate a computer are referred to as softwareProgramming languages come in a variety of forms and typesCompiler and interpreter languages are referred to as high-level languages52A First Book of ANSI C, Fourth EditionSummary (continued)Algorithm: step-by-step sequence of instructions that must terminate and describes how to perform an operation to produce a desired outputThe software development procedure consists of the following four phases:Specification of the programs requirementsDesign and developmentDocumentationMaintenance53A First Book of ANSI C, Fourth EditionSummary (continued)Steps of the design and development phase are:Analyze the problemSelect an overall solution algorithmWrite the programTest and correct the programWriting a program consists of translating the solution algorithm into a computer languageFundamental programming control structuresSequence, selection, iteration and invocationYou always need at least one backup of a program54A First Book of ANSI C, Fourth Edition
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号