Escolar Documentos
Profissional Documentos
Cultura Documentos
CSIS1117A
CS S
Computer Programming I
1
Contents
Introduction to hardware and software
Steps for running a program
L
Layoutt off a C
C++ program
Testing and Debugging
c1117 lecture 1 2
Introduction
Basic concepts
Program: A set of instructions for a computer to follow
Hardware: The actual physical parts that make up a
computer
Software: The collection of programs used by a computer
Hardware (only the simple model that is sufficient
for our programming purpose is mentioned)
Basic components: Central processing unit, main memory,
input/output devices,
devices secondary memory
memory.
c1117 lecture 1 3
Output device
Input device
P
Power Supply
S l
H d Disk
Hard Di k
DVD Drive
Main Memory
CPU
c1117 lecture 1 4
Hardware
Ce t a Processing
Central ocess g U
Unitt (C
(CPU):
U) tthee “brain”
ba of computer
o co pute
use to perform basic operations like simple calculation
A Pentium chip
c1117 lecture 1 5
Hardware
Input/output device: allow computer to communicate
information with users.
e.g keyboard, mouse, monitor, speaker, printer
Secondary memory: For permanent storage of data &
information in units of files
e.g. hard disk, ssd (solid-state drives), DVD/CD ROM,
memory stick
The information will NOT be lost even after turning off
the computer
c1117 lecture 1 6
Hardware
CPU
Input Output
devices Main memory devices
Secondary
memory
c1117 lecture 1 7
Software
Software that are frequently encountered in
programming
Operating system,
system Command prompt
prompt, Editor
Editor, Compiler
Compiler,
Debugger
Operating System
A program that is running as long as the computer is on
It acts as a manager to coordinate the resources of a
computer e.g. read data from disk, execute programs.
c1117 lecture 1 8
為了協助保護您的隱私權, PowerPoint 無法自動下載此外部圖片。若要下載並顯示此圖片,請按一下訊息列的 [選項 ],然後按一下 [啟用] 外部內容。
Window XP
desktop
c1117 lecture 1 9
More
Command prompt
A program that allows user to interact with the OS
to start or stop programs
Init by typing “cmd” in RUN for windows.
Editors
A word
word-processor
processor like program used to create our
programs, e.g. emacs, vi, pico, notepad, ultraEdit
c1117 lecture 1 10
Compilers
A program that translates the high-level program into
low-level
low level native code, which is understandable by a
computer
E.g. g++, gcc, vc++, pascal compiler, etc.
High-level language (such as C++) is closer to English
and is more human-readable.
int main() {
t width
int dt = 5;
int height = 10;
int area = width * height;
}
c1117 lecture 1 11
Low-level
Low level language
However,, a computer
p can onlyy understand pprogram
g
written in it’s low-level language (instructions), which
varies from different types of computers.
Th following
The f ll i are some extracted
t t d low-level
l l l instructions
i t ti
of the previous program in two different types of
computers
p (Pentium
( & Sparc).
p )
c1117 lecture 1 13
Steps for running a program
Compiler
Create a program Source code
Check the syntax
using an editor e.g. hello.cc
Library 1
Generate object
Library 2 code
……
Object code
e.g. hello.obj
Executable
E t bl Combine
C bi with
ith library
lib
e.g. hello.exe components by a linker
c1117 lecture 1 14
Steps for running a program
C t a program with
Create ith an Editor
Edit
Source code: Your program written in high-level
language (e.g.
(e g C++)
c1117 lecture 1 15
Steps for running a program
Link necessary library codes by linker
The object code produced by the compiler usually is
not enough,
enough e.g.
e g your program may need codes for
system input/output, which you don’t know how to
write,, but have been written byy some experts.
p The
linker program helps to link them to your program
automatically.
We seldom deal with linker in this course.
An executable is produced after linking.
Usually, compile-and-link in one single step.
c1117 lecture 1 17
A simple C++ program
Here is a simple C++ program, hello.cc
c1117 lecture 1 18
Layout of a C++ program
// Specifying
//…: p y g the Comments. The whole line is
ignored by the compiler.
A Comment line starts with a double slash and is valid
till the end of the line.
They make a program more readable.
#include <…>: Statement tells ll the
h compiler
l that
h
the program requires external library components.
U i library
Using lib can speed
d up development
d l t
int main(){…}: The main function. A essential
componentt off a C++ program.
When a program is put into execution, it starts at the
main function
c1117 lecture 1 19
Layout of a C++ program
cout: It is used to print the output to screen.
Imagine that cout is connected to screen, text to be
printed are inserted into cout using
p g <<,, the insertion
operator.
endl: It is used to p
produce a newline ((end of line).
)
return 0: Indicating the end of execution of the
main function,
function and the control is returned to OS
using namespace std: you will know about it
later
c1117 lecture 1 20
String literals
A string literal (or constant) is a sequence of
characters enclosed by a pair of double quotes
e g “This is a string”
e.g. string”, “Hello world!”
You can think literals as something not interpreted
b the
by th computer,
t it iis the
th same as what
h t we see.
cout << “Hello world!” << endl;
Meant prints the string Hello world! to the screen
followed by a newline (equivalent to press the “Enter”).
Hello world!
New line
c1117 lecture 1
Output of the program hello.cc
21
Layout of a simple C++ program
#include <iostream>
using namespace std;
int main () {
statement 1;
statement 2;
. . . . .
statement n;
return 0;;
}
c1117 lecture 1 23
Expressions
C++ statements usually contain expressions.
expressions
Expressions are composed of operators and
operands and they contain values.
values
e.g. 23*89/2,(2+4)*(30-99) (* -- multiplication in
programming language)
Radius*2*3.14159 (radius is called a variable)
Besides arithmetic operators
operators, there are logical
logical,
relational, bit shifting, etc.
S quote.cc as an example
See l in
i illustrating
ill t ti theth
different between quoted strings and simple
expressions.
i
c1117 lecture 1 24
Free formatting
The amount of whitespaces
p ((spaces/
p / newlines//
tabs) are not important in C++
You mayy add whitespaces
p at suitable p
places to make
your program easier to read.
For example, the program hello.cc can be written as
the following. It’s not easy to be read.
#include <iostream>
using namespace std;int main(){cout <<
"Hello World!" << endl; return 0;}
However for some cases
However, cases, space is important
important.
e.g. it is wrong to write "#in clude <iostream>"
W
Wrong area will
ill be
b calculated
l l d
c1117 lecture 1 27
Debugging Tool
A Debugger is a tool to help debugging programs
It is included in the IDE of netbeans/C++ (using
the gnu gdb debugger)
Features available
Set break points – stop the execution at a specified
place.
Si l step
Single t – execution
ti one statement
t t t att a ti
time
Variable watch – watch the content of variables.
Increase productivity significantly.
Debugger
gg is an indispensible tool for programmers,
g
and should learn to use it as early as possible.
c1117 lecture 1 28