You are on page 1of 14

ENGG1016 / ENGG1112 Computer Programming and Applications I Chapter 1 page 1

Introduction
Chapter 1 Introduction

Learning objective

Upon completion of this chapter, students will:
be aware of the need to acquire the skill of computer programming as an
engineering student
have a basic understanding of the hardware and software components of a
computer
have a basic knowledge on the steps to write a program and the tools for writing a
computer program


Reference:
Walter Savitch, Problem Solving with C++ 7/E, Addison-Wesley, 2009


Why should engineers learn computer programming?
1. A good engineer should be able to solve practical problems.
- Problem-solving means the ability to
formulate problems,
think creatively about solutions, and
express a solution clearly and accurately.
- Learning computer programming is an excellent way to develop
practical problem solving skills for an engineer.
ENGG1016 / ENGG1112 Computer Programming and Applications I Chapter 1 page 2
Introduction
2. Computers are used in a lot of engineering activities.
- Circuit design and analysis
- Product design and development
- Communications
- Facility layout
- Production scheduling
- Process simulation
- Enterprise resources planning
- Human resources planning
- Supply chain management
- Customer relations management
- and more
- Machine design
- Device control
- Process improvement
- Quality assurance
- Quality control
- Technology management
- CAD/CAM
- Graphics processing
- Market survey and analysis
- Logistics management
3. Computer is the best tool to process large amount of data correctly,
efficiently and automatically. Engineering activities usually have a
lot of data. Who write the program to process the data?
4. There may already have software on the market to do the jobs. But
what is software? Software is a collection of computer programs.
Engineers should have the knowledge of computer programming to
write programs or adapt the program to solve engineering problems.
An engineer should make sure the programs work correctly even if
the programs are written by somebody else.
5. Computers are widespread in our daily life, and they are not limited to
PCs. Machines, instruments, tools, and consumer electronic products
(like your MP3 players, CD players, mobile phones, iPads,
iPhones ) are all controlled by software embedded in programmable
circuitries that can be classified as computers.
6. To develop, design, produce and
supply these machines / instruments /
tools / devices requires engineers
from different disciplines. Engineers
should know the principles of
computer programming to facilitate
communications.
ENGG1016 / ENGG1112 Computer Programming and Applications I Chapter 1 page 3
Introduction
Why C / C++?


Source: The TIOBE Programming Community index

1. The TIBOE Programming Community index gives an indication of the popularity
of programming languages. (http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html)
2. The top four most popular languages in August 2012 are: C, Java, Objective-C
and C++.
C is the root language of the three top most popular languages.
C++ = C + object-oriented features.
Java has similar syntax as C and C++.
Objective-C = C + Object-oriented features of Smalltalk-style messaging
If an engineer has already mastered C and C++ languages, it will not be difficult
for him or her to understand programs written in Java and Objective-C.
ENGG1016 / ENGG1112 Computer Programming and Applications I Chapter 1 page 4
Introduction
What is a computer?
A computer is a machine that manipulates data according to the instructions given
by the users.
A computer can be divided into two parts: hardware and software.
The hardware part of a computer includes all the physical devices that make up
the computer.
All computers have memory devices. Although the memory devices belong to the
hardware part, the information stored inside the memory is regarded as the
software of the computer.

Computer hardware
Secondary
Memory
Input
Device(s)
Output
Device(s)
Processor
(CPU)
Main
Memory

A computer consists mainly of five main components: Processor, Main Memory,
Secondary Memory, Input Device(s) and Output Device(s).
Processor, or Central Processing Unit (CPU), is the central control of the whole
computer system. The CPU follows instructions in a program to control and
coordinate all operations, e.g., moving data from one memory location to another.
The CPU also performs arithmetic and logical operations on data, such as addition,
subtraction, multiplication, division, or comparison of numbers stored in memory.
Input devices are any hardware device that enables users to communicate with
the computer, for example, keyboard, mouse, and scanner.
ENGG1016 / ENGG1112 Computer Programming and Applications I Chapter 1 page 5
Introduction
Output devices are any hardware device that enables the computer to
communicate to the users. Monitor, printer and plotter are typical examples of
output devices.
Memory is the hardware component(s) inside a computer that can retain data or
information for the processor to manipulate. The basic unit of the memory is a
byte which is a collection of 8 bits. A bit is a binary unit representing a zero or a
one. In general, memory can be classified into two types: the main memory and
the secondary memory.
Main memory
Main memory is the memory devices that are directly connected to the CPU.
The program that is being executed is stored in the main memory.
The main memory also provides temporary storage for the CPU during program
execution.
Main memory consists of a sequence of storage locations called memory cells.
Each memory cell has a unique address.
The address of a memory cell is its relative position in the main memory.
Secondary Memory
Secondary memory is the memory devices that are used for keeping a permanent
record of information for the computer.
Hard disks, floppy disks, flash memory drives, CDs and DVDs are examples of
secondary memory.
Information in secondary memory is stored in units called files.
Programs stored in a file have to be moved into the main memory before it is
executed by the CPU.
Similarly, data stored in a file has to be moved into the main memory before it can
be manipulated by the CPU.
ENGG1016 / ENGG1112 Computer Programming and Applications I Chapter 1 page 6
Introduction
Computer software
A program is a set of instructions for a computer to execute.
Software can be regarded as the collection of programs used by a computer.
Software is usually stored in secondary memory, but it should be loaded into the
main memory before execution.
The most important piece of software inside a computer is the operating system.
Windows, DOS, LINUX, UNIX, Mac OS are examples of operating systems.
The main function of an operating system is to manage the computers resources
for different tasks. Conceptually, an operating system is a software layer located
between the computer hardware and application programs.

The basic unit of a program is an instruction. Examples:
1. Moves the content in memory location A to memory location B.
2. Increments the content in memory location A by 1.
A program is simply a collection of instructions. The instruction set of a CPU is the
set of all possible instructions that can be executed by the CPU.
If a program is written in the form of zeros and ones which are readily executed by
the CPU, the program is said to be written in machine language. Programs written
in machine language cannot be easily understood by a human being.
Assembly language is a direct translation from machine language in human
readable text. Instead of zeros and ones, assembly language is written in symbols
and alphabets.

Hardware
Operating
System
Application
Programs
ENGG1016 Computer Programming and Applications Chapter 1 page 7
Introduction
Examples of instructions written in assembly language:
1. MOV A B
2. INC A
Note: MOV and INC are short forms of the words move and increment
respectively. A and B are symbols to represent memory locations A and B.
Program written in assembly language is easier to be understood than machine
language.
To convert the program written in assembly language back to zeros and ones
executable by the machine, an assembler is needed.
An assembler is a program that translates the assembly language into machine
language for program execution in the computer.
Assembly language and machine language are called low-level language, which is
closely associated with the low level instructions of a computer.
To increase the efficiency in programming, it is necessary to provide a mean for a
programmer to program in a language resemble to the human language. High-level
languages are programming languages resemble to the human languages and are
easier for human being to read and write.
Examples of high-level programming languages: C, C++, Java, Visual Basic, Python,
Ruby and PHP.
A program written in low-level language is machine-dependent.
A program written in a high-level language can, theoretically, run on different
machines after compilation.
The process of converting a machine-independent high-level language into
machine-dependent machine language is called compilation. A compiler is a
program for compilation.
C and C++ are high-level programming languages and C++ is also an
object-oriented programming language. Object-oriented programming languages are
popular because software written in object-oriented language is easier to be reused.
In object-oriented programming, data and the program manipulating the data, called
methods, are combined into a single entity called an object. Properties of objects are
defined by classes.
ENGG1016 Computer Programming and Applications Chapter 1 page 8
Introduction
File System
Data and software are stored as files in the secondary memory of a computer.
Use meaningful names to represent the nature of the files.
In DOS or Microsoft Windows, file names are not case-sensitive, i.e., upper and
lower case are the same.
A file name contains two parts: name and extension. The name and the extension are
separated by a period, i.e., a dot sign.

f i l e n a m e . e x t
name extension

There is no limit in the number of characters used for both the name and the
extension of a filename but it is suggested to keep the name as short as possible but
not too short so that the file name can show the meaning representing the nature of
the file.
File extensions are used to represent the type of the file. For examples:
.txt
An ASCII text file
.exe
An executable file in Windows or DOS
.c
A C program
.cpp
A C++ program
.doc
A MS Word document
.pdf
A file in portable document format ( a file format created by Adobe Systems)
.jpg
A photo in JPEG format
Not all characters can be used for file names or extensions. In Windows, the
following characters are not allowed to be used in a file name:
\ / : * ? " < > |
A period to separate
name and extension
ENGG1016 Computer Programming and Applications Chapter 1 page 9
Introduction
Directory and Path
Files are organized and grouped in directories. A directory is a collection of files
and directories. In Microsoft Windows, a directory is called a folder.
Folder1
Folder2.1
Folder3
Folder2.2
disk C:\
file_a
file_b
c:\file.a
c:\file.b
c:\Folder1\f1
c:\Folder1\f2
c:\Folder1\Folder2.1\f4
c:\Folder1\Folder2.2\f3
c:\Folder1\Folder2.1\Folder3\f5
f1
f2
f5
f3
f4

ENGG1016 Computer Programming and Applications Chapter 1 page 10
Introduction
If A is a directory and B is another directory located in A, A is said to be the parent
directory of B and B is a sub-directory in A.
A
B

The directories in a file system form a tree structure and the topmost directory in the
tree is called the root directory.

C:
Program Files myFiles WINDOWS
Temp System32 Adobe
Acrobat 8.0
Adobe Help
Viewer
Borland
models
documents

The location of a file in a file system is specified by a path.
A path is the general form of a file name. For example, in Microsoft Windows, the
path of a file named report1.doc located in the directory documents is written as:
C:\myFiles\documents\report1.doc
C: is the root directory.
C: refers to the C drive.
myFiles is a sub-directory in C:.
documents is a sub-directory in myFiles.
A path is a character string that specifies the location of a file from the root directory
and the subdirectories containing the file to the name of the file. The directories and
file name are separated (or delimited) by backslash characters \.
Some operating systems use the slash / as the delimiter in the path.

The root directory
Parent directory of B
Sub-directory in A
ENGG1016 Computer Programming and Applications Chapter 1 page 11
Introduction
Windows Commands
Command prompt is a text-based interface that the user can control the computer by
typing text commands.
You can run executable programs by typing the filename of the program in a
command prompt.
You can also run Windows commands in a command prompt. Window commands
are executable programs built in the operating system.
Windows commands are case insensitive. If you want to copy a file, you can type
"copy" or "COPY". If you want to suspend a process, you can type "pause" or
"PAUSE".
Note: C / C++ programs are case sensitive.
The following table shows some commonly used Windows commands.

CLS Clears the screen.
COPY Copies one or more files to another location.
DEL Deletes one or more files.
DIR Displays a list of files and subdirectories in a directory.
HELP Provides Help information for Windows commands.
PATH Displays or sets a search path for executable files.
PAUSE Suspends processing of a batch file and displays a message.
RENAME Renames a file or files.
CD Change the current directory
EXIT Leave the command prompt
TYPE Displays the contents of a text file.

ENGG1016 Computer Programming and Applications Chapter 1 page 12
Introduction
How to write a program?
To use a computer, a program has to be written (normally in a high-level language,
such as C/C++). The program defines exactly what you want the computer to do. It
is then compiled into machine language and loaded into a computer to run.
The process to writing and compiling a program into an executable file that can run
on a computer is summarized in the following diagram.
Write program
using an
Editor
Compile in a
Compiler
Error? Yes
Combine object
codes in library
using a Linker
No
Source
code
Object code
Executable
file
Error?
Yes
No
Load to main
memory by Loader
library
Execute

An Editor is a program for the programmer to type in and edit a program. Notepad,
Notepad++, emac, vi, pico and UltraEdit are examples of editor.
The resulting file from an editor is called source file. The source file of a C++
program is usually named with the file extension .cpp in Windows/DOS or .cc
in Unix/Linux.
The source file is then translated into an intermediate file called object file. The
translation is done by a compiler. Usually, the object file is automatically assigned a
name same as the source file but with an extension of .obj in Windows/DOS or .o
in Unix/Linux.
ENGG1016 Computer Programming and Applications Chapter 1 page 13
Introduction
It is not necessary for all codes of the same program to be written by the same
person. Indeed, it is more efficient to write a program if object codes written and
tested by somebody else can be reused. In fact, some standard object codes, such as
codes for input and output, are already compiled and waiting to be combined into
your program. A collection of these pre-compiled object codes is called a library.
The process to combine all object codes into an executable file is called linking and
the program doing this process is called a linker.
The executable file is then loaded into the main memory of the computer by a
loader and run on the machine.
Usually, the executable file is automatically assigned a name same as the source file
but with an extension of .exe in Windows/DOS or .out in Unix/Linux.

What is an IDE?
Although the process of program development is separated into different
sub-processes, conceptually using different programs (editor, compiler, linker and
loader), programmers nowadays usually work in an integrated environment.
The software that combines the processes of program development (editing,
compiling, linking and loading) is called an Integrated Development Environment
(IDE).
Some IDEs also provide features that facilitate debugging. A bug means a mistake
in a program. To remove mistakes in a program is called debug. A program that
facilitates the process of debugging is called a debugger.
Examples of IDEs:
- Microsoft Visual C++ 2010: This is a window-based IDE and it is a commercial
product from Microsoft. A free version (Visual C++ 2010 Express) can be
downloaded from Microsoft's web site at
http://www.microsoft.com/visualstudio/eng/downloads#d-2010-express
- Bloodshed Dev-C++: This is a window-based IDE. It is an open source software.
Users can freely download, install and use the software under the GNU General
Public License agreement. A copy of the software can be downloaded from the
Internet at
http://www.bloodshed.net/dev/devcpp.html.
ENGG1016 Computer Programming and Applications Chapter 1 page 14
Introduction
The seven steps of programming

1. Define objectives
2. Design the program
3. Write the program
4. Compile the program
5. Run the program
6. Test and debug the
program
7. Maintain and modify the
program

You might also like