You are on page 1of 16

٠٧/٠١/٤٠

Chapter 1: Introduction to Computers and


Programming Topics
1.1 Why Program?
Starting Out with C++ 1.2 Computer Systems: Hardware and
Early Objects Software
Eighth Edition 1.3 Programs and Programming Languages
1.4 What Is a Program Made of?
by Tony Gaddis, Judy Walters,
and Godfrey Muganda 1.5 Input, Processing, and Output
1.6 The Programming Process

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-2

١
٠٧/٠١/٤٠

1.2 Computer Systems: Hardware and


1.1 Why Program? Software
Computer – programmable machine designed to Hardware – Physical components of a
follow instructions computer
Program/Software – instructions in computer Main Hardware Component Categories
memory to make it do something
1. Central Processing Unit (CPU)
Programmer – person who writes instructions
(programs) to make computer perform a task 2. Main memory (RAM)
3. Secondary storage devices
SO, without programmers, no programs; without 4. Input Devices
programs, the computer cannot do anything
5. Output Devices

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-3 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-4

٢
٠٧/٠١/٤٠

Main Hardware Component Categories Central Processing Unit (CPU)


CPU – Hardware
component that runs
programs
Includes
• Control Unit
– Retrieves and decodes
program instructions
– Coordinates computer
operations

• Arithmetic & Logic Unit


(ALU)
– Performs mathematical
operations
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-5 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-6

٣
٠٧/٠١/٤٠

The CPU's Role in Running a Program Main Memory


Cycle through: • Holds both program instructions and data
• Fetch: get the next program instruction
• Volatile – erased when program
from main memory terminates or computer is turned off
• Decode: interpret the instruction and
generate a signal • Also called Random Access Memory
(RAM)
• Execute: route the signal to the
appropriate component to perform an
operation

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-7 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-8

٤
٠٧/٠١/٤٠

Main Memory Organization Secondary Storage


• Bit
– Smallest piece of memory • Non-volatile - data retained when
– Stands for binary digit program is not running or computer is
– Has values 0 (off) or 1 (on) 8 bits
turned off
• Byte
0 1 1 0 0 1 1 1 • Comes in a variety of media
– Is 8 consecutive bits
– Has an address – magnetic: floppy or hard disk drive,
internal or external
• Word – optical: CD or DVD drive
– Usually 4 consecutive bytes 1 byte – flash: USB flash drive
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-9 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-10

٥
٠٧/٠١/٤٠

Input Devices Output Devices

• Used to send information to the


computer from outside
• Used to send information from the
• Many devices can provide input computer to the outside
– keyboard, mouse, microphone, scanner,
digital camera, disk drive, CD/DVD drive, • Many devices can be used for output
USB flash drive – Computer screen, printer, speakers, disk
drive, CD/DVD recorder, USB flash drive

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-11 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-12

٦
٠٧/٠١/٤٠

Software Programs That Run on a 1.3 Programs and Programming


Computer Languages
• System software • Program
– programs that manage the computer hardware and the
programs that run on the computer a set of instructions directing a computer to
– Operating Systems perform a task
• Controls operation of computer
• Manages connected devices • Programming Language
• Runs programs
– Utility Programs a language used to write programs
• Support programs that enhance computer operations
• Examples: anti-virus software, data backup, data compression
– Software development tools
• Used by programmers to create software
• Examples: compilers, integrated development environments
(IDEs)

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-13 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-14

٧
٠٧/٠١/٤٠

Algorithm Programs and Programming Languages


Types of languages
Algorithm: a set of steps to perform a task – Low-level: used for communication with
or to solve a problem computer hardware directly.
– High-level: closer to human language
Order is important. Steps must be
performed sequentially

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-15 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-16

٨
٠٧/٠١/٤٠

From a High-level Program to an From a High-level Program to an


Executable File Executable File

a) Create file containing the program with a d) Run linker to connect hardware-specific
text editor. library code to machine instructions,
b) Run preprocessor to convert source file producing an executable file.
directives to source code program Steps b) through d) are often performed by a
statements. single command or button click.
c) Run compiler to convert source program Errors occuring at any step will prevent
statements into machine instructions. execution of the following steps.

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-17 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-18

٩
٠٧/٠١/٤٠

From a High-level Program to an


Executable File 1.4 What Is a Program Made Of?

Common elements in programming


languages:

– Key Words
– Programmer-Defined Identifiers
– Operators
– Punctuation
– Syntax

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-19 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-20

١٠
٠٧/٠١/٤٠

Example Program Key Words


#include <iostream>
using namespace std;
• Also known as reserved words
• Have a special meaning in C++
int main()
{ • Can not be used for another purpose
double num1 = 5,
num2, sum; • Written using lowercase letters
num2 = 12;
• Examples in program (shown in green):
sum = num1 + num2; using namespace std;
cout << "The sum is " << sum; int main()
return 0;
}
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-21 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-22

١١
٠٧/٠١/٤٠

Programmer-Defined Identifiers Operators


• Names made up by the programmer • Used to perform operations on data
• Not part of the C++ language • Many types of operators
– Arithmetic: +, -, *, /
• Used to represent various things, such as
variables (memory locations) – Assignment: =

• Example in program (shown in green): • Examples in program (shown in green):


num2 = 12;
double num1
sum = num1 + num2;

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-23 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-24

١٢
٠٧/٠١/٤٠

Punctuation Lines vs. Statements


• Characters that mark the end of a In a source file,
statement, or that separate items in a list A line is all of the characters entered before a
• Example in program (shown in green): carriage return.
double num1 = 5, Blank lines improve the readability of a
num2, sum; program.
num2 = 12; Here are four sample lines. Line 3 is blank:
1. double num1 = 5, num2, sum;
2. num2 = 12;
3.
4. sum = num1 + num2;

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-25 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-26

١٣
٠٧/٠١/٤٠

Lines vs. Statements Variables


In a source file, • A variable is a named location in computer memory (in
RAM)
A statement is an instruction to the computer to
perform an action. • It holds a piece of data. The data that it holds may
change while the program is running.
A statement may contain keywords, operators,
programmer-defined identifiers, and • The name of the variable should reflect its purpose
punctuation. • It must be defined before it can be used. Variable
A statement may fit on one line, or it may definitions indicate the variable name and the type of
data that it can hold.
occupy multiple lines.
Here is a single statement that uses two lines: • Example variable definition:
double num1 = 5, double num1;
num2, sum;
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-27 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-28

١٤
٠٧/٠١/٤٠

1.5 Input, Processing, and Output 1.6 The Programming Process


Three steps that many programs perform 1. Define what the program is to do.
1) Gather input data 2. Visualize the program running on the
- from keyboard computer.
- from files on disk drives 3. Use design tools to create a model of the
2) Process the input data program.
3) Display the results as output Hierarchy charts, flowcharts, pseudocode, etc.
- send it to the screen or a printer 4. Check the model for logical errors.
- write it to a file 5. Write the program source code.
6. Compile the source code.

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-29 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-30

١٥
٠٧/٠١/٤٠

Chapter 1: Introduction to Computers and


The Programming Process (cont.) Programming
7. Correct any errors found during compilation.
8. Link the program to create an executable file. Starting Out with C++
Early Objects
9. Run the program using test data for input. Eighth Edition
10. Correct any errors found while running the
program. by Tony Gaddis, Judy Walters,
Repeat steps 4 - 10 as many times as necessary. and Godfrey Muganda

11. Validate the results of the program.


Does the program do what was defined in step 1?

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-31 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

١٦

You might also like