Lectures PF 1
Lectures PF 1
Note: Documents provided are only to be used as helping materials we will focus more on Conceptual Based Learning
What is Computer?
• A computer is a machine that
– inputs (takes in) facts and information (known as data*)
– then processes (does something to or with) it
– can also store data
– afterwards it outputs, or displays, the results for you to see
* Data is all kinds of information, including, pictures, letters, numbers, and sounds
Minicomputers
o They are desk size machines.
o They fall between microcomputers and mainframe computers in their processing
speeds and data-storing capabilities.
o They might be used for research or monitoring a particular manufacturing process.
Smaller companies have been using minicomputers for their data processing needs such as
accounting/billing systems
Mainframe Computers
o These are large computers.
o They are capable of great processing speeds and data-storing.
o They are used by large organizations – business, banks, government agencies etc –
to handle millions of transactions.
o For example, airline companies use mainframe computers to process information
about millions of travelers.
Supercomputers
o The most powerful type of computer is the supercomputer.
o These machines are special, high-capacity computers used by very large
organizations.
o For example, NASA uses supercomputers to track and control space explorations.
o Supercomputers are also used for oil exploration, simulations and worldwide
weather forecasting.
Computer: Peripherals
• A peripheral device is an equipment that might be added to a computer system to enhance
its functionality
– Printer
– Digital camera
– Scanner
– Projector
– Joystick
– Graphics tablet
Introduction to Problem Solving
Definition “Problem”
A problem is generally considered to be a task, a situation or a certain scenario which is
difficult to deal with or control due to certain complexity.
Example: Arranging a set of 10 000 random numbers in ascending order.
Write a Program that displays a structure of N (odd) asterisk, similar to the image shown
below (Program 2):
A brief review of Von-Neumann architecture
The Von Neumann architecture is a standard design of computer system with different entities
connected over a bus:
I/O Devices
Inputs are the signals or data received into the computer system from the outside world via input
devices. The data is stored in internal registers. Outputs are the signals or data returned from the
computer system to the outside world via output devices. Together these peripheral devices form
the I/O unit.
ALU
This calculates the arithmetic logic of instructions from programs (carries out calculations on the
data) needed by programs. It can carry out simple calculations like
adding/subtracting/multiplying/dividing and make logic operations like `greater than’ or `less than’
or `equal to’. It also acts as a conduit for input and output to and from the processor.
Control Unit
This controls the flow of data through the processor. It manages the execution of machine code by
sending control signals to the rest of the computer. Control signals are sent over the control bus to
connected devices like hard drives and graphics cards. The control unit synchronises instructions to
the internal clock speed. Some instructions can take less than one clock cycle to complete but the
CPU will only start on the next instruction once the clock cycle is over. The control unit command for
instructions and data to be fetched by assigning control signals to the memory registers. Once the
instruction is decoded it sends more signals to the hardware to execute it (i.e. it tells the computers’
memory, ALU and I/O devices how to respond to an instruction).
Memory unit
The memory unit is responsible for fetching data and instructions from the main memory and pulling
them into registers needed for the data to be processed. An instruction can only run on data within a
register so the memory unit is responsible for fetching from and saving to memory. Therefore, the
memory unit makes use of what is known as the fetch-decode-execute cycle.
Processor speed
This is measured by the number of clocks cycles a processor can perform per second and is
measured in hertz (Hz). Clock cycle = One increment of the CPU clock (fetch-decode-execute a single
instruction)
Bus types
The bus is a set of parallel wires connecting two or more components of a computer. It typically
consists of 8, 16, 32 or 64 lines. They connect all the components (I/O devices) to the CPU and RAM
and allow the transfer of data between the components.
These 3 buses are commonly collectively called the system bus. Each bus is a shared transmission
medium, so only one device can transmit along the bus at any given time. (The von Neumann
architecture has only one data bus so data transfers and instruction fetches are scheduled: They
cannot run at the simultaneously).
The main idea the Von Neumann architecture introduced was that not only the data but also the
program processing the data should also be stored in the memory. This made it easy to re-program
the computer.
The control bus is bidirectional so signals can be transferred in both directions. The data and address
buses are shared by all components in the system. Control lines must, therefore, be provided to
ensure access and use of the data and address busses by different components does not conflict.
Data Representation
Definition
Modern computers are digital devices
– A digital device works with discrete data, such as the digits 1 and 0
– An analog device works with continuous data
Just as a standard light switch is a simpler technology than a dimmer, so is digital when compared to
analog
Why Data Representation is Important
Number System
• We use decimal number system [base 10] when representing numeric values in our daily life
– E.g., consider the number 123
• 123 = 100 + 20 + 3 = 1*102 + 2 * 101 + 3*100
• Most modern computer systems do not use the decimal system to represent numeric
values.
– Instead, they use a binary numbering system [base 2]
• Consisting of only two digits: 1 and 0
• Uses powers of 2 rather than 10
Representing Non-Numeric Data
• Some decimal values & their binary equivalents
• In addition to numeric data, computers also
manipulate character data
• numbers, symbols, numerals that are not used
in arithmetic operations
– To represent them, codes have been developed
that specify binary equivalent for each character
• ASCII – 7 bits
• Unicode – 16 bits
• Sounds and pictures must be transformed
into a format the computer can understand
– A computer must digitize colors, notes, and
instrument sounds into 1s and 0s
Introduction to programming
What is Programming
Programming refers to a technological process for telling a computer which tasks to perform
in order to solve problems.
A plan or schedule of activities, procedures, etc., to be followed.
Programs are created to implement algorithms. These algos can be represented in the form
of flowcharts, mind maps or pseudocode, and programming is the translation of these ideas
into a computer program.
Examples of Programming Languages:
C/C++
Java
Python
JavaScript
Ruby and many more
Steps in Learning Programming Language
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
Line 1: #include <iostream> is a header file library that lets us work with input and output objects,
such as cout (used in line 5). Header files add functionality to C++ programs.
Line 2: using namespace std means that we can use names for objects and variables from the
standard library.
Line 3: A blank line. C++ ignores white space. But we use it to make the code more readable.
Line 4: Another thing that always appear in a C++ program, is int main(). This is called a function. Any
code inside its curly brackets {} will be executed.
Line 5: cout (pronounced "see-out") is an object used together with the insertion operator (<<) to
output/print text. In our example it will output "Hello World!".
Note: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }
Remember: The compiler ignores white spaces. However, multiple lines makes the code more
readable.
Line 6: return 0 ends the main function.
Line 7: Do not forget to add the closing curly bracket } to actually end the main function.
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
cout << "I am learning C++";
return 0;
}
The “cout” object, together with the “<<” operator, is used to output values/print text.
You can add as many cout objects as you want. However, note that it does not insert a new
line at the end of the output
C++ Adding of New or Blank lines
#include <iostream>
using namespace std;
int main() {
cout << "Hello World! \n\n";
cout << "I am learning C++"<<endl;
return 0;
}
Both \n and endl are used to break lines. However, \n is most used.
But what is \n exactly?
The newline character (\n) is called an Escape Sequence, and it forces the cursor to change
its position to the beginning of the next line on the screen. This results in a new line.
C++ Single Line Comments
// This is a comment
cout << "Hello World!";
-------------------------------------------------------------------
cout << "Hello World!"; // This is a comment
-------------------------------------------------------------------
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World!";
Comments can be used to explain C++ code, and to make it more readable. It can also be used to
prevent execution when testing alternative code. Comments can be singled-lined or multi-lined.
Variables
Variables are containers for storing data values.
In C++, there are different types of variables (defined with different keywords), for example:
int - stores integers (whole numbers), without decimals, such as 123 or -123
double - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
string - stores text, such as "Hello World". String values are surrounded by double quotes
bool - stores values with two states: true or false
General syntax
Creating a Variable
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
int x, y, z;
x = y = z = 50;
cout << x + y + z;
C++ Identifiers
// Good
int minutesPerHour = 60;
User inputs
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
Float
Double
float f1 = 35e3;
double d1 = 12E4;
cout << f1;
cout << d1;
#include <iostream>
using namespace std;
int main() {
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun << "\n";
cout << isFishTasty;
return 0;
}
C++ Character Data Types
The char data type is used to store a single character. The character must be surrounded by single
quotes, like 'A' or 'c':
C++ Operators
Operators are used to perform operations on variables and values.There are four main types of
operators:
Arithmetic
Addition
Adds together two values.
Operator (+)
Example Syntax (x + y)
Subtraction
Subtracts one value from another
Example Syntax (x + y)
Operator (-)
Example Syntax (x - y)
Multiplication
Multiplies two values
Operator (*)
Example Syntax (x * y)
Division
Divides one value by another
Operator (/)
Example Syntax (x / y)
Modulus
Returns the division remainder
Operator (%)
Example Syntax (x % y)
Increment
Increases the value of a variable by 1
Operator (++)
Example Syntax (++x)
Decrement
Decreases the value of a variable by 1
Operator (--)
Example Syntax (--x)
Assignment
Assignment operators are used to assign values to variables.
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
Comparison
Comparison operators are used to compare two values (or variables). This is important in
programming, because it helps us to find answers and make decisions.
The return value of a comparison is either 1 or 0, which means true (1) or false (0). These values are
known as Boolean values
Operator Name Example
== Equal to x == y
!= Not equal x != y
Logical
As with comparison operators, you can also test for true (1) or false (0) values with logical
operators. Logical operators are used to determine the logic between variables or values:
&& Logical and Returns true if both statements x < 5 && x < 10
are true
! Logical not Reverse the result, returns false if !(x < 5 && x < 10)
the result is true
-------------------Assignment 1-------------------
Create a Simple Calculator that takes an input of two number and perform ‘Addition’,
’Subtraction’, ’Multiplication’ and ‘Division’; displaying all 4 results as output.
Create a Simple Calculator that takes an input of two numbers and asks user which one of
the following operations needs to be done Addition’, ’Subtraction’, ’Multiplication’ and
‘Division’; displaying one desired output only.