0% found this document useful (0 votes)
15 views

Pearson compsci - chapter 1

This document introduces programming concepts using C++, covering the roles and qualities of programmers, the history and types of programming languages, and basic control structures like sequence, selection, and repetition. It emphasizes the importance of algorithms in programming and provides examples of programming tasks. Additionally, it outlines employment opportunities for programmers and the skills required for success in the field.

Uploaded by

eufceeuce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Pearson compsci - chapter 1

This document introduces programming concepts using C++, covering the roles and qualities of programmers, the history and types of programming languages, and basic control structures like sequence, selection, and repetition. It emphasizes the importance of algorithms in programming and provides examples of programming tasks. Additionally, it outlines employment opportunities for programmers and the skills required for success in the field.

Uploaded by

eufceeuce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

An Introduction to

Programming with C++


Sixth Edition

Chapter 1
An Introduction to Programming
Objectives
• 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

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


Objectives (cont’d.)
• Explain the sequence, selection, and repetition
structures
• Write simple algorithms using the sequence,
selection, and repetition structures

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


Programming a Computer
• Programs are the directions given to computers
• Programmers are the people who write computer
programs
• Programming Languages enable programmers to
communicate with the computer
– Popular languages: C++, Visual Basic, C#, Java

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


The Programmer’s Job
• Programmers help solve computer problems
• Employee or freelance
• Typical steps involved
– Meet with user to determine problem
(requirements phase)
– Convert the problem into a program
(design and coding)
– Test the program
(progressive process)
– Provide user manual

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


Do I Have What It Takes to Be a
Programmer/Computer Scientist?
• Qualities employers look for
– Logical and analytical thinking
– Close attention to detail
– Patience and persistence
– Ingenuity and creativity
– Good communication skills: technical and
nontechnical
– Business skills (for managerial positions)

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


Employment Opportunities
• Computer programmer: codes a computer solution
– Coding is the process of translating a computer
solution into a language a computer can understand
• Computer software engineer: designs an
appropriate solution to a user’s problem
• Some positions call for both engineering and
programming

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


A Brief History of Programming
Languages
• Enable programmer to communicate with computer
• Different types
– Machine languages
– Assembly languages
– High-level languages

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


Machine Languages
• The first programmers had to write the program
instructions using only combinations of 0s and 1s
– Example: 00101 10001 10000
• Instructions written in 0s and 1s are called
machine language or machine code
• Each type of machine has its own language
• Machine languages are the only way to
communicate directly with the computer
• Programming in machine language: tedious and
error-prone; requires highly trained programmers
An Introduction to Programming with C++, Sixth Edition 9
Assembly Languages
• Assembly languages simplify programmer’s job
• Can use mnemonics instead of 0s and 1s
– Example: ADD bx, ax
• Assembly programs require an assembler to
convert instructions into machine code
• Easier to write programs in assembly language
– But still tedious and requires highly trained
programmers

An Introduction to Programming with C++, Sixth Edition 10


High-Level Languages
• High-level languages allow programmer to use
English-like instructions
– Example: grossPay = hours * rate
– High-level languages are more machine independent
• Programs written in a high-level language can be used
on many different types of computers
• Compilers translate high-level instructions into 0s
and 1s (machine language)
• Interpreters translate the program line by line as
the program is running

An Introduction to Programming with C++, Sixth Edition 11


High-Level Languages (cont’d.)
• When writing a procedure-oriented program, the
programmer concentrates on the major tasks that
the program needs to perform
– Examples: COBOL, BASIC, C
• An object-oriented program requires programmer
to focus on the objects that the program can use to
accomplish its goal
– Examples: C++, Visual Basic, Java, C#
• Object-oriented programs allow for better code-
reuse
– An object can be used in more than one program
An Introduction to Programming with C++, Sixth Edition 12
Control Structures
• Programs are written using three basic structures
– Sequence
• Used in every program you write
– Repetition
• Used in most programs you write
– Selection
• Used in most programs you write
• These are called control structures or logic
structures

An Introduction to Programming with C++, Sixth Edition 13


The Sequence Structure
• The sequence structure directs the computer to
process the program instructions, one after
another, in the order listed in the program
• An algorithm is a set of step-by-step instructions
that accomplish a task
• Example: following a recipe to make cookies

An Introduction to Programming with C++, Sixth Edition 14


The Sequence Structure (cont’d.)

Figure 1-1 An example of the sequence structure

An Introduction to Programming with C++, Sixth Edition 15


The Selection Structure
• Selection structure: makes a decision and then
takes an appropriate action based on that decision
– Also called the decision structure
• Example: waiting or crossing at railroad tracks
depending on signal lights

An Introduction to Programming with C++, Sixth Edition 16


The Selection Structure (cont’d.)

Figure 1-2 An example of the selection structure

An Introduction to Programming with C++, Sixth Edition 17


The Selection Structure (cont’d.)

Figure 1-3 Another example of the selection structure

An Introduction to Programming with C++, Sixth Edition 18


The Repetition Structure
• Repetition structure: directs computer to repeat
one or more instructions until some condition is met
– Also called a loop or iteration

An Introduction to Programming with C++, Sixth Edition 19


The Repetition Structure (cont’d.)

Figure 1-4 Modified algorithm showing the repetition structure

An Introduction to Programming with C++, Sixth Edition 20


The Repetition Structure (cont’d.)
• What could you do if you don’t know precisely how
many steps separate Robin from the boxes?

An Introduction to Programming with C++, Sixth Edition 21


The Repetition Structure (cont’d.)

Figure 1-5 Algorithm showing the modified condition in the repetition structure

An Introduction to Programming with C++, Sixth Edition 22


Summary
• Programs are step-by-step instructions that tell a
computer how to perform a task
• Programmers use programming languages to
communicate with the computer
– First programming languages were machine
languages: 0s and 1s (machine code)
– Next came assembly languages, which allowed for
mnemonics
– High-level languages can be used to create
procedure-oriented programs or object-oriented
programs

An Introduction to Programming with C++, Sixth Edition 23


Summary (cont’d.)
• Algorithm: step-by-step instructions that
accomplish a task (not written in a programming
language)
– Algorithms contain one or more of the following
control structures: sequence, selection, and
repetition
• Sequence structure: process the instructions, one
after another, in the order listed

An Introduction to Programming with C++, Sixth Edition 24


Summary (cont’d.)
• Selection structure: directs the computer to make a
decision and then to select an appropriate action
based on that decision
• Repetition structure: repeat one or more
instructions until some condition is met

An Introduction to Programming with C++, Sixth Edition 25


Lab 1-1: Stop and Analyze
• A local business employs five salespeople and
pays a 3% bonus on a salesperson’s sales
• Your task is to create a program that calculates the
amount of each salesperson’s bonus
• The program should print each salesperson’s name
and bonus amount

An Introduction to Programming with C++, Sixth Edition 26


Lab 1-2: Plan and Create
• Using only the instructions shown below, create an
algorithm that shows the steps an instructor takes
when grading a test that contains 25 questions

An Introduction to Programming with C++, Sixth Edition 27


Lab 1-3: Modify
• Modify the algorithm shown in Lab 1-1 so that it
gives a 3.5% bonus to salespeople selling more
than $2,000
• All other salespeople should receive a 3% bonus
• original algorithm:
repeat 5 times
enter the salesperson’s name and sales amount
calculate bonus by multiplying sales amount by 3%
print the salesperson’s name and bonus amount
end repeat

An Introduction to Programming with C++, Sixth Edition 28


Lab 1-3: Modify
• Modify the algorithm shown in Lab 1-1 so that it
gives a 3.5% bonus to salespeople selling more
than $2,000
• All other salespeople should receive a 3% bonus
• new algorithm(1)
repeat 5 times
enter the salesperson’s name and sales amount
if ( sales amount is greater than $2,000 )
calculate bonus by multiplying sales amount by 3.5%
else
calculate bonus by multiplying sales amount by 3%
end if
print the salesperson’s name and bonus amount
end repeat
29
Lab 1-3: Modify
• Modify the algorithm shown in Lab 1-1 so that it
gives a 3.5% bonus to salespeople selling more
than $2,000
• All other salespeople should receive a 3% bonus
• new algorithm(2)
repeat 5 times
enter the salesperson’s name and sales amount
if ( sales amount is less than $2,000 )
calculate bonus by multiplying sales amount by 3%
else
calculate bonus by multiplying sales amount by 3.5%
end if
print the salesperson’s name and bonus amount
end repeat
• Wrong! Will give incorrect results if sales amount is exactly $2,000
30

You might also like