9780538466523_im_ch01

9
An Introduction to Programming with C++, Sixth Edition 1-1 Chapter 1 An Introduction to Programming At a Glance Instructor’s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Discussion Topics Additional Projects Additional Resources Key Terms

Upload: kelly-brown

Post on 10-Oct-2014

30 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 9780538466523_IM_ch01

An Introduction to Programming with C++, Sixth Edition 1-1

Chapter 1 An Introduction to Programming At a Glance Instructor’s Manual Table of Contents • Overview • Objectives • Teaching Tips • Quick Quizzes • Discussion Topics • Additional Projects

• Additional Resources • Key Terms

Page 2: 9780538466523_IM_ch01

An Introduction to Programming with C++, Sixth Edition 1-2

Lecture Notes Overview

This chapter begins with a brief history of programming languages. The chapter then covers the three basic control structures common to most programming languages. These are the sequence, selection, and repetition structures. Subsequent chapters cover the ways these control structures are implemented in C++. However, it is imperative that students understand how and why each control structure is used.

Objectives After completing the chapter, the student will be able to:

• Define the terminology used in programming • Explain the tasks performed by a programmer • Describe the qualities of a good programmer • Understand the employment opportunities for programmers and software engineers • Explain the history of programming languages • Explain the sequence, selection, and repetition structures • Write simple algorithms using the sequence, selection, and repetition structures

Teaching Tips Programming a Computer

1. Programming essentially means giving a mechanism the directions to accomplish a task.

2. The directions given to a computer are called computer programs, or just programs. People who write programs are called programmers.

3. Programmers use programming languages to communicate with the computer.

The Programmer’s Job

1. A programmer’s job is to design a computer solution to a user’s problem and implement that solution as a computer program.

2. The programmer first meets with the user and gets a clear understanding of the problem

that must be solved and what an acceptable solution will be. There may be more than one programmer for a given project.

Page 3: 9780538466523_IM_ch01

An Introduction to Programming with C++, Sixth Edition 1-3

3. The programmer then converts the solution to a computer program, meeting with the user during the process to ensure that the program fulfills the user’s needs and to refine any relevant details.

4. When the solution is satisfactory, the programmer then rigorously tests the program with

sample data before releasing it to the user.

5. The creation of a good program thus requires a great deal of interaction between the programmer and user.

Do I Have What It Takes to Be a Programmer?

1. According to the 2008-09 Edition of the Occupational Outlook Handbook (OOH), published by the U.S. Department of Labor’s Bureau of Labor Statistics, companies look for programmers that:

a. Think logically and pay close attention to detail b. Exhibit patience, persistence, and the ability to work on exacting analytical work c. Show ingenuity and creativity when solving problems and testing solutions d. Communicate well with technical and nontechnical personnel e. Possess good business skills (for managerial positions)

Employment Opportunities

1. There are generally two types of programming positions: computer software engineers and computer programmers.

2. Computer software engineers are typically responsible for designing an appropriate solution to a user’s problem, whereas computer programmers are responsible for translating the solution into a language the computer can understand. The translation process is called coding.

3. Sometimes both the design and coding process will be carried out by the same person.

4. Typically, computer software engineers are expected to have at least a bachelor’s degree in computer engineering or computer science, along with practical work experience.

5. Computer programmers usually need at least an associate’s degree in computer science, mathematics, or information systems, as well as proficiency in one or more programming languages.

6. According to the May 2008 Occupational Employment Statistics, programmers held about 394,230 jobs, with a mean annual wage of $73,470. Computer software engineers held about 494,160 jobs, with a mean annual wage of $87,900.

Page 4: 9780538466523_IM_ch01

An Introduction to Programming with C++, Sixth Edition 1-4

Teaching Tip

Students may be interested in looking at information about computer programming and computer software engineering jobs, which can be found on the Bureau of Labor Statistics Web site at www.bls.gov/.

A Brief History of Programming Languages

1. There are many different types of programming languages. 2. This text will discuss machine languages, assembly languages, high-level procedure-

oriented languages, and high-level object-oriented languages. Machine Languages

1. Machine language, or machine code, is a programming language that uses bits (0s and 1s) to form instructions to communicate with a computer.

2. Each computer has its own instruction set (set of instructions it understands). Machine language uses binary, which is a number system using 1s and 0s to represent data (base two).

3. Because of all the 1s and 0s, programming in machine language is very tedious and prone to committing errors.

Assembly Languages

1. Assembly language is a language using mnemonics in place of 1s and 0s. Mnemonics are symbols used to represent the actual machine language instructions.

2. Since the only instructions that the computer understands are machine language

instructions, an assembler is required to convert the assembly language code to machine code before being executed by the computer.

3. Note that each assembly language instruction is translated into one machine language

instruction. 4. It is easy to see that programming in assembly language is preferred to programming in

machine language; however, programming in assembly language can also be very tedious.

Page 5: 9780538466523_IM_ch01

An Introduction to Programming with C++, Sixth Edition 1-5

High-Level Languages

1. High-level languages allow the programmer to use instructions that more closely resemble the English language.

2. Still, the only instructions that the computer can understand are machine language instructions. Therefore, a compiler is required to convert the high-level code to machine code before the instructions can be executed by the computer.

3. A compiler translates all of a program’s high-level instructions before running the program; an interpreter translates the instructions line by line as the program is running.

4. Programs written in high-level languages can be either procedure-oriented or object-

oriented.

5. Procedure-oriented programs focus on the major tasks that the program needs to perform. For example, a procedure-oriented payroll program might focus on the tasks of calculating gross pay, net pay, taxes, etc. Examples of procedure-oriented languages are COBOL, BASIC, and C.

6. Object-oriented programs focus on the objects that the program can use to accomplish

its goal. For example, an object-oriented payroll program may focus on objects like time cards, employees, and checks. Examples of object-oriented languages are C++, Visual Basic, Java, and C#.

Teaching Tip

Objects written in object-oriented programming languages can be used for more than one purpose and in more than one program. This is called code-reuse, and it saves programming time and money.

Quick Quiz 1

1. __________ write computer programs. Answer: Programmers

2. C++, Java, Visual Basic, and COBOL are examples of __________ __________.

Answer: programming languages 3. A(n) __________ is used to convert assembly language instructions to machine language.

Answer: assembler

Page 6: 9780538466523_IM_ch01

An Introduction to Programming with C++, Sixth Edition 1-6

4. A(n) __________ is used to convert high-level language instructions to machine language. Answer: compiler

Control Structures

1. All computer programs are written using one or more of three basic control structures: sequence, repetition, and selection.

2. Another term used for control structures is logic structures, because they control the logic flow of the program.

3. The sequence structure is used in every program that is written. Most programs use all

three control structures. The Sequence Structure

1. The sequence structure directs the computer to process the program instructions, one after another, in the order in which they are listed.

2. The sequence structure is the most simple of the basic control structures and is common to all computer programs.

3. An algorithm is a set of step-by-step instructions that accomplish a task. The Selection Structure

1. The selection structure, also called the decision structure, directs the computer to make a decision (evaluate a condition) and then take an appropriate action based upon that decision.

2. The selection structure allows the programmer to evaluate data and thereby properly control the logic flow of the program.

The Repetition Structure

1. The repetition structure, also referred to as a loop or iteration, directs the computer to repeat one or more program instructions until some condition is met and then stop.

2. The conditions may take the form of repeating a set of instructions some fixed number of times or indefinitely until some desired situation is reached.

Page 7: 9780538466523_IM_ch01

An Introduction to Programming with C++, Sixth Edition 1-7

Quick Quiz 2

1. The __________ structure is the most simple of the basic control structures. Answer: sequence

2. The __________ structure allows the programmer to create a loop in a program.

Answer: repetition 3. The __________ structure allows the programmer to evaluate a condition in a program.

Answer: selection 4. The selection structure is commonly called the __________ structure.

Answer: decision Discussion Topics

1. Discuss with the class which operating systems and application software are available in your labs on your campus. Ask students to explain why these might have been chosen.

2. Discuss with the class which programming languages are taught in your curriculum and

why they were chosen. 3. Show students examples of code from various programming languages and discuss the

advantages and disadvantages of each. If students have taken classes utilizing some of these languages, make sure to ask them to share their experiences.

4. Discuss the importance of program design and how it fits into the program development

process. 5. Have students give examples of when each control structure might be used. 6. Stress readability and maintainability at this stage—making sure that programs are easy

to read/understand and maintain. 7. Discuss code efficiency vs. readability and maintainability; which is more important?

Additional Projects

1. Have each student write the steps necessary to make a peanut butter and jelly sandwich. Start with one jar of peanut butter, one jar of jelly, and two knives in front of them on a table. End with the completed sandwich and all jars in their original position. (Two separate knives keep the peanut butter from getting into the jelly and vice-versa).

Page 8: 9780538466523_IM_ch01

An Introduction to Programming with C++, Sixth Edition 1-8

2. If you really want to have some fun with the class, purchase some plastic knives, a jar of peanut butter, a jar of jelly, and a couple of loaves of bread. Bring these items to class after your students have completed their previous peanut butter and jelly assignment (above), and split the class into groups of two. Have them take turns calling the instructions out to each other and actually making the sandwiches. This lets them observe the steps they have possibly missed first-hand and makes for a fun class!

3. Somewhere in the peanut butter and jelly assignment, surely the phrase “scoop the peanut

butter” will be used. Have students further define the term using instructions such as hand, knife, left, right, up, down, rotate, etc. Even though the overall process was previously defined, explain that we must continue to break down (refine) the task into actual instructions that can be performed (pretend we are telling a robot how to “scoop”).

Additional Resources 1. What is a computer algorithm?: http://computer.howstuffworks.com/question717.htm http://en.wikipedia.org/wiki/Algorithm 2. What is object-oriented programming?: www.google.com/search?hl=en&client=firefox-a&rls=org.mozilla:en-

US:official&hs=mWG&defl=en&q=define:object+oriented+programming&ei=oVmcSv3zGuCEmQe0tbSxBA&sa=X&oi=glossary_definition&ct=title

3. The homepage of Bjarne Stroustrup, the designer of the C++ programming language: www.research.att.com/~bs/homepage.html

Key Terms

Algorithm—the set of step-by-step instructions that accomplish a task Assembler—a program that converts assembly instructions into machine code Assembly languages—programming languages that use mnemonics, such as ADD Coding—the process of translating a solution into a language that the computer can

understand Compiler—a program that converts high-level instructions into a language that the

computer can understand; unlike an interpreter, a compiler converts all of a program’s instructions before running the program

Computer programs—the directions given to computers; also called programs Control structures—the structures that control the flow of a program’s logic; also called

logic structures; sequence, selection, and repetition Decision structure—another term for the selection structure High-level languages—programming languages whose instructions more closely

resemble the English language

Page 9: 9780538466523_IM_ch01

An Introduction to Programming with C++, Sixth Edition 1-9

Interpreter—a program that converts high-level instructions into a language that the computer can understand; unlike a compiler, an interpreter converts a program’s instructions, line by line, as the program is running

Iteration—another term for the repetition structure Logic structures—another term for control structures Loop—another term for the repetition structure Machine code—another term for machine language Machine language—computer instructions written in 0s and 1s; also called machine

code Mnemonics—the alphabetic abbreviations used to represent instructions in assembly

languages Object-oriented program—a program designed by focusing on the objects that the

program could use to accomplish its goal Procedure-oriented program—a program designed by focusing on the individual tasks

to be performed Programmers—the people who write computer programs Programming—giving a mechanism the directions to accomplish a task Programming languages—languages used to communicate with a computer Programs—the directions given to computers; also called computer programs Repetition structure—the control structure that directs the computer to repeat one or

more instructions until some condition is met, at which time the computer should stop repeating the instructions; also called a loop or iteration

Selection structure—the control structure that directs the computer to make a decision and then take the appropriate action based on that decision; also called the decision structure

Sequence structure—the control structure that directs the computer to process each instruction in the order listed in the program