Transcript Chapter 1

Chapter 1
Introduction to Computer and C++
Programming
Computer Systems
• There are 3 classes of computers:
– Personal Computers (PCs)
– Workstations
– Mainframes
• Network:
– Consists of a number of computers connected
to share resources and information.
– Including server and client computers.
Computer Hardware
• Input devices:
– Allowing users to communicate information to
computers.
– Ex: keyboard, mouse, scanner…
• Output devices:
– Allowing computer to communicate
information to users.
– Ex: monitor, printer
Computer Hardware (cont.)
• Central Processing Unit (CPU).
– Is the Control unit (cerebellum) of all
operations of a computer
– Containing Arithmetic and Logical Unit (ALU)
Computer Hardware (cont.)
• Main memory:
– Temporary storage for data
– Very fast to access
– Random access memory (RAM)
– Locations: a number to present the memory
address
– Units:
• Bit: the smallest unit, containing binary values (0 or 1)
• Byte: equals to 8 bits (standard unit of memory)
• Word: equals to 2 bytes
Computer Hardware (cont.)
• Secondary memory:
– Permanent storage
– Slower to access
– Removable
– Information stored a files (execution, data…)
– Ex: floppy drive, hard drive, thumb drive, CDs
...
Main Components of Computers
CPU
Input
Device(s)
Output
Device(s)
Main
Memory
Secondary
Memory
Computer Software
• Operating System (OS)
– The SW to:
• Control behaviors of a computer.
• Control communications to memories and devices
• Manage resources
– Ex: DOS, UNIX, Linux, Windows, Android,
Mac …
Computer Software (cont.)
• Program
– Is a set of instructions for a computer to
follow.
– Usually known as application
• Data
– Inputs to a program
Computer Languages
•
Machine language:
– low level - On or Off - series of 0’s and 1’s.
– Difficult for humans to read and debug. Very
fast for computer to process.
– Specific to a particular machine.
– Example:
0001 0010 0100 0001 (16-bit)
0000 0001 0000 0010 0000 0100 0000 0001 (32-bit)
Computer Languages (cont.)
•
Assembly language
– consists of a command and one or two
storage locations which have been assigned
letter names.
– Computer must interpret into machine
language using an assembler
– Example:
Add R2, R4, R1
Computer Languages (cont.)
•
Higher level languages
–
–
–
–
like English. Easy to read and understand.
Portable to many machines and has a language
standard. Slower to process. Commands are
written and saved in a source file.
A compiler program translates into machine
language object or binary file and a linker connects
it to other programs (such as to printer), loads in
memory and executes. If there are any language
(SYNTAX) errors the compiler cannot compile and
the compilation stops.
Example:
R1 = R2 + R4;
Programming and Problem Solving
• Programming is Problem Solving!!!!!
Algorithm
• A sequence of precise instructions that
lead to a solution or step-by-step set of
instructions to solve a problem.
• Three requirements:
1. There must be a finite number of steps.
2. Each step must be well defined.
3. Must accomplish the task.
Program Design
– Precise Definition of the problem
(WHAT’s)
•
•
•
What will the output of your program be?
What information is needed?
What formulas are needed
– Algorithm Design (HOW’s)
•
Define how to solve the problem
– Testing
•
Check if the problem solved.
Algorithm Design (example)
Determine how many times a name occurs in a list
of names.
1. Get the list of names.
2. Get the name being checked.
3. Set a counter to 0.
4. For each name in the list, do the followings:
•
•
Compare the name with the targeted name.
If the names are the same, increase the counter.
5. Announce the answer is the number indicated
by the counter.
Object Oriented Programming
(OOP)
• A new approach where we model objects
in the real world developed in the 1990’s.
Used first for simulations, data programs,
it has now become the industry standard.
Objects are defined by code called
classes.
OOP Characteristics
• Encapsulation: Combining the data of
the object with its behavior and hiding the
implementation from the user.
• Inheritance: Allowing one object to reuse
code of another object.
• Polymorphism: Giving different
meanings to the same name.
Software Development Cycle
1. Analysis and specification of the task (Problem
definition).
2. Design the software (Object and algorithm
design)
3. Implementation (Coding)
4. Testing
5. Maintenance and Enhancement
Who programs first, finishes last!
Example of SW Design
• Compute the miles per gallon for a trip of a
certain distance in miles
Example of SW Design (cont.)
• Analysis: (WHAT’s)
– Distance (miles)
– Gas amount (gallons)
– MPG (miles per gallon)
Data Dictionary
Identifier
Type
I/O
Description
miles
Double
input
miles > 0.0
gallons
Double
input
gallons > 0.0
MPG
Double
output
Miles per gallon
Example of SW Design (cont.)
• Initial Design: (HOW’s)
– Read in distance
– Read in gas amount
– Calculate MPG
– Print out result
Example of SW Design (cont.)
•
Refined Design: (HOW’s)
1. Read in distance (miles)
•
Check if distance <= 0.0, then go back to 1
2. Read in gas amount (gallons)
•
Check if gallons <= 0.0, then go back to 2
3. Calculate MPG = miles/gallons
4. Print out result
Example of SW Design (cont.)
• Structure chart:
– (Top Down Design: Divide and Conquer Break down the main problem into
subproblems and the data flow between them.
– Minimum of 3 subtasks: Input, Process,
Output
Example of SW Design (cont.)
Structure Chart
Example 1
Level 0
Compute Miles per
gallon
Miles
Gallons
Level 1
Get Input
Miles
Gallons
MPG
Miles
Gallons
Output
Compute MPG
Miles
Gallons
Miles
Gallons
MPG
Error Check
Level 2
Print Header
Header_Out
Print Results
Example of SW Design (cont.)
FLOW CHART
Start
Get
distance
NO
Distance > 0?
YES
YES
Get gallons
NO
Gallons > 0?
YES
Calculate
MPG
Output
result
End
Introduction to C++
1. BCPL - Binary Coded Programming Language - System language,
supported integers only - Martin Richard
2. B -evolved from BCPL as a general purpose language, however
allows only native (machine dependent) data types- Ken Thompson
3. C - separated data types from machine dependency - Dennis
Ritchie in 1970’s. More portable, simpler. Became popular with
influx of PC’s.
– UNIX - first operating system written in higher level language and thus
portable
– C allows access to machine addresses and is sometimes considered a
mid or hybrid language. Dangerous because it allows user to alter
operating system.
4. C ++ developed in 1980’s by Bjarne Stroustrup is an extension of C
with the inclusion of object-oriented programming. C++ overlaps C.
Sample C++ Program
//Sample Program
#include <iostream>
using namespace std;
int main ()
{
int number_of pods, peas_per_pod, total_peas;
cout << “Press return after entering a number.\n”;
cout << “Enter the number of pods:\n”;
cin >> number_of_pods;
cout << “Enter the number of peas in a pod:\n”;
cin >> peas_per_pods;
total_peas = number_of_pods * peas_per_pod;
cout << “If you have ”;
cout << numer_of_pods;
cout << “ pea pods\n”;
cout << “ and ” << peas_per_pods;
cout << “ peas in each pod, then\n”;
cout << “you have” << total_peas;
cout << “ peas in all the pods.\n”;
return 0;
}
Sample C++ Program (cont.)
Comment:
//Sample Program
Anything after // is not
concerned by the compiler.
#include <iostream>
using namespace std;
int main ()
{
int number_of pods, peas_per_pod, total_peas;
cout << “Press return after entering a number.\n”;
cout << “Enter the number of pods:\n”;
cin >> number_of_pods;
cout << “Enter the number of peas in a pod:\n”;
cin >> peas_per_pods;
total_peas = number_of_pods * peas_per_pod;
cout << “If you have ”;
cout << numer_of_pods;
cout << “ pea pods\n”;
cout << “ and ” << peas_per_pods;
cout << “ peas in each pod, then\n”;
cout << “you have” << total_peas;
cout << “ peas in all the pods.\n”;
return 0;
}
Include directive:
#include: a preprocessing directive to link this library
to this program prior to execution.
The <> says look in directory where the compiler is
stored first. Later, you will write your own libraries and
store them on your disk. Then you will use “file.h”
which tells the compiler to look in the current directory
first (where your program is stored).
iostream: This is a library containing standard I/O
functions. C++ is a very powerful language that can do
many things. To facilitate the programmer’s job, C++
contains code to do common tasks (such as graphics,
input/output, math functions, etc.) and groups these
functions in libraries.
C++ divides names into namespaces. The
standard language names are in the standard
(std) namespace. We need to use this line to
use the standard libraries.
Sample C++ Program (cont.)
int main ()
//Sample Program
A C++ program is a function that may call other functions to
#include <iostream>
accomplish subtasks
using namespace std;
main: is the main program function—it is always used.
{} is used to group together the code inside a function. begin-end.
int main ()
{
Variable Declarations (Storing data):
int number_of pods, peas_per_pod, total_peas;
sets up storage spaces in the memory to store information and
attaches the name (area).
cout << “Press return after entering a number.\n”;
The word int tells the computer it will need enough storage space to
cout << “Enter the number of pods:\n”;
store an integer number and to use that format.
cin >> number_of_pods;
cout << “Enter the number of peas in a pod:\n”;
Input statement: cin>> variable;
cin >> peas_per_pods;
stores value typed from standard input device
(keyboard) into variable.
total_peas = number_of_pods * peas_per_pod;
cout << “If you have ”;
cout << numer_of_pods;
cout << “ pea pods\n”;
cout << “ and ” << peas_per_pods;
cout << “ peas in each pod, then\n”;
cout << “you have” << total_peas;
cout << “ peas in all the pods.\n”; Return statement:
return 0;
}
returns the value
computed by the function
and control back to the
operating system.
Output statement:
sends output to the standard output device (screen)
String constants (in double quotes) are to be printed as
is,
without the
quotes.
example
semicolons
denote
the In
end
of eachabove,
line in “Enter...
C++. “ is
called a prompt. All user input must be preceded by a
prompt to tell the user what is expected.
\n advances the cursor to the start of the next line
rather than to the next space. Backslash is the escape
symbol. It means ignore the usual meaning of the
next symbol and go for the alternate.
Variables are displayed when named in cout.
cout<<total_peas;
Compiling and Running a C++ Program
• Source code: The file you type in with the
editor.
• Compile and Build: Translates your code
into machine language-object code and
adds system libraries to your code.
– Compile detects syntax errors.
– Build detects linking errors
• Run: Links your code to the standard
routines and executes.
Testing and Debugging
• Syntax errors: improper use of the language.
Caught by the compiler. Fatal
• Runtime errors: violations in execution (divide by
zero, negative square root) Fatal—program
terminates
• Logic errors: mistake in algorithm: non fatal time consuming
• Warnings: you are doing something that can be
done but is usually ill-advised.
• DEBUGGER: a tool to watch a program during
execution. Used for runtime and logic errors.