Physics Department
Faculty of Science
Cairo University
Introduction
to
Computer Science
(PNR 509)
1
Two hours for Lecture
Contact:
[email protected]
2
Contents
Ch. 1: Computer Basics.
Ch. 2: Introduction to Programming.
(Algorithms & Flowcharts)
Ch. 3: Introduction to C++.
Ch. 4: Programming and Application
on the C++ and Python programs.
Ch. 5: Data acquisition- Data treatment-
Computational Analysis.
3
Chapter 1
Computer Basics
4
What is a computer?
We will define a computer as a device that:
1. Accepts some form of input/s.
2. Responds to this input information in a “pre-determined”
(programmed) way.
3. Produces some form of output/s (usually more organized or
coherent than the input).
Modern computers are electronic and digital.
- How is the oldest computer build up?
5
Computer Operations
Input Processing Output
External Storage
6
Hardware
Refers to the physical components of the computer, such
as the monitor, keyboard, hard drive, etc.
7
Software
Refers to a collection of instructions and data for the
computer in order to carry out some complete task.
8
Networks
Connects computers
LAN - Local Area
WAN - Wide Area
Wireless
Allows sharing of
programs, files,
printers, etc.
Server is “main”
computer
9
Digital computers
• Digital computers are the simplest of all! They
only understand two things, which we label
“1” and “0”, or “on” and “off”, but more
precisely, states of “high” or “low” voltage.
They are the binary digits, or bits
• Computers switch between these two states
very, VERY quickly. ASCII Code
• A single bit is not very useful for storing
information, since it can only differentiate
between two different values. 10
• If you combine two bits, however, there are
four different patterns that can be used to
represent values: 00, 01, 10, and 11.
• Three bits are able to represent eight values
using the patterns: 000, 001, 010, 011, 100, 101,
110, and 111.
• Each additional bit of storage increases the
range of potential values that can be stored. In
general, N bits can represent 2N different
values.
• Although the bit is the building block of digital
memory, memory capacity is usually specified
in bytes.
11
• A byte is a collection of 8 bits, and thus is
capable of representing 28 = 256 different
values.
byte → 8 bits
kilobyte (KB) → 210 bytes = 1,024 bytes
( = 8,192 bits)
megabyte (MB) → 220 bytes = 1,048,576 bytes
( = 8,388,608 bits)
gigabyte (GB) → 230 bytes = 1,073,741,824 bytes
( = 8,589,934,592 bits)
terabyte (TB) → 240 bytes = 1,099,511,627,776 bytes
( = 8,796,093,022,208 bits) 12
Example: What is 10012 in
Decimal?
The "1" on the left is in the "2×2×2"
position, so that means 1×2×2×2 (=8)
The "0" is in the "2×2" position, so that
means 0×2×2 (=0)
The next "0" is in the "2" position, so that
means 0×2 (=0)
The last "1" is in the ones position, so that
means 1
Answer: 1001 = 8+0+0+1 = 9 in Decimal
13
Example: What is 11112 in
Decimal?
The "1" on the left is in the "2×2×2"
position, so that means 1×2×2×2 (=8)
The next "1" is in the "2×2" position, so
that means 1×2×2 (=4)
The next "1" is in the "2" position, so that
means 1×2 (=2)
The last "1" is in the ones position, so that
means 1
Answer: 1111 = 8+4+2+1 = 15 in
Decimal
14
Examples:
What is 101012 in Decimal?
What is 1011012 in Decimal?
What is 111012 in Decimal?
What is 1810 in Binary?
What is 2710 in Binary?
What is 11810 in Binary?
15
Programming Languages
A programming language is a vocabulary and set
of grammatical rules for instructing a computer
to perform specific tasks. The term programming
language usually refers to high level languages,
such as BASIC, C, C++, FORTRAN and Pascal.
Each language has a unique set of key words.
There are two major types of programming
languages. These are Low Level Languages, High
Level Languages or very high level languages.
16
1. Low level languages
The term low level means closeness to the
way in which the machine has been built.
Low Level languages are machine oriented
and require extensive knowledge of
computer hardware and its configuration.
Low Level languages are further divided
into Machine language and Assembly
language.
17
1-a. Machine Language
Machine Language is the language of the
computer and is the only language that is
directly understood by the computer. We also
call it machine code and it is written as strings
of 1’s and 0’s.
For example, a program instruction may look
like this: 1011000111101
18
Advantage:
The only advantage is that a program of machine
language run very fast because no translation program
is required for the CPU.
Disadvantages:
1. It is very difficult to program in machine language.
The programmer has to know details of hardware to
write program.
2. Machine language is hardware dependent.
3. The programmer has to remember a lot of codes to
write a program, which results in program errors.
4. It is difficult to debug the program.
19
1-b. Assembly Language
In this language, the machine codes comprising
of 1's and 0's are substituted by symbolic codes
(called mnemonic codes) to improve their
understanding.
The set of symbols and letters forms the
assembly language and a translator program
(called Assembler) is required to translate the
programs written in assembly language into
machine language for execution by the
computer.
20
Advantages:
1. The symbolic programming of Assembly
Language is easier to understand and saves a
lot of time and effort of the programmer.
2. It is relatively easier to correct errors and
modify program instruction
3. It has almost the same efficiency of execution
as the machine level language because this is
one-to-one translator between assembly
language program and its corresponding
machine language program.
21
Disadvantages:
- One of the major disadvantage is that assembly
language is machine dependent.
A program written for one computer might not
run on other computers with different
hardware configuration.
22
2. High Level Languages
High-level languages are simple language that
use English and mathematical symbols like +, -,
%, /, etc. for its program construction.
You should know that any higher-level
language has to be converted to machine
language for the computer to understand.
Higher-level languages are problem-oriented
languages because the instructions are suitable
for solving a particular problem.
23
There are mathematical oriented languages like
FORTRAN (Formula Translation) and BASIC
(Beginners All-purpose Symbolic Instructions
Code) where very large processing is required.
Advantages:
Higher-level languages are easy to learn and
use. It is because that they are similar to the
languages used by us in our day-to-day life. The
programs can easily be debugged and are
machine independent.
24
COMPILER
It is a program that translates the instructions
of higher-level languages to machine language.
It is called compiler because it compiles every
program instruction given in higher-level
languages into machine language. Thus
compiler is a program translator like assembler
but more sophisticated. It scans the entire
program first and then translates it into
machine code.
The program written by the programmer in
higher-level language is called source program.
25
After this program is converted to machine
language by the compiler it is called object
program.
A compiler can translate only those source
programs, which have been written, in that
language for which the compiler is meant for.
Object program generated by compiler is machine
dependent.
For Example, there is a FORTRAN compiler for
PCs and another for Apple Macintosh computers.
In addition, the compiler industry is quite
competitive, so there are actually many compilers
for each language on each type of computer.
26
INTERPRETER
An interpreter is another type of program
translator used for translating higher-level
language instructions into machine language
instructions. It takes one statement of higher-
level language at a time, translates it into
machine language and executes it immediately.
The advantage of interpreter compared to
compiler is its fast response to changes in source
program. It eliminates the need for a separate
compilation after changes to each program.
Interpreters are easy to write and do not
require large memory in computer. 27
The disadvantage of interpreter is that it is
time-consuming method because each time a
statement in a program is executed, it is first
translated. Thus compiled machine language
program runs much faster than an interpreted
program.
BASIC, for example, is especially designed to
be executed by an interpreter.
28
Applications Software
Helps you to accomplish a certain task
Examples
Word processing - memos, reports, etc.
Spreadsheets - budgets, etc.
Database - search, sort, select data
Educational - simulations, practice
Graphics - charts, diagrams
Desktop publishing - pamphlets, etc.
29
Algorithm
30
What’s an Algorithm?
Dictionary definition: a procedure for solving a mathematical
problem in a finite number of steps that frequently involves
repetition of an operation;
OR : a step-by-step method for accomplishing some task.
Informal definition: an ordered sequence of instructions
(operations) that is guaranteed to solve a specific problem.
Step 1: Start,
Step 2: Do something
Step 3: Do something
…
Step N: Stop, you are finished
31
Operations In An Algorithm
A Computer programming language is used by a programmer
to express the solution to a problem in terms that the
computer system can understand.
1. A sequential operation
2. A conditional operation
3. An Iterative operation
32
Operations In An Algorithm
A Computer programming language is used by a programmer
to express the solution to a problem in terms that the
computer system can understand.
1. A sequential operation carries out a single well-defined task.
When that task is finished, the algorithm moves on to the next
operation.
Set the value of x to 1.
2. A conditional operation is the “question-asking” instructions
of an algorithm. It asks a question and then select the next
operation to be executed according to the question answer
If x is not equal to 0, then set y equal to 1/x; otherwise, print an error
message that says we cannot divide by 0.
33
Operations in an Algorithm (cont.)
3. An Iterative operation is a “looping” instruction of an algorithm.
It tells us not to go on to the next instruction, but instead, to go
back and repeat the execution of a pervious block of instructions
Repeat steps 1, 2, and 3 until the value of y is equal to 21.
34
Formal Definition of an Algorithm
If we can specify an algorithm to solve a problem, then we can
automate its solution.
(convert it into a program)
An Algorithm is
a. A well-ordered collection of
b. clear and effectively computable operations
that, when executed, produces
c. A result and
d. halts in a finite amount of time
35
a. A well-ordered collection…
Clear and un-ambiguous ordering to these operations
What’s the next operation when we finish any one operation?
At step 4, what operations should be repeated?
Ambiguous ordering
Go back and do it again
Go back to step 3 and begin execution from that point
Start over
Start over from step 1
If you understand this material, you may skip ahead
If you understand this material, skip ahead to line 2.
36
b. Unambiguous and effectively
computable operations…
An unambiguous operation
is one that can be understood and carried out directly by the
computing agent without needing to be further simplified or
explained.
Primitive (operation).
Effectively computable or doable operation
There exists a computational process that allows the
computing agent to complete that operation successfully.
37
b. Unambiguous and effectively
computable operations…
Which of the following are primitive
operations for a computer?
Add x and y to get the sum z.
See whether x is greater than, equal to, or less
than y.
Sort a list of names into alphabetical order.
Factor an arbitrary integer into all of its prime
factors.
38
c. Produces a result…
In order to know whether a solution is
correct, an algorithm must produce a
result that is observable to a user.
a. Sometimes it is not possible for an algorithm to
produce the correct answer because for a given
set of input, a correct answer does not exist.
b. Error messages (result) should be produced
instead.
39
d. Halts in a finite amount of time…
The result must be produced after the
execution of a finite number of
operations, and we must guarantee that
algorithm eventually reaches a statement
that says “Stop, you are done”.
40
Conclusion:
Example 1: Finding the
largest integer among five
integers
12 8 13 9 11 41
42
Algorithm of actions in
Find Largest:
43
Find Largest refined
44
Generalization of Find Largest
45
Example 2
Write an algorithm to calculate the area of circle
Solution:
Input diameter as d (Ask user to input
diameter)
Calculate radius as r (r=d/2)
Set pi = 3.14
Calculate area (A)= pi * radius * radius
Output A
46
Example 3
How many stamps do you use when mailing a letter?
One rule of thumb is to use one stamp for every five
sheets of paper or fraction thereof.
47
Example 3
1. Request the number of sheets of paper; call it
Sheets. (input)
2. Divide Sheets by 5. (processing)
3. Round the quotient up to the next highest whole
number; call it Stamps. (processing)
4. Reply with the number Stamps. (output)
48
Flowchart
49
Flowchart
is one way of diagramming the logic of the program.
Flow charts use specific symbols to represent
different activities and a written message within each
symbol to describe each activity. These symbols are
standardized and are called ANSI (American National
Standards Institute) symbols. In general, a flow chart
should have a "start" and at least one stop. Normally,
the flow chart is running from top to bottom and from
left to right.
50
Arrowheads can be used to indicate the direction of
the flow. All flow charts must be checked carefully for
logical errors by testing the flow of logic from up to
down and from left to right.
There are four major steps that must be
performed when writing a computer program:
Defining the problem
Planning the solution
Writing the program
Testing the program
51
Symbol Name Meaning
Terminal Start or end of the
program
Input / Output Input or Output
operation
Process Calculation or data
manipulation
Decision A decision that
requires a choice
Which will determine
the logical Path to
follow
Flow line Or To show the
Arrows direction of
processing logic
flow
52
Types of flow charts
Flow charts are categorized into 4 main
categories:
1- Simple flow charts
2- Branched flow charts
3- Single loop flow charts
4- Multi loops flow charts
53
1- Simple flow charts
We arrange the steps of the solution for
these types of charts in the form of
straight chain from start of program till
its end. In this type, there are no
branches or loops.
The general form of this type is shown in
figure below.
54
1- Simple flow charts
INPUT/
S
55
Example 1:
How many stamps do you use
when mailing a letter?
* One rule of thumb is to use one
stamp for every five sheets of
paper or fraction thereof.
Solutions:
1. Request the number of
sheets of paper; call it Sheets.
(input)
2. Divide Sheets by 5.
(processing)
3. Round the quotient up to
the next highest whole
number; call it Stamps.
(processing) 56
Example 2:
Develop a flow chart to find the
perimeter and area of a circle of
radius R.
Answer:
1- Start.
2- Read value of R.
2
3- π=3.14.
4- Calculate the Area (A=πR2).
5- Calculate the perimeter
(P=2 πR).
6- Print A & P.
7- Stop.
57
Example 3:
Develop a flow chart to find the area
and the perimeter of a rectangle of
length X and width Y.
Answer:
1- Start.
2- Read X and Y.
3- Calculate the area A= XY.
4- Calculate the perimeter
p= 2(X+Y).
5- Print A & P.
6- Stop.
58
2- Branched flow charts
Branching occurs due to the need of
taking a decision, or making a choice.
This type is best illustrated by figures
and examples.
59
Generally, branched flow chart can
take one of the following figures
60
Example 4:
A salesman gives a 7% discount if you buy
things from him for more than 500
pounds, and 4% if less. Draw a flow chart
for the selling process of this salesman.
Answer:
First, read the value of the things you
bought
if X ≥ 500 then calculate the discount where the
discount = (X*7/100),
else (500 ≥ X) then the discount is =(X*4/100) .
The required fellow chart is shown below.
61
62