Introduction
Contents
• Computers
• Programming
What is a Computer ?
• We live in a world of electronic devices
• Not all of these are computers
• What makes a computer different from the other devices?
What is a Computer ?
• A computer can
• Receive data from the outside world
• Process this data in some way to compute results
• Store data for varying periods of time
• Output results to the outside world
• Allow the user to have the computer remember a series of operations to perform so that the same thing can be done again
How does this differ from a calculator?
• A calculator can• Accept input from the world via the keys
• Perform calculations by pressing keys
• Store values in its memory
• Output results
• A calculator cannot• Store a series of operations to be performed
later
What Does a Computer Do?Whatever I tell it to do
• I say: Add 2 and 3 and show me the result
• Computer says: 5
• I say: Multiply that result by 7 and show me the result
• Computer says: 35
• I say: Show me the home page of the Marmara CE department
• Computer says:
What’s Going On?
The Ghost in the Machine• Read an input and memorize it• Read another input and memorize it• Add the inputs, display and memorize result• Read input and memorize it• Multiply input with previous result and display the result
• Hardware does the work• Programmer writes a program for the machine• Program tells hardware what to do at each step
WHAT is PROGRAMMING??scheduling or performing a task or /
and event
WHAT is COMPUTER PROGRAMMING??
creating a sequence of steps for a computer to follow in
performing a task
WHAT is a PROGRAMMING
LANGUAGE ?
A set of rules, symbols, and special words used to construct a computer program
Where do you see programming in real life?
• Short answer: all over the place!
12
What’s a Computer?• A computer is a device that processes data and
computes results under the direction of a program
• Notice that the computer does nothing by itself
• It is told what to do by a program (software)
• Program refers to a specific set of instructions given to the computer to accomplish a specific task
• Compute the sum of N numbers
• Find min/max of N numbers
• Sort numbers
• Compute average grade of a class
• Find the shortest path between 2 cities
• ……
13
Hardware (Computer Architecture)
• Central Processing Unit (CPU)
• Executes instructions
• Random Access Memory (RAM)
• Stores volatile data
• Input/Output (I/O) Devices
• Keyboard, Monitor, Disk, Mouse, Network Card, …
• Used to input/output data
CPU MemoryInput/Output (I/O)
Devices
System BusRelevant Courses1. Computer Architecture2. Microprocessors
14
Memory• Divided into logical units
of the same size called a byte
• Each byte is made up of 8 consecutive binary digits or bits
• Each bit is in one of two states
• OFF: 0
• ON: 1
• Each byte has a unique address
• First byte has address ” 0 ”
Address
Byte 0Byte 1Byte 2
Byte 3
Byte N-1
1 1 0 10 1 0 1
1 0 0 11 1 0 0
0 0 0 10 0 0 0
0 0 0 00 0 0 0
1 1 1 11 1 1 1
1 0 1 10 0 1 1
Most-significant bit
Least-significant bit
1 byte
15
More on Memory
• Bytes are also grouped in larger chunks
• According to Intel convention
• 1 byte : 8 consecutive bits
• 1 word : 2 consecutive bytes
• 1 longword: 4 consecutive bytes
• 1 quadword: 8 consecutive bytes
• 1 octaword: 16 consecutive bytes
16
More on Memory• Size of a memory is measured in bytes
• However the memory size is usually expressed in larger units
• 1 Kilobyte (KB) = 1024 bytes = 2^10 ~ 10^3 bytes
• 1 Megabyte (MB) = 1024*1024 = 2^20 ~ 10^6 bytes
• 1 Gigabyte (GB) = 2^30 ~ 10^9 bytes
• 1 Terrabyte (TB) = 2^40 ~ 10^12 bytes
• 1 Petabyte (PT) = 2^50 ~ 10^15 bytes
• Maximum memory size depends on the architecture
• 32-bit system max. memory size: 2^32=4GB
- 64-bit system max. memory size: 2^64bytes
17
Central Processing Unit (CPU)
• Brain of the computer
• Executes instructions specified by the program
ControlUnit
RegistersALU/FPU
Bus Interface
IP
System Bus
• CPU consists of several subunits– Control Unit + Instruction Pointer (IP)
• Fetch & Decode Instructions– Arithmetic Logic Unit
• Perform arithmetic and logical operations– Add, subtract, multiply, divide, compare, shift,…
– Registers• Hold temporary data needed by the instruction
18
Basic Operation of the Computer
• loop:
• Fetch the next instruction from memory
• Decode the instruction
• Interpret what the instruction means
• Fetch the operands (if necessary)
• Fetch the operands from memory to CPU registers
• Execute the instruction
• Perform what the instruction is telling us to do (add, subtract, multiply, divide, …)
• Store the result back to memory (if necessary)
• repeat
Why do we need programming?
• You think: “Hello World”
• The computer would think:
H E L L O [space] W O R L D
01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100
This notation is simple or….. what? What do you think? What is your prefer?
Nobody else wants to, either.
What’s the other choice?
• Instead, we use “programming languages” to talk to the computer.
• Just like other languages, you express ideas in different ways when using different programming languages.
• What are some foreign languages you can think of?
There are lots of programming languages, too.
• Here are some you might have heard of: Basic, Java, Javascript, Logo.
• Here are some you probably haven’t heard of before now: C++, Perl, Scheme, C#, Python, Lisp, Cobol, Fortran, Pascal, SQL, Haskell, J2EE, Maple, PHP, Ruby, Ubercode.
23
Program (Software)
• Conceptually a program is a step-by-step solution to a problem
• Also named an algorithm
• A well-defined computational procedure that takes some values (data) as input and produces some results as output
ALGORITHMInput (DATA) Output (Results)
Algorithms (source : wikipedia)
• In mathematics, computing, linguistics and related disciplines, an algorithm is a procedure (a finite set of well-defined instructions) for accomplishing some task which, given an initial state, will terminate in a defined end-state.
• Algorithms are essential to the way computers process information, because a computer program is essentially an algorithm that tells the computer what specific steps to perform (in what specific order) in order to carry out a specified task, such as calculating employees’ paychecks or printing students’ report cards. Thus, an algorithm can be considered to be any sequence of operations which can be performed by a Turing-Complete system.
25
More on Software
Step1
Step2
Algorithm Steps
Step3
Step4
Step5
StepN
Pseudo-code (Human-readable)
DATA
Relevant Courses1. Intro to Comp. Eng.2. Data Structures3. Databases4. Algorithms
Relevant Courses1. Introduction to Computer Engineering 2. C Programming3. Visual Programming (C#)4. Object Oriented Programming (Adv. OO Concepts and Java)5. Advanced Programming Techniques (Advanced Java)
IP
Expressing the Algorithms
• A “Standard” way of describing an algorithm must exist if we expect our solution to be understood by others easily
• There are two standards in programming:• NATURAL LANGUAGE
• PSEUDOCODE
• FLOWCHARTS
• PROGRAMMING LANGUAGE
Natural Language
• "...prose to describe an algorithm, ignoring the implementation details. At this level we do not need to mention how the machine manages its tape or head“
• In the philosophy of language, a natural language (or ordinary language) is a language that is spoken, written, or signed (visually or tactilely) by humans for general-purpose communication, as distinguished from such constructs as computer-programming languages or the "languages" used in the study of formal logic, especially mathematical logic.
Pseudo Code
• “Pseudo” means “pretend” or “false”
• Pseudo Code is pretend or false computer code; generic English-like terms that are somewhat like computer code
• Pseudo Code is not as standardized as flowcharts, and does not facilitate the breaking down of problems as well as a flowchart does
Pseudocode (wikipedia)
• Pseudocode (derived from pseudo and code) is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines, variable declarations or language-specific syntax. The programming language is augmented with natural language descriptions of the details, where convenient.
Flowcharts
• A Flowchart is a Visual Representation of an algorithm
• A Flowchart uses easy-to-understand symbols to represent actions on data and the flow of data
• Flowcharts aid in breaking down a problem into simple steps
Example of flowcharts
Example :
One of the simplest algorithms is to find the largest number in an (unsorted) list of numbers. The solution necessarily requires looking at every number in the list, but only once at each. From this follows a simple algorithm, which can be stated in a high-level description English prose, as:
•Natural language:
Assume the first item is largest.
Look at each of the remaining items in the list and if it is larger than the largest item so far, make a note of it.
The last noted item is the largest in the list when the process is complete.
• (Quasi-) Formal description: Written in prose but much closer to the high-level language of a
computer program, the following is the more formal coding of the algorithm in pseudo code :
Algorithm LargestNumber Input: A non-empty list of numbers L. Output: The largest number in the list L. largest ← L0 for each item in the list L≥1, do if the item > largest, then largest ← the item return largest
• "←" is a loose shorthand for "changes to". For instance, "largest ← item" means that the value of largest changes to the value of item.
• "return" terminates the algorithm and outputs the value that follows.
35
Example Program: Compute the sum, product and avg of 2 numbers, a & b
a 10b 40sum a+bproduct a*bavg sum/2print sumprint productprint avg
? a
? b
? sum
? product
? avg
DATAALGORITHMIP
?10IP
40IP
50IP
400
IP
25
IPIPIP
5040025
SCREEN
Assignment !
• Make an algorithms in high level language, pseudo code and flowchart for
1. Computing factorial (N!)
2. Finding roots of ax2 + bx + c = 0
37
Operating System• Is a layer of software that exports an easy-to-use
interface to program the hardware
• User programs make use of the hardware through the services provided by the OS– Thus OS makes life easier for user programs– Relevant course: Operating Systems
Operating System (Windows, Linux, Solaris, …)
Hardware
P1 P2 Pn
System call API
Hardware API
User Programs
38
From Algorithm to 0s and 1s• Although we showed the instructions stored in
memory in pseudo-code (human readable form), what is really stored is 0s and 1s
• Remember a computer only understands 0s
and 1s
• This means that we need tools that would help us convert the program from human-readable form to the machine-readable form called the machine language
• These tools are called system software tools
39
System Software Components• We have several system software components to make
programming the computer easy
• High-level Programming Languages (C, C++, Java, C#, ..)
• Compiler
• Converts your programs implemented in high-level language to assembly language, which are mnemonic codes that correspond one-to-one with machine language
• Assembler
• Converts programs implemented in assembly language to machine language, which are binary number codes understood by a specific CPU
• Linker
• Combines several object files together into an executable
• Loader + Operating System
• Loads your program into memory for execution
What Programs Look Like
read (a)
read (b)
c = a + b
read (d)
e = d * c
print (e)
; Read first input and store it in variable a
; Read second input and store it in variable b
; Add the values of a and b and store the result in variable c
; Read third input and store it in variable d
; Multiply the values of c and d and store the result in variable e
; Display the value of variable e
• The computer takes each of these instructions and executes it
Computer’s Job: Compute Outputs from Inputs
CPU
out = in1 + in2
2
3
5in1
in2out
Computer’s Job: Read/Write Variables From/To Memory
CPU
c = a + b
a
c
b
2
5
3
c = a + b
2
3Memory
5
Computer’s Job: Input and Output
CPU
Memory
2
read (a)
2
a
c
b
2
read (a)read (b)
print (c)c = a + b
Computer’s Job: Input and Output
CPU
Memory
3
3
read (b)
a
c
b
2
3
read (a)read (b)
print (c)c = a + b
Computer’s Job: Input and Output
CPU
Memory
a
c
b
2
5
3
c = a + b
2
3
5
read (a)read (b)
print (c)c = a + b
Computer’s Job: Input and Output
CPU
Memory
read (a)read (b)a
c
b
2
5
3
print (c)
print (c)c = a + b
5
5
5
• How does computer understand “a”, “b”, “4”, etc.
• i.e. data values?
• How does computer understand “read (a)”; that it should add its inputs, when to read from memory, when to take next step etc.
• i.e. control
Computer’s Job: Representing and Translating Information
• Answer: Everything is represented as 1’s and 0’s– Computer only sees 1’s and 0’s– All data values, instructions etc. are encoded as
combinations of 1’s and 0’s
The World of 1’s and 0’s
CPU
c = a + b
010111001
0010
Memory0011
0101
0010
0011
0101
0000000001
00010
10011
11111
WHAT is LANGUAGE ?
Programmability
• The ability to store a series of operations and perform them later is called programmability
• It is programmability which distinguishes a computer from a calculator
• Programmability allows
• Long sequences of operations to be stored
• These sequences to be repeated any time
• Conditional logic to be included
What is Programming ?
• Programming is the act of writing a computer program
• This program will tell the hardware of the computer what operations need to be performed
• These programs are written in one of several programming languages
Programming Languages
• Much more precise than human languages
• Much smaller than human languages
• Many are based on a mathematical notation
• Take much less time to learn than a human language
Programming
• Computers are dumb(dilsiz)
• They are glorified(kaliteli) calculators
• Therefore
• They do exactly what they are told to do
• You must be very careful that you tell them to do the right thing
• This requires attention to detail
Who is a Programmer?
• A programmer is a person who writes the required computer programs.
• Programmers translate the expected tasks given in human understandable form into machine understandable form by using compilers and interpreters.
The Programmer Personality
• Patient
• Persistent(kalıcı)
• Precise(kesin)
• Likes to figure out how to solve small problems
• Can plan what is needed to solve a larger problem
• Confidence that they can solve to problem
Programming language rules consist of:
Rules of Syntax which specify how valid instructions are written in the language(like natural language rules subject + verb +object )
Rules of Semantics which determine the meaning of the instructions (what the computer will do)(like natural language rules A book has bitten
a car )
57
Software Development Cycle1. Specify problem requirements
• State the problem clearly; understand what is required
2. Analyze the problem
• Identify the inputs (data), outputs (results), and any additional requirements
3. Design a solution (algorithm)
• Develop the list of steps that solves the problem
• Do Procedural Design or Object-Oriented Design
4. Implement the algorithm
• Code the algorithm using a programming language (C, C++, Java, C#, …)
• Notice that an algorithm is independent of the PL
58
Software Development Cycle (cont)
5. Test and verify
• Run the code with many sets of test data to verify that it does what it is supposed to do
6. Maintain and update code
• Modify the code as needed
Programming Languages
Examples of program codes in C and Pascal
Programming Languages
Borland C
Microsoft Visual Studio
Programming LanguagesMicrosoft Visual Basic
Overview of Programming Environment
What are the roles play by each component?
Hardware
program texts
Software
People(analysts, testers, programmers, etc)
1. Programming Languages (Machine Languages, Assembly Languages, C, C++, Java, FORTRAN, COBOL, etc)
2. Programming Tools (Editors, Compilers, Linkers, Pre-Processors, loaders),
3. Programming Procedures and Methods (structured programming, OOP, achieving, etc.)
Programming Environment.
1. Hardware
2. Software
•The physical computers that host the DBMS and the applications, Network hardware, connections etc.
Collection of computer programs, operating system, utility tools, applications written in some programming languages such as Pascal, C, C++, Cobol, etc.
Hardware Software Procedure People
•Procedures refers to instructions and rules for the design, implementation, deployment and use a database.
•The major roles people play in a database environment are: DBA -Data/Database Administrator
•Programmers, End Users, vendors
4 Procedure5.People
3.Data
Data
Data is reason that DBMS exist. It is bridge between people and machine.
There two types of data : operational and meta-data.
The data structures are use to organize and store data.
Why do we write computer programs?
Computer programs are written to solve real-life problems.
E.g. Bookstore problem.
A bookstore needs to keep track of books sold by the store. Each sale is recorded by the book title and by it’s publisher. At the end of the week, the owner manually generates a list of the number of each book sold and number of books sold from each publisher. A list
1. Read the sales file
2. Count the sales by title
3. Count the sales by publisher
4. Sort the titles by publisher
5. Write the results.
Program Development Cycle: Design, Write, Compile and Run.
Problem statement
Step-wise structured program design.
Problem / Solution Analysis
start
finish
Waterfall Model
Design
Implementation
Testing
Deployment
Why C? • Native language of UNIX
• Standard development language for personal computers
• Portable (can be moved to other machine !)
• Powerful set of operators and powerful libraries (some operators: ++,--….)
• Basis for Java, C++…..
Execution of a Program.
Using Compiler:
Source Code Compiler Object Code
Execute a lineof ProgramExecute a lineof Program
Using Interpreter:
Source Code Interpreter
Execute ProgramExecute Program
Program Development Process
Source Program - Write program in high-level
language
Compile program into assembly language
Assemble program into machine language
Link multiple machine-language program into
one application
Program Development
Load program into computer’s main memory
Execute program
Program Execution
Compiling, linking & producing an executable program
Editor(create and modify
code)
Compiler(convert source
code into m/c code)
Memory (RAM or ROM)
Libraries and other object code
Source code file (FILE.C)
Object code file (FILE.OBJ)
Executable file (FILE.EXE)
Errors and/or warnings
Compiler = changes the high-level language into machine code, e.g.: high level (C, Basic, COBOL, Fortran, Pascal)Linker = links the object code with other object code files (to produce executable program)