CMP102 - Lecture Note - OD
CMP102 - Lecture Note - OD
FACULTY OF SCIENCES
DEPARTMENT OF MATHEMATICAL
SCIENCES
Introduction
Potential students of this class are welcomed. CMP102 is a foundational course for students
studying towards acquiring the Bachelor of Science in Computer Science degree. In this
course we would study data structure and the basic algorithms for manipulating them.
The aim of the course is to introduce students to programming and computational problem
solving and techniques. Topics relating to Basics of programming, flow charts and algorithms
are discussed. A bottom-up approach was adopted in structuring this course. It starts from the
building blocks of programming, then the fundamental principles of problem-solving using
computers.
This course is about learning to solve problems, not learning facts. This course compulsory
for all students in the Department of Computer Science and elective for other students in the
faculty of science. In view of this, students are expected to participate in all the activities and
have a minimum of 75% attendance to be able to write the final examination. The course is
divided into 13 study sessions or modules.
Course Aim
This course aims to provide foundation to students with an understanding on how ot solve
computational problems using computer programs.
Course Objectives
It is important to note that each session has specific objectives. Students should study them
carefully before proceeding to subsequent sessions. Therefore, it may be useful to refer to
CMP102: Introduction to Programming
these objectives in the course of your study of the session to assess your progress. You should
always look at the unit objectives after completing a session. In this way, you can be sure that
you have done what is required of you by the end of the study session.
1. Describe the basic operations on stacks, lists and queue data structures
2. Explain the notion of trees, hashing and binary trees
3. Discuss the underlying principles of data types: lists, stacks and queues
4. Describe structures and algorithms for external storage: external sorting, external
search trees.
5. Identify directed and undirected graphs
6. Discuss sorting: internal and external sort
7. Efficiency of algorithms, recursion and recursive programmed
8. Discuss the algorithm design techniques: greedy algorithms, divide and conquer,
dynamic programming
Recommended Texts
The following texts and Internet resource links will be of enormous benefit to you in learning
this course:
Assessment
There are two aspects to the assessment in this course: tutor-marked assignments and written
examination. Therefore, you are expected to take note of the concepts, information and
problem-solving procedure gathered during the course. The tutor-marked assignments must
be submitted to your tutor for formal assessment in accordance to the deadline given. The
work submitted will count for 30% of your total course mark. At the end of the course, you
will need to sit for a final written examination. This examination will account for 70% of
your total score. You will be required to submit some assignments by uploading them to
CMP 206 page on the LMS.
You should endeavour to attend the tutorials. This is the only opportunity to have face-to-face
contact with your tutor and ask questions which are answered instantly. You can raise any
problem encountered in the course of your study. To gain the maximum benefit from the
course tutorials, have some questions handy before attending them. You will learn a lot from
participating actively in
CMP102: Introduction to Programming
Study Session One: Introduction to Computers and
Programming
Introduction
The set of instructors that control the computer is called a program. A program can be
executed by the computer only when it is stored in the computer’s memory and is in
machine language code.
Objectives
When you have studied this session, you should be able to:
1. Understand the concept of data type, abstract datatype and data structure.
Content
What is computer
Computers are automatic machines that can accept data, store vast amounts of
information and perform arithmetic at high speeds to solve complex machines
Computer program
The computer may be thought of as a system composed of five components. The
components and their functions are as follows:
1. The Input Unit: This unit feeds information from the outside world to the
computer. Input units are capable of reading information recorded on such
different mediums as punched cards or magnetic tape or from terminals. The
information read from these devices is placed into appropriate memory locations.
2. The Memory Unit: The memory unit stores information. It holds the sequence of
instructions necessary to solve particular problem and any additional data
required. The memory is divided into locations that each have an address (are
addressable). Instructions are stored in these cells.
3. The Control Unit: The control unit fetches the instructions and data from
memory and executes the instructions one at a time with logical unit. All of the
CMP102: Introduction to Programming
other components operate as directed by the control unit.
4. The Arithmetic/Logical Unit: The arithmetic/ logical unit consists of the
electronic circuitry that performs arithmetic operations such as comparison of
numbers.
5. The Output Unit: The output unit transfers or copies the contents of certain
memory locations onto some external medium such as punched cards, punched
paper tape, magnetic tape, a printed page produced by a teletype or line printer, or
a cathode ray tube (CRT) screen for visual display.
The memory, control and arithmetic/ logical units are collectively called the
central processing unit (CPU).
Interpreters
Some languages are interpreted. The translator processes the source code line by line and
runs every line in the final program or app. This means that interpreted source code starts
running until it encounters an error. Then the interpreter stops to report such errors. Python is
a good example of an interpreted programming language.
Compilers
Compilers function differently. They convert the source code in its entirety via a compilation
process to binary. The binary is then executed. If there were errors in the source code, they
are detected during the compilation time and flagged. This interrupts the compilation process,
and no binary is generated.
Sample program
1. INPUT A
2. INPUT B
3. LET C = A + B
4. PRINT C
5. END
CMP102: Introduction to Programming
PROGRAMMING TOOLS
Definition of Algorithm
To write a logical step-by-step method to solve the problem is called algorithm, in other
words, an algorithm is a procedure for solving problems. In order to solve a mathematical or
computer problem, this is the first step of the procedure. An algorithm includes calculations,
reasoning and data processing. Algorithms can be presented by natural languages, pseudo
code and flowcharts, etc.
Definition of Flowchart
Input / Output
Process / Instruction
Decision
Connector / Arrow
CMP102: Introduction to Programming
The graphics above represent different part of a flowchart. The process in a flowchart can be
expressed through boxes and arrows with different sizes and colours. In a flowchart, we can
easily highlight a certain element and the relationships between each part.
Now that we have the definitions of algorithm and flowchart, how do we use a flowchart to
represent an algorithm?
Algorithms are mainly used for mathematical and computer programs, whilst flowcharts can
be used to describe all sorts of processes: business, educational, personal and of course
algorithms. So, flowcharts are often used as a program planning tool to visually organize the
step-by-step process of a program. Here are some examples:
Algorithm:
Step 1: Initialize X as 0,
Step 2: Increment X by 1,
Step 3: Print X,
Flowchart:
CMP102: Introduction to Programming
Algorithm:
Step 3: Print C,
Flowchart:
CMP102: Introduction to Programming
Conclusion
From the above we can come to a conclusion that a flowchart is pictorial representation of an
algorithm, an algorithm can be expressed and analysed through a flowchart.
An algorithm shows you every step of reaching the final solution, while a flowchart shows
you how to carry out the process by connecting each step. An algorithm uses mainly words to
describe the steps while a flowchart uses the help of symbols, shapes and arrows to make the
process more logical.
Assignment
Objectives
When you have studied this session, you should be able to:
Content
The early design and the first version of PureBasic started in 1998 and it was designed by Frédéric
Laboureur. The main differences between PureBasic and ‘normal’ compilers then was the inclusion of
a ‘virtual processor’ (which actually used the 680x0 assembly mnemonics) right from the start to
allow different kinds of assembly output (or any language) possible without changing the compiler
core. After the initial design was finished and programming began, things started to move very fast.
PureBasic files are saved as .pb.
The IDE
The IDE (Fig. 1) is written entirely in PureBasic and is the main tool used to write PureBasic
programs, so let’s take a tour around the main interface of the editor.
At the top is the menu bar giving access to the menu commands, below that is a customizable toolbar
which can be configured to hold many different icons each triggering a pre-defined menu command.
Underneath the toolbar on the left is the main editor pane, here all PureBasic code is entered.
On the right of the editor pane is another user customized pane which can contain a procedure viewer,
a variable viewer, a file explorer, etc...
Also, by default the error/compiler activity log will be shown as a pane attached to the bottom of the
editor pane, this can be turned off and on via a menu command (Menu: Debugger->Error Log->Show
Error Log).
Below the panes there is the status bar which shows information on what row and column is being
edited and displays the quick help.
The Compiler
This editor is a ‘front-end’ to the actual PureBasic compiler. Once code has been entered into the
editor pane and the ‘Compile/Run’ toolbar button has been pressed (Shortcut Key: F5) the code is
then passed to the compiler for the creation of an executable.
When selected the text file is immediately compiled into a temporary executable file called
‘purebasic0.exe’ within the ‘/PureBasic/Compilers’ folder and that executable file is then run. This is
handy when you need to see immediately how your program runs, without specifying a proper name
for the compiled executable. If this temporary program file is still running while another program is
compiled and run, then a new temporary file will be created called ‘purebasic1.exe’, and so on. If,
however the first program has ended while using the ‘Compile/Run’ method, then the compiler reuses
old numbers on the temporary files and keeps on using ‘purebasic0.exe’ as the temporary name. This
makes sure that there aren’t huge numbers of temporary executable files within the
‘/PureBasic/Compilers’ folder.
The last IDE method, using the ‘Compiler->Create Executable...’ menu command is for when all
development is finished on the current program and you are ready to compile and deploy your
executable file. Once this menu command has been selected a dialog box appears asking you to specify
a name and location for your final executable program file.
The Debugger
The PureBasic debugger is a tool which controls the execution of the program you’re working on. It
controls and keeps track of all variables and procedure parameters, etc. as the program is running. It
is a second pair of eyes on your code to highlight errors and to avoid potential program crashes such
as zero division, illegal array offset access and data overflow errors. It can also be used as a brake to
halt the program at any given time to see what actual variable values are. The program execution can
be stopped and forwarded step by step to locate any faults or strange behaviour. This can be very
useful especially if a program falls into an endless loop or you are unsure of what value a particular
variable holds at any given time.
OpenConsole()
Print("Hello World")
Print("My name is Taiwo")
Input()
End
The compiler processes the text file from the top to the bottom. It’s that easy. Commands written at
the top of the file are processed before the ones written below, just as if you were reading the file
yourself. If the debugger spots a problem the compilation is stopped and an error is raised.
Assignment