Download Intro to computer programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Programming language wikipedia , lookup

Compiler wikipedia , lookup

Reactive programming wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

Stream processing wikipedia , lookup

Go (programming language) wikipedia , lookup

Structured programming wikipedia , lookup

Interpreter (computing) wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Parallel computing wikipedia , lookup

Assembly language wikipedia , lookup

ILLIAC IV wikipedia , lookup

Transcript
PGT106 – C PROGRAMMING
Introduction to Computers and Programming Languages (Part 1)
Computer Fundamentals
Computer system is divided into hardware and software.
Hardware refers to physical components of computer which are:
o Main Memory
o Central Processing Unit (CPU)
o Input Device
o Output Device
o Secondary Memory Device
Figure 1.1 - example of processors (CPU)
The Intel Atom processor chip contains the full circuitry of a central
processing unit in an integrated circuit whose small size and low power
requirements make it suitable for use in mobile internet devices. (Intel
Corporation Pressroom Photo Archives)
Figure 1.2 - Types of Computers
(a) Notebook Computer (HP Pavilion dv5©, Courtesy of Hewlett-Packard).
(b) Palmtop Computer (iPhone 3G©, Courtesy of Apple, Inc.)
(c) Desktop Computer (iMac©, Courtesy of Apple, Inc.)
1
PGT106 – C PROGRAMMING
Figure 1.3 Components of a Computer
CPU
Control Unit
Input Device
Arithmetic and
Logic Unit
Output Device
Register
Main Memory
Secondary Memory
Central Processing Unit (CPU)
o CPU is the computer’s administrator and is responsible for supervising
the operation of the other sections
o Consists of two functional units;
 control unit
- supervises all activities of the computer system
 arithmetic-logic unit (ALU)
- performs basic arithmetic operations and comparison
operations
2
PGT106 – C PROGRAMMING
Main
o
o
o
Memory
keeps information from the input unit.
Used only during processing of data.
also keeps processed information until it can be placed on output
devices
o all programs must be loaded into main memory before they can be
executed and all data must be brought into main memory before it can
be manipulated.
o However, the data/information is temporary. It will be discarded after a
program ends, or when the computer is turned off.
o Main memory can be further classified into two types:
 Random Access Memory (RAM)
- information in RAM will be lost when the computer is
turned-off.
 Read Only Memory (ROM)
- It has been set during manufacturing process. ROM
usually contains instructions and information considered
to be fundamental to the computer.
o Figure 1.4
Visual representation of 1000 Memory Cells in Main Memory
Secondary Memory/ Secondary storage
o Stores information permanently (as compared to temporary
information retain of main memory)
o The stored information can be retrieved later.
o Examples of information/data that is stored :
 program file, data file, image file, etc
o Examples of secondary storage devices:
 hard disk, diskette, CD, USB drives, etc
 (refer to figure 1.3)
3
PGT106 – C PROGRAMMING
Input/Output Devices
o Input devices –
 feed data and programs into computers
 e.g. keyboard, mouse, touch screen, scanners
o Output devices –
 display results produced by computer
 e.g. monitor, printer, speaker
Software
o As a complement to hardware, computer system needs software to
work/function.
o Softwares are classified into :
 System software
 Application software
 Programming Languages
o System software : manages the computer and its peripheral devices
(hardware)
 E.g. Operating system (OS)
 Text editor
 Pre-processor
 Language translator
 Linker
 Loader
o Application software : performs specific tasks
E.g. word processor, desktop publishing software, spreadsheets,
database, graphics, communication, programs perform specific
tasks such as accounting, scientific, engineering, education, etc
Programming Fundamentals
Programming Languages
o Programming language is divided into three categories:
 Machine Language
 Assembly Language
 High-Level Language
o Machine Language
 Language understood by the computer
 Bunch of 0’s and 1’s (binary code)
 Program written in machine language can be executed without
being translated
 Nevertheless, hard to learn because it is written in 0’s and 1’s
 Program is too long to solve simple problem
 Machine-dependant and not portable
E.g.
1011 1100 0100 1011 1000
1010 0101 1001 1100 0111
4
PGT106 – C PROGRAMMING
Figure 1.6 Relationship Between a Byte and a Bit
o Assembly Language
 Strings of 0’s and 1’s are replaced into instructions which
resemble English language to represent computer operation
element
 Easier to understand and write
 E.g.
LOAD
MULT
STOR


rate
hour
wages
Nevertheless, needs a language translator called Assembler to
change Assembly Language to Machine Code for execution
purpose
still too long and not portable
o High-Level Language
 Improves weaknesses in Machine Language and Assembly
Language
 Easier understood because it is closer to natural language
(especially English)
 Portable (Can be executed on more than one platforms/
environments)
 Written in one instruction to carry out several instructions in
machine level
 E.g. discount_price = price – discount;
 needs a compiler : a system software that translates source
program to object program
- translates the codes into machine language
 Examples:
- C, C++, Java, Visual Basic, Pascal.
Algorithms
o Refers to the solution to any computing problem which involves
executing series of actions in a specific order (step-by-step
instructions)
o Represented in writing, and can be replicated (used again and again)
5
PGT106 – C PROGRAMMING
o Two most common ways of representing an algorithm:
 Pseudo code
 Flow chart
o Pseudo code : artificial and informal language that helps programmers
develop algorithms
 E.g. :
Start
Get student’s grade.
if student’s grade is greater than or equal to 50
Print “Pass”
else
Print “Fail”
End
o Flowchart: visual-form of an algorithm
 E.g.
start
Read grade
grade >= 50
True
Print “Pass”
False
Print “Fail”
end
o Basic symbols in a flowchart
Terminator
(start/ end)
Direction flow
Process
connector
Input/ output
(data)
Decision
6
PGT106 – C PROGRAMMING
Basics of a Typical C Program Development Environment
7
PGT106 – C PROGRAMMING
Figure 1.7 Entering, Translating, and Running a High-Level Language
Program
Figure 1.8 Flow of Information During Program
Execution
8
PGT106 – C PROGRAMMING
Simple C Program: Program to add two numbers
o For starters, just remember that a program MUST include the parts
marked as * .
#include <stdio.h>
*
int main()
{
int dA, dB, dSum;
printf (“Input first integer \n”);
scanf (“%d”, &dA);
printf (“Input second integer \n”);
scanf (“%d”, &dB);
dSum = dA + dB;
printf (“Sum is %d\n”, dSum);
return 0;
*
}
9