Study Guide
Introduction to Programming By
Lisa T. Cordeiro
About the Author
Lisa T. Cordeiro served in the Marines Corps in Okinawa, Japan, working in telecommunications. She later earned two bachelor degrees from the University of Massachusetts at Amherst and a master’s degree from Harvard University’s Extension School. Ms. Cordeiro has worked as a Web manager and technical writer and editor for 13 years. She has written several study guides for Penn Foster on topics such as the Internet, Microsoft Office, and Adobe products. In addition, Ms. Cordeiro has written articles and books.
Copyright © 2015 by Penn Foster, Inc.
All rights reserved. No part of the material protected by this copyright may be reproduced or utilized in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system, without permission in writing from the copyright owner.
Requests for permission to make copies of any part of the work should be mailed to Copyright Permissions, Penn Foster, 925 Oak Street, Scranton, Pennsylvania 18515.
Printed in the United States of America
All terms mentioned in this text that are known to be trademarks or service marks have been appropriately capitalized. Use of a term in this text should not be regarded as affecting the validity of any trademark or service mark.
INSTRUCTION TO STUDENTS 1
LESSON ASSIGNMENTS 5
LESSON 1: INTRODUCTION TO PROGRAMMING 7
GRADED PROJECT—LESSON 1 15
LESSON 2: MAKING DECISIONS 17
GRADED PROJECT—LESSON 2 27
LESSON 3: ARRAYS AND MENU-DRIVEN PROGRAMS 29
GRADED PROJECT—LESSON 3 37
LESSON 4: ADVANCED PROGRAMMING 39
GRADED PROJECT—LESSON 4 51
iii
C o
n t
e n
t s
C o
n t
e n
t s
YOUR COURSE Welcome to your course in computer programming. In this course, you’ll learn the basics of computer programming and logic. You won’t learn a specific programming language; rather, you’ll examine the basic logic common to all programming languages. You’ll come away from this course with solid knowledge of how to create your own programs. You’ll also learn guidelines on how to incorporate good design into your programs. You’ll walk away from this course with a solid understanding of programming concepts that you can use in your own designs.
This course is designed for beginner programmers. You don’t need any previous programming experience or an extensive math background; the text is written in nontechnical lan- guage, making it easy for beginners to follow the logic. You also don’t need to focus on a specific programming language. This course focuses on basic guidelines and best practices for developing good programming skills.
OBJECTIVES When you complete this course, you’ll be able to
n Describe basic programming steps
n Create modules and hierarchy charts
n Use Boolean expressions
n Create loops
n Add control breaks
n Create arrays
n Search and sort arrays
n Validate input
1
In s
tr u
c tio
n s
In s
tr u
c tio
n s
n Solve problems with recursion
n Use UML diagrams to design classes
n Design graphical user interfaces
n Write event handlers
YOUR TEXTBOOK Your textbook for this course is Programming Logic and Design, Third Edition, by Tony Gaddis. The textbook contains the material on which you’ll be tested. You should become familiar with this text prior to beginning your studies.
Begin by skimming the brief contents on page v. Here, you’ll see a list of the chapter titles, which give you an overall idea of what you’ll learn in this course. The contents, which begins on page vii, provides a more detailed listing of the topics you’ll learn in each chapter. Read the preface beginning on page xiii to explore how the text is organized.
The main part of your textbook (pages 1–583) is the study material for the course. Each chapter begins with a list of topics, followed by the study material itself. Each chapter also includes checkpoints to make sure you understand the content. At the end of each chapter are review questions and exercises; you’ll find the answers to these questions and exercises on your student Web site. Log in to the Student Area at http://www.pennfoster.edu, go to Student portal, and look for the Solutions link associated with your course.
Three appendices—A, B, and C—are located at the end of the text. An index begins on page 601. A fourth appendix is located on the CD included at the back of the textbook. This appendix includes answers to the checkpoints in the text. The index includes important topics covered in your textbook, along with page references where you can locate material relating to each term.
Instructions to Students2
Instructions to Students
COURSE MATERIALS This course includes the following materials:
1. This study guide, which contains an introduction to your course, plus
n A lesson assignments page with a schedule of study assignments
n Assignment introductions emphasizing the main points in the textbook
n Four graded projects, one for each of the lessons in this course
2. Your course textbook, Programming Logic and Design, Third Edition, which contains the assigned reading mate- rial for this course
A STUDY PLAN 1. Note the pages for each assignment and read the intro-
ductory material in this study guide.
2. Study the assigned pages, paying attention to all details, especially the main concepts.
3. Complete the checkpoints in the textbook. After answering the checkpoints, check your answers with those provided in Appendix D on the CD. If you miss any of the ques- tions, review the pages in the textbook that cover those questions. These questions are designed to reveal weak points that you should review. Do not send the answers to these questions to the school. They’re for only you to eval- uate your understanding of the material.
3. Complete the review questions and all assigned exercises at the end of the chapter to review the material. When you’re finished, go to http://www.pennfoster.edu and log in to your Student Area. Go to student portal and look for the Solutions link associated with this course. The review questions and assigned exercises are for you to practice the course material; do not send your answers to the school.
3
4. After you’ve completed and checked answers for the assignments for Lesson 1, complete the graded project for Lesson 1 and follow the directions for submitting your projects. Follow Steps 1–4 for Lessons 2–4.
You can e-mail your instructor for assistance at any time. You’re now ready to begin. Good luck!
Instructions to Students4
Remember to check your student portal regularly. Your instructor may
post additional resources that you can access to enhance your learn-
ing experience.
Lesson 1: Introduction to Programming For: Read in the Read in study guide: the textbook:
Assignment 1 Pages 8–9 Pages 1–26
Assignment 2 Pages 10–12 Pages 27–78
Assignment 3 Pages 12–14 Pages 79–119
Graded Project 41882700 Material in Lesson 1
Lesson 2: Making Decisions For: Read in the Read in study guide: the textbook:
Assignment 4 Pages 17–20 Pages 121–168
Assignment 5 Pages 20–22 Pages 169–224
Assignment 6 Pages 22–23 Pages 225–265
Assignment 7 Pages 24–25 Pages 267–279
Graded Project 41882800 Material in Lesson 2
Lesson 3: Arrays and Menu-Driven Programs For: Read in the Read in study guide: the textbook:
Assignment 8 Pages 29–31 Pages 281–335
Assignment 9 Pages 31–33 Pages 337–374
Assignment 10 Pages 33–35 Pages 375–428
Assignment 11 Pages 35–36 Pages 429–474
Graded Project 41882900 Material in Lesson 3
5
A s
s ig
n m
e n
ts A
s s
ig n
m e
n ts
Lesson 4: Advanced Programming For: Read in the Read in study guide: the textbook:
Assignment 12 Pages 40–41 Pages 475–496
Assignment 13 Pages 41–42 Pages 497–517
Assignment 14 Pages 43–47 Pages 519–564
Assignment 15 Pages 47–50 Pages 565–583
Graded Project 41883000 Material in Lesson 4
Lesson Assignments6
7
L e
s s
o n
1 L
e s
s o
n 1
Introduction to Programming If you’re new to programming, you might be intimidated by code and flowcharts. You might even wonder how you’ll ever understand them. This lesson offers some basic ideas and programming examples with which you can follow along. You’ll soon understand the logic behind software programs.
In your first assignment, you’ll read an introduction to com- puters and programming. In the second assignment, you’ll learn the steps in creating a program. In the third assign- ment, you’ll learn about modules and the best practices for creating a software program.
OBJECTIVES When you complete this lesson, you’ll be able to
n Describe basic programming concepts and steps
n Declare variables
n Describe documentation
n Discuss the importance of structure
n Create modules
n Pass arguments
n Describe global variables and constants
This study guide is designed to break down the material into easy-to-understand chunks so that you aren’t intimidated by the material. Follow the directions outlined in the assign- ments, read the assigned text, and answer the questions to get the most from this course. You’ll soon understand the basics of programming.
Introduction to Programming8
ASSIGNMENT 1: AN OVERVIEW OF COMPUTERS AND PROGRAMMING Read this assignment, and then read pages 1–26 in your text- book. When you’re comfortable with the material presented in the text, answer the review questions in the textbook and com- plete the assigned exercises to gauge your progress. If there are any areas with which you don’t feel comfortable, reread those sections in the textbook before returning to the study guide. When you feel you’ve mastered the material in Chapter 1, move on to the next assignment.
Computers are a big part of people’s lives today, but what makes them capable of doing such powerful tasks? The answer is that they’re programmed to perform tasks, or what someone tells them to do. A program is a set of instructions for the computer to follow; programs are often referred to as software. Someone who creates programs or software is known as a programmer or software developer.
Hardware refers to the physical parts of a computer. You’ll read examples of different types of hardware, such as memory, a central processing unit (CPU), storage devices, input devices, and output devices.
A computer needs both hardware and software to perform its four major functions: input, processing, output, and storage. Programmers tell the computer what to do by using programming languages, such as Visual Basic and C#. The programming language must follow certain rules, called syntax, for it to be understood. The commands that tell the computer what to do also must follow a certain sequence, or logic, so the computer can process the request.
Computers store data in bytes, which are tiny storage loca- tions. Each byte is divided into eight smaller units called bits. These bits act like switches because they’re either “on” or “off.” When bits represent numbers, or binary numbers,
Note: Make sure to answer the checkpoints in the textbook as you
read along. Check your answers in Appendix D, which is located on
a PDF file on the CD included at the back of your textbook.
Lesson 1 9
they’re assigned the number 0 when off and 1 when on. You’ll read more about how numbers and characters are stored in this chapter.
Machines understand machine language, which is difficult for people to communicate in. Therefore, program languages that instruct computers on what to do have been created. Types of programming languages include C, C++, Java, JavaScript, and Visual Basic, as well as several more. These are known as high-level languages. Programmers write statements known as source code. High-level languages need to be translated into machine language. A program that handles this transla- tion is called a compiler.
After programs are created, they’re store on a storage device, such as a disk drive. When the CPU runs, or executes, a program, it follows three steps:
1. Fetch
2. Decode
3. Execute
Lastly, you’ll read about types of software. System software manages computer operations and includes operating sys- tems and software development tools. Application software is used for tasks, which include word processing, e-mail, and spreadsheets.
Now, answer the review questions at the end of Chapter 1, then complete Exercises 1–4 on page 26 of your textbook. Log in to your Student Area at http://www.pennfoster.edu. Go to Student portal and look for the Solutions link associ- ated with this course. After you complete the review questions and exercises, move on to the next assignment.
Introduction to Programming10
ASSIGNMENT 2: INPUT, PROCESSING, AND OUTPUT Read this assignment, and then read pages 27–78 in your text- book. When you’re comfortable with the material presented in the text, answer the review questions in the textbook and com- plete the assigned exercises to gauge your progress. If there are any areas with which you don’t feel comfortable, reread those sections in the textbook before returning to the study guide. When you feel you’ve mastered the material in Chapter 2, move on to the next assignment.
Programmers perform five steps when creating a program (Figure 1).
Syntax is the programming language’s rules. Debugging refers to fixing any errors in the code so the program will work correctly. You’ll read about each of these steps in greater detail in this chapter.
When planning the logic of a program, programmers will often use flowcharts or pseudocode. A flowchart is a pictorial representation of the steps in a pro- gram. Pseudocode is an English-like representation of the steps. See Appendix B and Appendix C in your textbook for more information on flowcharts and pseudocode.
Computer programs work by following these three steps:
1. Receive input (data)
2. Perform a process
3. Produce output (such as displaying informa- tion or performing a task)
Program code can become quite complicated. When a collection of program statements becomes difficult to read, it’s called spaghetti code because it’s like following a noodle in a bowl of spaghetti. Therefore, keep your code simple by using struc-
FIGURE 1—Steps of Program Creation
Note: Make sure to answer the checkpoints in the textbook as you
read along. Check your answers in Appendix D, which is located on
a PDF file on the CD included at the back of your textbook.
Lesson 1 11
tures. A sequence is one kind of structure (Figure 2). You can stack structures on each other at entrance or exit points. Putting one structure within another is called nesting.
Programmers create variables that stand for dif- ferent values. The name of a variable should be no more than one word. Programmers end pro- grams by creating a decision, which is represented in a flowchart by a diamond. If the flowchart runs onto another page, they use a connector. When assigning values to variables, programmers often use the equal sign (=).
Many programs require some sort of calculation to be performed, such as addition or multiplica- tion. In this chapter, you’ll read about these and other common math operations, as well as the order of operations.
In most programs, variables must be declared before they can be used. This means that you must name the memory location and specify the type of data. Variable names are usually nouns, such as payRate, and types are the type of data the variable will hold (numbers or letters, for example).
Two types of documentation are generated during the pro- gramming process. Internal program documentation is for the programmer and includes program comments that identify the logic of the program. External documentation is for the people who use your programs—the end users—who will need documentation to know how to use your program. This type of documentation is also called user documentation.
Now, answer the review questions at the end of Chapter 2. In addition, complete the following exercises at the end of the chapter:
n Algorithm Workbench 1, 5, and 11
n Debugging Exercises 1 and 2
n Programming Exercises 1 and 7
FIGURE 2—A Sequence
Introduction to Programming12
Log in to the Student Area at http://www.pennfoster.edu. Go to Student portal and look for the Solutions link associ- ated with this course. After you complete the review questions and exercises, move on to the next assignment.
ASSIGNMENT 3: MODULES Read this assignment, and then read pages 79–119 in your textbook. When you’re comfortable with the material presented in the text, answer the review questions in the textbook and complete the assigned exercises to gauge your progress. If there are any areas with which you don’t feel comfortable, reread those sections in the textbook before returning to the study guide. When you feel you’ve mastered the material in Chapter 3, move on to the next assignment.
Programmers usually break down a program into separate units called modules. Modularization, or breaking programs into modules, is beneficial because it
n Uses simpler code
n Promotes code reuse
n Enables better testing
n Allows multiple programmers to work on a program
When naming a module, be sure to give it a one-word name that identifies it. Module names are often verbs, such as calculatePayment. A separate flowchart is created for each module. The module is called by placing the module’s name in a rectangle with bars across the sides (Figure 3). Note that modules can also call other modules.
To keep track of modules, you can create a hierarchy chart, which shows the relationship between modules
and demonstrates which modules call which other modules (Figure 4).
FIGURE 3—In a flowchart, modules
are depicted by a rectangle with a
bar across the top.
Note: Make sure to answer the checkpoints in the textbook as you
read along. Check your answers in Appendix D, which is located on
a PDF file on the CD included at the back of your textbook.
Lesson 1 13
Local variables are those declared inside modules. Global variables are those that can be accessed by all modules in a program; they’re declared outside modules.
Arguments are pieces of data sent into modules. Parameters are variables that receive arguments passed into modules. You’ll read about various ways to pass arguments into mod- ules, such as passing arguments by value and by reference.
When you create a program, you should develop a plan before you start so that you create a sound program design. One way to facilitate sound program design is to store your programming components in separate files so that you can easily find, read, and reuse segments. Another good practice is to choose suitable names for variables and modules. The following list contains some tips for naming variables and modules:
n Use meaningful names.
n Use names that are easily pronounced.
n Choose abbreviations that are easily understood by others.
n Avoid using numbers.
n Use both upper- and lowercase letters (camel case), if possible, in multiword names, or use dashes or under- scores to separate words.
n Use verbs in status names.
FIGURE 4—A hierarchy
chart shows the relation-
ships between modules
just, as a company’s orga-
nizational chart shows the
relationships within the
company or department.
Introduction to Programming14
To create programs with good design, you should also divide your line breaks consistently. Note that shorter lines are easier to read and follow. In addition, consider using tempo- rary variables when you have a long statement so that you can organize multiple variables.
Finally, you should use constants instead of numbers in your programs when possible. For example, suppose you have a value for an employee’s pay rate. Preferably, you should store the value as payRate rather than 10 (for $10 an hour), because the employee’s pay rate might change if he or she receives a raise.
Now, answer the review questions at the end of Chapter 3. In addition, complete the following exercises at the end of the chapter:
n Algorithm Workbench 1 and 2
n Debugging Exercises 1 and 4
n Programming Exercise 1
Check your answers by logging in to the Student Area at http://www.pennfoster.edu. Go to Student portaland look for the Solutions link associated with this course.
After completing the review questions and exercises, complete the first graded project. After you complete the graded project, move on to Lesson 2.
15
G ra
d e
d P
ro je
c t
G ra
d e
d P
ro je
c t
Lesson 1 Graded Project
Project Number: 41882700
This graded project will test your knowledge of what you’ve learned in this course so far. If you’ve read all the material in each assignment and completed the checkpoints, review questions, and assigned exercises in the text, you shouldn’t have any problems successfully completing these tasks.
Complete Exercise 6 on page 118 by creating both
n Pseudocode
n A flowchart
Scenario A personal trainer asks you to create a program to calculate and displays client’s body mass index (BMI). Use the information in Exercise 6 on page 118 to create this program by
1. Creating the pseudocode for this program
2. Creating a flowchart for this program
Hint: Use Visio or Microsoft Word’s Drawing toolbar.
Submitting Your Project Follow this procedure to submit your assignment online:
1. On your computer, save a revised and corrected version of your project. Make sure to include the project number (41882700) to identify the project.
2. Go to http://www.pennfoster.edu and log in.
3. Go to Student portal.
4. Click on Take Exam next to the lesson on which you’re working
5. Enter your e-mail address in the box provided. (Note: This information is required for online submission.)
Graded Project16
6. Attach your file or files as follows:
n Click on the Browse box.
n Locate the file you wish to attach.
n Double-click on the file.
n Click on Upload File.
7. Click on Submit Files.
Grading Your instructor will grade your project as follows:
n Used the lessons learned in this course to create the pseudocode for this program 25 points
n Created the modules for an effective program 25 points
n Created a flowchart for this program using lessons learned in this course 25 points
n Used the appropriate flowchart shapes to create a flowchart 25 points
TOTAL POINTS 100 points
17
L e
s s
o n
2 L
e s
s o
n 2
Making Decisions In this lesson, you’ll learn how to create and structure deci- sions in your programs. You’ll learn how to create loops and when to use loop-type structures in your programs. You’ll learn how you can use functions, which are available in many language libraries. You’ll also learn about the input validation loop and defensive programming.
OBJECTIVES When you complete this lesson, you’ll be able to
n Insert Boolean expressions for comparisons
n Create decision structures
n Use case structures and logical operators
n Create different types of loops
n Write and use functions
n Create an input validation loop
n Describe defensive programming
ASSIGNMENT 4: DECISION STRUCTURES AND BOOLEAN LOGIC Read this assignment, and then read pages 121–168 in your textbook. When you’re comfortable with the material presented in the text, answer the review questions in the textbook and complete the assigned exercises to gauge your progress. If there are any areas with which you don’t feel comfortable, reread those sections in the textbook before returning to the study guide. When you feel you’ve mastered the material in Chapter 4, move on to the next assignment.