Computer Programming and Applications: Topic 1
Computer Programming and Applications: Topic 1
and Applications
Topic 1
Introduction to
Programming and C++
Basics
Program
Software
Hardware
Hardware
CPU
Input
devices
Main memory
Output
devices
Secondary
memory
ENGG1111A: Computer Programming and Applications
Input/output components
CPU
Input
devices
Main memory
Secondary
memory
ENGG1111A: Computer Programming and Applications
Output
devices
An output device allows
the computer to
communicate
information to the user.
A monitor is the most
common output device.
Internal hardware
components
CPU
Input
devices
Main memory
Output
devices
Secondary
memory
ENGG1111A: Computer Programming and Applications
Hardware components:
Memory
Main memory is used by an executing program. Its
contents are volatile which means they will be lost if,
for example, power is turned off.
Hardware components:
Main memory
Main memory provides temporary
storage for data and for the program
being executed.
It is arranged into numbered locations.
Each location is a byte (8 bits) or, in
some architectures, a word (multiple
bytes).
Data items can be stored in these
locations. If the item requires more than
one byte, it is stored in multiple
adjacent bytes.
The computer keeps track of what type
of data is encoded in each location
00000000
byte 1000
00110101
byte 1001
10001110
byte 1002
01111101
byte 1003
00000111
byte 1004
01101010
byte 1005
01000011
byte 1006
00001101
byte 1007
11100111
byte 1008
11000001
byte 1009
01100110
byte 1010
00000000
byte 1011
00000000
4 byte
location
2 byte
location
4 byte
location
Information is represented
as strings of 0s and 1s
byte 1000
00110101
byte 1001
10001110
byte 1002
01111101
byte 1003
00000111
byte 1004
01101010
byte 1005
01000011
byte 1006
00001101
byte 1007
11100111
byte 1008
11000001
byte 1009
01100110
byte 1010
00000000
byte 1011
00000000
4 byte location
with address
1000
2 byte location
with address
1004
4 byte location
with address
1006
Hardware components:
The CPU
Although this is at the heart of the machine, it simply
follows the instructions provided in a program and
executes the required operations.
10
Typical levels of
abstraction
Application level
High-order
language level
Assembly level
Operating
system level
Increasingly
machine-oriented
Instruction set
architecture level
Microcode level
Logic gate level
11
Software
We use and work with many types of software in
programming. These include:
Operating systems
Command line interpreters
Editors
Compilers
Development environments
Debuggers
12
13
14
Text editor
A word-processor-like program we use to write our own
programs. Examples include emacs, vi, pico, notepad, UltraEdit,
etc.
Some editors provide direct support for programming.
15
The compiler
A program, such as g++ or vc++, that translates a program
written in a high-level language like C++ into low-level
native code, which can be understood by a computer
16
int main()
{
int height = 4;
int width = 7;
int area = height * width;
return 0;
}
Let's compile this for a Pentium microprocessor and take a look at the relevant
assembly-level instructions generated by the compiler.
mov
mov
mov
imul
mov
17
Machine code
The assembly language instructions on the previous slide are still too highlevel to be understood by the computer. They must be converted into
strings of 0 and 1 that the computer can understand. This final form is
machine code.
The machine code for our example is shown on the left:
4-byte
instruction:
C7 45 FC 04 00 00 00 mov
C7 45 F8 07 00 00 00 mov
8B 45 FC
mov
0F AF 45 F8
imul
89 45 F4
mov
00001111
10101111
01000101
11111000
18
Application level
High-order
language level
Assembly level
Operating system
level
Object program
Machine
dependent
Instruction set
architecture level
Microcode level
19
The executable
If we ask the compiler to create an executable machine code program
from our simple source code, we'll end up with a file which is much larger
than the compiled version of our source code.
size of the
each file
09/03/2014
09/03/2014
09/03/2014
02:45 PM
02:46 PM
02:46 PM
102
460
15,451
area.cpp
area.o
area.exe
If we take a look, we'll see there are a lot of instructions there that we
didn't write ourselves.
How did they get there? They are combined with our code in the final
stage of creating an executable: linking.
ENGG1111A: Computer Programming and Applications
20
Source code
Compiler
e.g. area.cpp
21
Source code
e.g. area.cpp
Compiler
Check the syntax
Generate object
code
22
23
Compiler
Source code
e.g. area.cpp
Generate object
code
Linker
Object code
e.g. area.o
Executable
e.g. area.exe
Run
ENGG1111A: Computer Programming and Applications
Library 1
Library 2
Library 3
24
IDE
Software that integrates the steps of developing a program (e.g.
editing, compiling, linking, debugging) is called an Integrated
Code::Blocks
In this course well use Code::Blocks - a cross-platform IDE for
programming
This software is free and can be easily downloaded using the
links well provide
Our next class introduces Code::Blocks
25
Computer Programming
and Applications
Programming in C++
Our code
// hello.cpp
// This program displays Hello World!
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
27
Comments
Information for
human readers
// hello.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
/* This is a
// hello.cpp
// This program displays Hello World!
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
29
// hello.cpp
// This program displays Hello World!
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
30
// hello.cpp
// This program displays Hello World!
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
usingnamespacestd;
ENGG1111A: Computer Programming and Applications
31
int main(){...}
Every C++ program must contain
a main function
The braces { and } mark the
beginning and end of the function.
Our code
// hello.cpp
// This program displays Hello World!
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
32
Statements
Our code
// hello.cpp
Between the braces, the main function // This program displays Hello World!
contains a sequence of statements
the instructions that the computer will
#include <iostream>
execute one by one.
using namespace std;
Each statement MUST end with a
semi-colon.
int main()
{
cout << "Hello World!" << endl;
return 0;
}
33
Our code
iostream
cout is used to construct output
statements which will write output to
the monitor screen.
// hello.cpp
// This program displays Hello World!
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
34
String literals
double quotes
Our code
// hello.cpp
// This program displays Hello World!
e.g. "Thisisastring"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
35
Our code
// hello.cpp
// This program displays Hello World!
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
36
Source code
Compiler
Check the syntax
hello.cpp
Generate object
code
Linker
Object code
hello.o
Executable
hello.exe
iostream
Run
ENGG1111A: Computer Programming and Applications
37
10:20
10:20
10:20
10:20
PM
PM
PM
PM
<DIR>
<DIR>
.
..
167 hello.cpp
1,406 hello.o
Creating an executable
Our output file
will be executable
and so we name it
appropriately
Directory of D:\ENGG1111A
09/07/2014
09/07/2014
09/07/2014
09/07/2014
09/07/2014
10:23 PM
<DIR>
.
10:23 PM
<DIR>
..
10:20 PM
167 hello.cpp
10:23 PM
1,001,851 hello.exe
10:20 PM
1,406 hello.o
3 File(s)
1,003,424 bytes
2 Dir(s) 30,894,530,560 bytes free
D:\ENGG1111A>hello
Hello World!
D:\ENGG1111A>
It works!!
Displays the string
and a new line
39
In Code::Blocks
40
In Code::Blocks after F9
41
Programming errors
Sometimes we make mistakes when programming. These errors
introduce defects, also called bugs, into the program
The process of getting rid of the bugs is called debugging. We
have a lab session on debugging using the IDE later in the course.
There are three main types of programming error:
Syntax errors
Run-time errors
Logic errors
43
Syntax errors
These are caused by violating the grammar rules of the language.
The compiler will spot syntax errors. It will give you an error
message and terminate. It can't proceed because it doesn't know
what you mean.
Syntax errors
45
Run-time errors
These errors are detected when you run the program, hence at run-time.
Execution will typically stop and the run-time system will output an error
message if it can.
For example, consider a program containing the following fragment where
the 3 marks and the number of assignments submitted (all integers) have
been supplied as inputs:
46
Run-time errors
What you will see depends on the run-time environment:
$ ./average.exe
How many assignments submitted? 2
Enter marks: 55 0 75
Average per submission: 65
$ ./average.exe
How many assignments submitted? 0
Enter marks: 0 0 0
Floating point exception (core dumped)
$ more average.exe.stackdump
Exception: STATUS_INTEGER_DIVIDE_BY_ZERO at eip=00401244
D:\ENGG1111A>average
How many assignments submitted? 2
Enter marks: 55 0 75
Average per submission: 65
D:\ENGG1111A>average
How many assignments submitted? 0
Enter marks: 0 0 0
47
Logic errors
Even if a program compiled successfully and there are no run-time errors,
it does not mean that the program is correct.
For example there may be mistakes in the solution you developed or you
may not have implemented that solution correctly in C++.
The effect will be a program that runs, yet produces an incorrect result.
The program contains logic errors.
Logic errors are difficult to detect because we get no help in terms of error
messages from the compiler or at run-time.
48
49