Computer Programming
Using
Course Code: CoSc 1012
March-2025
Chapter I
Overview of Computer and Programming
2
Course Content
• Topic one: An overview of computer and programming
• Topic two: Basic elements of C++
• Topic three: C++ Control Statements
• Topic Four: Arrays, Strings and Pointers
3
Course Objectives
Introduce basic Computer Systems, and programs employing
them
Provide an introduction to the concepts of problem solving and
developing algorithms
Provide a grounding in the principles of programming, using
the C++ programming language
Provide an introduction to Structural programming
Introduce algorithms using sequencing, conditional and
iterative constructs;
4
Elements of a Computer
System
• Hardware
• CPU
• Main memory
• Secondary storage
• Input/Output devices
• Software
5
Hardware
• CPU
• Main memory: RAM
• Input/output devices
• Secondary storage
6
CPU (Central Processing
Unit)
• CU (Control Unit):
– Fetches and decodes instructions
– Controls flow of information in and out of MM
– Controls operation of internal CPU components
• PC (program counter): points to next instruction to be
executed
7
CPU (Central Processing
Unit) (continued)
• IR (instruction register): holds instruction currently being
executed
• ALU (arithmetic logic unit): carries out all arithmetic and logical
operations
8
9
Fig: Hardware Components of a Computer
Main Memory
• Directly connected to the CPU
• All programs must be loaded into main memory before they
can be executed
• All data must be brought into main memory before it can be
manipulated
• When computer power is turned off, everything in main
memory is lost
10
11
Secondary Storage
• Secondary storage: device that stores information permanently
• Examples of secondary storage:
– Hard disks
– Flash drives
– Floppy disks
– Zip disks
– CD-ROMs
– Tapes
12
Input/Output Devices
• Input devices feed data and programs into computers; they
include:
– Keyboard
– Mouse
– Secondary storage
• Output devices display results; they include:
– Monitor
– Printer
– Secondary storage
13
Software
• Software: programs that do specific tasks
• System programs take control of the computer, such as an
operating system
• Application programs perform a specific task
– Word processors
– Spreadsheets
– Games
14
The Language of a
Computer
• Digital signals are sequences of 0s and 1s
• Machine language: language of a computer
• Binary digit (bit):
– The digit 0 or 1
• Binary code:
– A sequence of 0s and 1s
• Byte:
– A sequence of eight bits
15
16
Coding Schemes
• ASCII (American Standard Code for Information Interchange)
– 128 characters
– A is encoded as 1000001 (66th character)
– 3 is encoded as 0110011
17
Base-N Number System
• Base N
• N Digits: 0, 1, 2, 3, 4, 5, …, N-1
• Example: 1045N
• Positional Number System
• n 1 4 3 2 1 0
N L N N N NN
dn1 L d4 d3 d2 d1 d0
• Digit do is the least significant digit (LSD).
• Digit dn-1 is the most significant digit (MSD).
18
Decimal Number System
• Base 10
• Ten Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
• Example: 104510
• Positional Number System
n 1 4 3 2 1 0
10 L 10 10 10 10 10
dn1 L d4 d3 d 2 d1 d0
• Digit d0 is the least significant digit (LSD).
• Digit dn-1 is the most significant digit (MSD).
19
Binary Number System
• Base 2
• Two Digits: 0, 1
• Example: 10101102
• Positional Number System
n 1 4 3 2 1 0
2 L 2 2 2 22
bn1 L b4 b3 b2 b1 b0
• Binary Digits are called Bits
• Bit bo is the least significant bit (LSB).
• Bit bn-1 is the most significant bit (MSB).
20
Definitions
• nybble = 4 bits
• byte = 8 bits
• (short) word = 2 bytes = 16 bits
• (double) word = 4 bytes = 32 bits
• (long) word = 8 bytes = 64 bits
• 1K (kilo or “kibi”) = 1,024
• 1M (mega or “mebi”) = (1K)*(1K) = 1,048,576
• 1G (giga or “gibi”) = (1K)*(1M) = 1,073,741,824
21
Hexadecimal Number
System
• Base 16
• Sixteen Digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
• Example: EF5616
• Positional Number System
• n1 4 3 2 1 0
16 L 16 16 16 16 16
0000 0 0100 4 1000 8 1100 C
0001 1 0101 5 1001 9 1101 D
0010 2 0110 6 1010 A 1110 E
0011 3 0111 7 1011 B 1111 F
22
Binary Addition
•Single Bit Addition Table
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10 Note “carry”
23
Hex Addition
• 4-bit Addition
4 + 4 = 8
4 + 8 = C
8 + 7 = F
F + E = 1D Note “carry”
24
1’s Complements
• 1’s complement (or Ones’ Complement)
– To calculate the 1’s complement of a binary number just “flip”
each bit of the original binary number.
– E.g. 0 1 , 1 0
– 01010100100 10101011011
25
2’s Complements
• 2’s complement
– To calculate the 2’s complement just calculate the 1’s
complement, then add 1.
01010100100 10101011011 + 1=
10101011100
– Handy Trick: Leave all of the least significant 0’s and first
1 unchanged, and then “flip” the bits for all other digits.
• Eg: 01010100100 -> 10101011100
26
Coding Schemes
(continued)
• EBCDIC
– Used by IBM
– 256 characters
• Unicode
– 65536 characters
– Two bytes are needed to store a character
27
ALGORITHMS AND FLOWCHARTS
A typical programming task can be divided into two
phases:
Problem solving Phase
produce an ordered sequence of steps that describe solution of
problem
this sequence of steps is called an algorithm
Implementation Phase
implement the program in some programming language
28
Steps in Problem Solving
First produce a general algorithm (one can use
pseudocode)
Refine the algorithm successively to get step by step
detailed algorithm that is very close to a computer
language.
Pseudocode is an artificial and informal language
that helps programmers develop algorithms.
Pseudocode is very similar to everyday English.
29
Pseudocode & Algorithm
Example 1: Write an algorithm to determine a student’s final
grade and indicate whether it is passing or failing. The final
grade is calculated as the average of four marks.
30
Pseudocode & Algorithm
Pseudocode:
Input a set of 4 marks
Calculate their average by summing and dividing by 4
if average is below 50
Print “FAIL”
else
Print “PASS”
31
Pseudocode & Algorithm
Detailed Algorithm
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
32
The Flowchart
(Dictionary) A schematic representation of a sequence of
operations, as in a manufacturing process or computer
program.
(Technical) A graphical representation of the sequence of
operations in an information system or program.
Information system flowcharts show how data flows from source
documents through the computer to final distribution to users.
Program flowcharts show the sequence of instructions in a single
program or subroutine. Different symbols are used to draw each
type of flowchart.
33
The Flowchart
A Flowchart
shows logic of an algorithm
emphasizes individual steps and their interconnections
e.g. control flow from one action to the next
34
Flowchart Symbols
Name Symbol Use in Flowchart
Oval Denotes the beginning or end of the program
Parallelogram Denotes an input operation
Rectangle Denotes a process to be carried out
e.g. addition, subtraction, division etc.
Diamond Denotes a decision (or branch) to be made.
The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)
Hybrid Denotes an output operation
Flow line Denotes the direction of logic flow in the program
35
Example
START
Input Step 1: Input M1,M2,M3,M4
M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE <50) then
Print “FAIL”
GRADE(M1+M2+M3+M4)/4 else
Print “PASS”
endif
N Y
IS
GRADE<50
PRINT PRINT
“PASS” “FAIL”
STOP
36
What is a Programming
Language?
A programming language is a notational system
for describing computation in machine-readable and
human-readable form.
Most of these forms are high-level languages,
which is the subject of the course.
Assembly languages and other languages that are
designed to more closely resemble the computer’s
instruction set than anything that is human-readable
are low-level languages. 37
Why Study Programming
Languages?
In 1969, Sammet listed 120 programming languages
in common use – now there are many more!
Most programmers never use more than a few.
Some limit their career’s to just one or two.
The gain is in learning about their underlying design
concepts and how this affects their implementation.
38
The Six Primary Reasons
Increased ability to express ideas
Improved background for choosing appropriate
languages
Increased ability to learn new languages
Better understanding of significance of
implementation
Better use of languages that are already known
Overall advancement of computing 39
Tiobe
Index
40
Programming Domains
Scientific Applications
Business Applications
Artificial Intelligence
Web Software
41
Numerically-Based Languages
Many of the earliest computers were used almost exclusively
for scientific calculations and consequently many of the earliest
attempts at languages were for scientific purposes.
Grace Murray Hopper’s A-0 and John Backus’s Speedcoding
ere designed to compile simple arithmetic expressions.
42
FORTRAN
John Backus’s team at IBM developed FORTRAN (for
FORmula TRANslator) in 1955-1957.
While FORTRAN was designed for numerical
computation, it included control structures, conditions
and input/output.
FORTRAN’s popularity led to FORTRAN II in 1958,
FORTRAN IV in 1962, leading to its standardization in
1966, with revised standards coming out in 1977 and
1990. 43
Artificial Intelligence
Artificial Intelligence deals with emulating human-style
reasoning on a computer.
These applications usually involve symbolic
computation, where most of the symbols are names
and not numbers.
The most common data structure is the list, not the
matrix or array as in scientific computing and not the
record as in business computing
Artificial intelligence requires more flexibility than other
programming domains. 44
Artificial Intelligence
Languages
The first AI language was IPL (International Processing
Language, developed by the Rand Corporation. Its low-level
design led to its limited use.
John McCarthy of MIT developed LIST for the IBM 704 (which
eventually led to Scheme and Common LISP). LISP is a
recursion-oriented, list-processing language that facilitated
game-playing programs.
Yngve of MIT developed COMIT, a string-processing language,
which was followed by AT&T’s SNOBOL.
Prolog was developed by Colmerauer, Roussel and Kowalski
based on predicate calculus and mathematical logic. 45
Systems Languages
Assembly languages were used for a very long time operating
systems programming because of its power and efficiency.
CPL, BCPL, C and C++ were later developed for this purpose.
Other languages for systems programming included PL/I,
BLISS, and extended ALGOL.
46
Web Software
Eclectic collection of languages:
Markup (e.g., HTML) – used for annotating a document in a
manner that can be distinguished from the text.
Scripting (e.g., PHP) - the language that enable the script to
run these commands and typically include control structures
such as if-then-else and while-do.
General-purpose (e.g., Java) – can be used for a wide range
of programming jobs.
47
Language Evaluation Criteria
Readability – the ease with which programs can be read and
understood.
Writability – the ease with which programs can be developed
for a given program domain.
Reliability – the extent to which a program will perform
according to its specifications.
48
The Evolution of
Programming Languages
Early computers were programmed in machine language
To calculate wages = rates * hours in machine language:
100100 010001 //Load
100110 010010 //Multiply
100010 010011 //Store
49
Assembly Language
Assembly language instructions are mnemonic
Assembler: translates a program written in assembly language
into machine language
50
Assembly Language
(continued)
Using assembly language instructions,
wages = rates • hours can be written as:
LOAD rate
MULT hour
STOR wages
51
High-Level Languages
High-level languages include Basic, FORTRAN, COBOL, Pascal,
C, C++, C#, and Java
Compiler: translates a program written in a high-level language
machine language
The equation wages = rate • hours can be written in
C++ as:
wages = rate * hours;
52
Programming Evolution
53
Calculate :
Machine Language
Assembly Language
C++
54
What is a program ?
A program is a sequence of instructions that specifies how
to perform a computation and written in a language the
computer understands.
The computation might be something mathematical, such
as solving a system of equations or finding the roots of a
polynomial.
But it can also be a symbolic computation, such as searching
and replacing text in a document or (strangely enough)
compiling a program.
55
Programming: a way of
Thinking
Combines features from mathematics, engineering, and natural
science.
Like Mathematicians: computer scientists use formal languages
to denote ideas (specifically computations).
Like Engineers: they design things, assembling components into
systems and evaluating tradeoffs among alternatives.
Like Scientists: they observe the behavior of complex systems,
form hypotheses, and test predictions.
The single most important skill for a computer scientist is problem
solving. Problem solving means the ability to formulate problems,
think creatively about solutions, and express a solution clearly and
accurately.
56
Compiler & Interpreter
57
Their Difference
58
Brief History of C++
C++, with its robust syntax and performance-centric approach,
stands as a pivotal cornerstone in the world of modern
programming.
Engineered for software where efficiency and flexibility are
critical, it has deeply influenced the development of operating
systems, game engines, and even other programming
languages.
Originating from the need to enhance the C programming
language, C++ introduced object-oriented features that would
later define and dominate software design paradigms. 59
Brief History of C++
60
Brief History of C++
61
Cont..
C++ is a general-purpose programming language that was
developed as an enhancement of the C language to include
object-oriented paradigm.
It is an imperative and a compiled language with the following
seven key characteristics
1. C++ is a high-level, general-purpose programming language
designed for system and application programming. And it is an
object-oriented, multi-paradigm language that supports
procedural, functional, and generic programming styles.
62
Cont…
2. One of the key features of C++ is its ability to support low-level,
system-level programming, making it suitable for developing
operating systems, device drivers, and other system software.
And used for developing desktop applications, video games,
and other complex applications.
3. C++ has a large, active community of developers and users,
and a wealth of resources and tools available for learning
and using the language.
4. C++ supports object-oriented programming, allowing
developers to create classes and objects and to define
63
Cont…
5. C++ templates allow developers to write generic code that
can work with any data type, making it easier to write
reusable and flexible code.
6. The Standard Template Library (STL) provides a wide range
of containers and algorithms for working with data, making it
easier to write efficient and effective code.
7. C++ provides robust exception handling capabilities, making
it easier to write code that can handle errors and unexpected
situations.
64
Phases of Program
Development in C++
65
Cont…
66
Cont…
67
Cont…
68
Cont…
69
Cont…
70
Cont…
71
Summary
72
Cont…
73
Cont…
74
Cont…
75
Cont…
76
Cont…
77
Cont…
78
Cont…
79
Artificial Intelligence
80
Cont…
81
Cont…
82
Cont…
83
Cont…
84