Upload
drake-banks
View
68
Download
1
Tags:
Embed Size (px)
DESCRIPTION
204111: Introduction to Computer & Programming. Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand http://kdl.cpe.ku.ac.th. Lecture 1: Introduction. Computer & Program. Computers - PowerPoint PPT Presentation
Citation preview
204111: Introduction to Computer & Programming
Lecture 1: Introduction
Dept. of Computer Engineering Faculty of Engineering, Kasetsart University
Bangkok, Thailand http://kdl.cpe.ku.ac.th
2
Computer & Program Computers
Devices for performing computations at high speeds with great accuracy
A machine that can be programmed to manipulate symbols.
Can quickly store and retrieve large amounts of data.
Physical components are known as "hardware". Hardware components: CPU, Memory, Mouse, Keyboard, etc.
Program A set of instructions for a computer to
follow, written in specific programming language
3
Computer Components
CPU (Central Processing Unit) Primary storage (memory) Secondary storage (disks, tapes etc.) Input devices (mouse, keyboard etc.) Output devices (screen, printer,
plotter, etc.)
5
Computer Systems
Hardware (HW) Actual physical machines (equipment)
that make up the computer
Software (SW) Programs written for a specific
application are often called softwares.
6
Computer Languages
• Computer languages categories: Machine language
Binary Code Assembly language High-Level Language
Uses English-like language Machine independent Portable Examples: Pascal, C, C++, Java, Fortran, .
. .Computer can execute only the machine language
7
High-Level Languages Procedural Language
Fortran Cobol Basic C Pascal
Object-Oriented Language C++
Functional Language Lisp
Logic Language Prolog
9
รู้� �จั�กก�บคอมพิ�วเตอรู้�
คอมพิ�วเตอรู้� (Computer) ค�อ เครู้��องค��นวณอ�เล็�กทรู้อน�กส์�ท��ส์�ม�รู้ถท��ง�นค��นวณผล็แล็ะเปรู้�ยบเท�ยบค$�ต�มชุ&ดค��ส์� �งด�วยคว�มเรู้�วส์�งอย$�งต$อเน��องแล็ะอ�ตโนม�ต�
ก�รู้ท��เครู้��องคอมพิ�วเตอรู้�จัะท��ง�นได� จั��เป*นจัะต�องม�ชุ&ดค��ส์� �งก��ก�บ เพิ��อให้�คอมพิ�วเตอรู้�ท��ง�นได�ผล็ต�มท��ต�องก�รู้
10
คอมพิ�วเตอรู้�ในง�นด��นต$�งๆ ก�รู้ค��นวณท�งคณ�ตศ�ส์ตรู้�
เครู้��องค�ดเล็ข ก�รู้ค��นวณท�งว�ทย�ศ�ส์ตรู้�
ก�รู้จั��ล็องส์ถ�นก�รู้ณ� (simulation)
ก�รู้แก�ไขป0ญห้�ท�งว�ทย�ศ�ส์ตรู้� (scientific problem solving)
ก�รู้ควบค&มธุ&รู้กรู้รู้ม ATM ก�รู้จั�ดก�รู้ปรู้ะมวล็ผล็บ�ญชุ�
ล็�กค��
ก�รู้จั�ดก�รู้ง�นท��วไป ก�รู้ท��เอกส์�รู้ ก�รู้น��เส์นอผล็ง�น
ก�รู้ส์��อส์�รู้ โทรู้ศ�พิท�ม�อถ�อ โทรู้คมน�คม
คว�มบ�นเท�ง ดนตรู้� ภ�พิยนตรู้� Graphic เกมส์�
11
ส์$วนปรู้ะกอบของคอมพิ�วเตอรู้� รู้ะบบคอมพิ�วเตอรู้� ปรู้ะกอบด�วย 2 ส์$วนห้ล็�ก
ได�แก$1 . ฮ�รู้�ดแวรู้� Hardware : Microprocessor ,
Monitor, Hard disk , Keyboard , Mouse , Printer , etc
2. ซอฟต�แวรู้� Software : Window XP , Linux Microsoft word, Internet Explorer, Adobe Photoshop, Ragnarok Online
13
Microprocessor เป*นเห้ม�อนส์$วนส์มอง
ห้ล็�กของเครู้��องคอมพิ�วเตอรู้�
ท��ห้น��ท��ปรู้ะมวล็ผล็ท�งคณ�ตศ�ส์ตรู้� (บวก ล็บ ค�ณ ห้�รู้) แล็ะท�งตรู้รู้กศ�ส์ตรู้� ( and or not)
Intel / AMD
14
Memory เป*นท��เก�บข�อม�ล็ในเครู้��องคอมพิ�วเตอรู้� ก$อนท��
จัะน��ข�อม�ล็น�7นม�ปรู้ะมวล็ผล็ใน microprocessor
ห้น$วยคว�มจั��ในเครู้��องคอมพิ�วเตอรู้�แบ$งออกเป*น 2 ปรู้ะเภท ห้น$วยคว�มจั��ห้ล็�ก (Main Memory) ห้น$วยคว�มจั��ส์��รู้อง (Secondary Memory)
15
ห้น$วยคว�มจั��ห้ล็�ก
RAM
เก�บชุ&ดค��ส์��งแล็ะข�อม�ล็ต$�งๆ ไมโครู้โปรู้เซส์เซอรู้�จัะอ$�นข�อม�ล็จั�กห้น$วย
คว�มจั��ชุน�ดน�7เป*นห้ล็�ก RAM (Random Access Memory)
ส์�ม�รู้ถเข�ยนห้รู้�ออ$�นข�อม�ล็ได� เก�บข�อม�ล็ไม$ถ�วรู้ ข�อม�ล็จัะส์�ญห้�ยห้�กข�ดไฟ
เล็�7ยง ROM (Read Only Memory)
อ$�นข�อม�ล็ได�อย$�งเด�ยว เก�บข�อม�ล็ได�ถ�วรู้ แม�ว$�จัะไม$ม�ไฟเล็�7ยง
16
Secondary Memory คว�มจั&ส์�งข87นกว$�ห้น$วยคว�ม
จั��ห้ล็�ก แต$คว�มเรู้�วในก�รู้ถ$�ยเทข�อม�ล็
น�อยกว$� Hard disk Floppy disk Zip drive Thumb drive CD-ROM
Thumb Drive
17
Data Representation ข�อม�ล็ในคอมพิ�วเตอรู้�จัะถ�กเก�บอย�$ในรู้�ปของ bit
bit = binary digit (เล็ขฐ�นส์อง) ม�ค$�เป*น 0 ห้รู้�อ 1 (ต�มส์ถ�นะของไฟฟ:�)
byte = 8 bits ข�อม�ล็ 1 byte ในเครู้��องคอมพิ�วเตอรู้�อ�จัจัะม�คว�มห้ม�ย
แทนข�อม�ล็ได�ห้ล็�กห้ล็�ยปรู้ะเภท ต�วอย$�งเชุ$น ข�อม�ล็ 1 byte เก�บค$�ด�งน�7 011000012
ข�อม�ล็ด�งกล็$�วอ�จัจัะแทนอ�กขรู้ะ ‘a’ ห้รู้�อแทนต�วเล็ข 97 ก�ได� ข87นอย�$ก�บโปรู้แกรู้มคอมพิ�วเตอรู้� ว$�จัะต�ค$� 1 byte น�7นเป*นอะไรู้
18
I/O Devices เป*นอ&ปกรู้ณ�ท��ท��ห้น��ท��
ต�ดต$อส์��อส์�รู้รู้ะห้ว$�งคอมพิ�วเตอรู้�ก�บโล็กภ�ยนอก
Keyboard Mouse Monitor Printer Speaker
20
ก�รู้ท��ง�นของเครู้��องคอมพิ�วเตอรู้� ชุ&ดค��ส์��งแล็ะข�อม�ล็จัะถ�กเก�บอย�$ในห้น$วยคว�มจั�� CPU (microprocessor)ด8งข�อม�ล็แล็ะชุ&ดค��ส์��ง
ม�จั�ก ห้น$วยคว�มจั�� อ&ปกรู้ณ�อ�นพิ&ต (Keyboard , Mouse , etc.)
CPU ปรู้ะมวล็ผล็ข�อม�ล็ CPU น��ข�อม�ล็ผล็ล็�พิธุ�ท��ถ�กปรู้ะมวล็ผล็แล็�วไปย�ง
ห้น$วยคว�มจั�� อ&ปกรู้ณ�เอ�ท�พิ&ต (Monitor , Printer , etc.)
21
โปรู้แกรู้มคอมพิ�วเตอรู้� เรู้�ไม$ส์�ม�รู้ถท��ง�นต$�งๆได� โดยอ�ศ�ยเพิ�ยงแต$เครู้��อง
คอมพิ�วเตอรู้�อย$�งเด�ยว เครู้��องคอมพิ�วเตอรู้�ส์�ม�รู้ถท��ง�นได�รู้วดเรู้�ว แต$ไม$ส์�ม�รู้ถท��
จัะต�ดส์�นใจัห้รู้�อแก�ป0ญห้�ใดๆได�ด�วยล็��พิ�ง มน&ษย�จัะต�องเป*นผ��ก��ห้นดแล็ะส์��งก�รู้คอมพิ�วเตอรู้�ให้�ท��ง�นให้�
ได�อย$�งท��มน&ษย�ต�องก�รู้
โปรแกรมคอมพิวเตอร� ห้ม�ยถ8ง ชุ&ดล็��ด�บของค��ส์��งท��ก��ห้นดให้�คอมพิ�วเตอรู้�ท��ก�รู้ปรู้ะมวล็ผล็ข�อม�ล็เพิ��อให้�แก�ป0ญห้� แล็ะท��ง�นได�อย$�งท��ต�องก�รู้
Programming = Designing and writing a computer program.
22
ชุน�ดของภ�ษ�ในก�รู้เข�ยนโปรู้แกรู้ม There are 3 kind of programming
languages Machine language (0’s and 1’s) Assembly language (mov, and, or,
etc…) High-level language (nearly like human
word) Computer itself understands only
Machine language.
24
Language translator……main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movl $0, %eax subl %eax, %esp subl $8, %esp pushl $.LC0……..
class MainClass{ public static void Main(string[] args) { Console.WriteLine("Hello World!"); }}
Interpreter/ Compiler
High-level language
Assembly language
….
00011000110001110
00110001110101111
00011111111110001
11011100001011011
……
Machine language
Hello World!
_
Assembler
Machine
25
Interpreter Read high-level language , translate ,
and then execute one command at a time.
….writeln(‘a’);writeln(‘b’);writeln(‘c’);….
Inter-
preter
0100100
0100101
0100110
abc
Source
26
Compiler Read all program at a time. Translate into executable file (machine
language)….writeln(‘a’);writeln(‘b’);writeln(‘c’);….
Compiler object file
library
Linker
….01001000100101 0100101.....
abc
Source
Exe file
27
ข�7นตอนในก�รู้เข�ยนโปรู้แกรู้ม ว�เครู้�ะห้�ป0ญห้� (Analysis)
รู้ะบ&ป0ญห้�ท��ต�องก�รู้จัะแก�ไข แบ$งป0ญห้�น�7นออกเป*นป0ญห้�ย$อยๆ ค�นห้�แนวค�ดแล็ะว�ธุ�ก�รู้ท��จัะแก�ป0ญห้�น�7นๆ
พิ�ฒน�โปรู้แกรู้ม (Implementation) เข�ยนโปรู้แกรู้มด�วยภ�ษ�คอมพิ�วเตอรู้� โดยอ�ศ�ยแนวค�ดก�รู้แก�ป0ญห้�
ท��ค�ดไว�ก$อนห้น�� Compile โปรู้แกรู้มท��เข�ยนข87นให้�เป*นภ�ษ�ท��เครู้��องอ$�นได�
ตรู้วจัส์อบแล็ะแก�ไขจั&ดผ�ดพิล็�ด (Testing & Debugging) ทดล็องรู้�นโปรู้แกรู้มแล็ะตรู้วจัส์อบก�รู้ท��ง�น ห้�กโปรู้แกรู้มท��ง�นไม$ถ�กต�อง ห้�จั&ดผ�ดให้�พิบ แก�ไขจั&ดผ�ดพิล็�ดแล็ะทดล็องให้ม$ จันกว$�โปรู้แกรู้มจัะท��ง�นได�ถ�กต�อง
28
Analysis Example : Circle area problem Input
circle radius (รู้�ศม�) Process
calculate circle area Output
Circle area
29
Analysis Pseudo-code (รู้ห้�ส์เท�ยม)
ใชุ�ค��ท��ม�คว�มห้ม�ย คนอ$�นแล็�วเข��ใจั ข�7นตอนก�รู้ท��ง�นเป*นแบบคอมพิ�วเตอรู้�
ExampleProgram Circle
Begin
read circle radius
area = Pi * radius * radius
write circle area
End.
30
Test & Debug Compile โปรู้แกรู้มท��เข�ยนข87นแล็�วทดล็องรู้�น ตรู้วจัส์อบผล็ก�รู้ท��ง�น แก�ไขข�อผ�ดพิล็�ด (bugs)
31
Implement เข�ยนโปรู้แกรู้มโดยใชุ�ภ�ษ� Assembly ห้รู้�อ
ภ�ษ�รู้ะด�บส์�ง เรู้�ส์�ม�รู้ถใชุ�แนวค�ดเด�ยวก�น เข�ยนโปรู้แกรู้ม
ได�ห้ล็�ยภ�ษ� Example
class MainClass{ public static void Main(string[] args) { const double pi = 3.1416;
int radius; double area;
radius = int.Parse(Console.ReadLine()); area = pi*radius*radius; Console.WriteLine(area); }}
#define PI 3.1416fvoid main(){
int radius;float area;
scanf(“%d”,&radius);area = PI * radius * radius;printf(“%f”,area);
}
32
Test & Debug There are 2 kinds of bugs
Syntax bug Semantic bug
class MainClass{ public static void Main(string[] args) { const double pi = 3.1416;
int radius; double area;
radius = int.Parse(Console.ReadLine()); area = pi * raddius * radius; Console.WriteLine(area); }}
class MainClass{ public static void Main(string[] args) { const double pi = 3.1416;
int radius; double area;
radius = int.Parse(Console.ReadLine()); area = pi * radius; Console.WriteLine(area); }}
33
Programming Languages
The computer really only knows its own machine language or assembly language. Any high level languages would have be translated into machine language.
An assembler translates an assembly language program into machine language.
A compiler translates a high-level language into machine language.
A source program is written in a high-level programming language. (.cs, .pas, .c)
An executable program is the machine language version of a source program. (.exe)
35
C# Program C# is an object-
oriented programming language.
Everything must be in some class.
A program is a set of class declarations.
แดง
ขาวด�าง
โฮ่�ง
สุ�นั�ข
class
38
C# Program: What did you see?
Grouping using { } Statement ending with ; Various keywords: using,
namespace, class, public, static, void
"Hello World!"
39
C# Program: class declaration
Class name Member declarations
Data member Method (or functions)
In the beginning of the class, we usually use only one method: Main --- which is where our program starts.
class MainClass{ public static void Main(string[] args) { const double pi = 3.1416;
int radius; double area;
radius = int.Parse(Console.ReadLine()); area = pi*radius*radius; Console.WriteLine(area); }}
40
Basic grouping { } { curly brackets are used to group
things together in C# } { they come in pairs, and { they can
{ be nested } } } Example:
41
Semi-colon; In C#, every statement must end in a semicolon. Compiler uses semicolon to find where one
statement ends. It does not use line breaks or any textual markers.
For example, the following are the same programs (for the compiler):
using System; namespace Hello{ class MainClass {public static void Main(string[] args) { Console.WriteLine("Hello World!");}}}
using System; namespace Hello { class MainClass { public static void Main(string[] args) { Console.WriteLine("Hello World!"); } }}
42
Various keywords Various keywords help the C#
compiler to understand our programsabstract as base bool break byte case
catch char checked class const continue decimal
default delegate do double else enum event
explicit extern false finally fixed float for
foreach goto if implicit in int interface
internal is lock long namespace new null
object operator out override params private protected
public readonly ref return sbyte sealed short
sizeof stackalloc static string struct switch this
throw true try typeof uint ulong unchecked
unsafe ushort using virtual void volatile while
43
Keywords namespace - Programs are
organized using “namespace.” using - specifies external
namespaces that the program uses.
class - defines class. (remember that our program is one of the classes.)
44
Keywords Other keywords we are using:
void, public, static, etc. At this point, it is difficult to
understand them fully. You just have to remember the patterns that we use them.
45
using System;class MyClass { // This is an example class, // and this is a line comment… static void Main() { /* Sometimes, we love to comment things, and anything inside thes markers are comments */ Console.WriteLine("Hello!"); // chill~ chill~~ }}
/* comments */Comments are inserted into C# program by enclosing it within /* and */ , or after //Comments are ignored by the computer, but helpful to explain the program
47
Main declaration Here is how we declare our Main function
(inside our class):static void Main() { // โปรู้แกรู้มของเรู้�จัะอย�$ต$อจั�กน�7 …
}
Remark: there are 4 ways to define Main function. We use the simplest one.
static void Main() {...}static void Main(string[] args) {...} static int Main() {...} static int Main(string[] args) {...}
48
Inside method Main Variable
declarations Statements
public static void Main(string[] args) { const double pi = 3.1416;
int radius; double area;
radius = int.Parse(Console.ReadLine()); area = pi*radius*radius; Console.WriteLine(area); }
49
Variables Example:
Variables are used to store “data.” They must be declared before
used. To declare them, we must specify
their “types.”
int radius;double area;
50
DATA - information that must be supplied to a program before it can produce results.
Basic data types: int - whole numbers
34 -90 0 112 double - numbers with fractions
34.345 0.0 bool
true or false char - Computer character set string - strings
Data
51
Data has to be stored and referenced. Easiest way to refer to data is to give it a symbolic
name or identifier Data in a program can be classified as Constant
data and Variable data.
Data
52
Variable is a container for a data value.
A variable is created via a declaration where both its name and type are specified
Every variable has a name, type and value.
Variables
53
Variable declaration Syntax:
<type> <name>;
Examples:
We can also assign its initial value. E.g.,
int radius;double area;int a,b,c;bool isokay;
int k = 200;bool done = false;
55
Constants Constants can be used in
programs. E.g., 5, 10.4, "Hello" are constants.
There are also named constants, which are declared in the same way as variables, but with additional const keyword.
Examples:const double pi = 3.1416;const string message = "Hello";
56
Statements Examples:
The first two are assignments. The third one writes the value of
“area” to the screen.
radius =
int.Parse( Console.ReadLine() );
area = pi*radius*radius;
Console.WriteLine(area);
57
Methods (again) We use various methods from
various classes.
Console and int are standard classes.
int.Parse
Console.ReadLine
Console.WriteLine
58
Exampleusing System;namespace Hello{ class MainClass { static void Main(string[] args) {
double price, money; int n; double leftover;
Console.Write("Enter candy price: "); price = double.Parse(Console.ReadLine()); Console.Write("Enter number of candies: "); n = int.Parse(Console.ReadLine()); Console.Write("Enter your money :"); money = double.Parse(Console.ReadLine());
leftover = money - price * n;
Console.WriteLine("You have {0} bahts left.", leftover); } }}
60
Identifiers Symbolic names for program elements
Program name Variable name Data Type name Etc.
Rules for constructing identifiers Letters, digits, and under score (_) First character letter Can be long (63 char) Reserved words (keywords) are not allowed
61
Identifier examples Valid examples
score, count, total score1, count99, total09 score_1, count_99, total_99 myStudentId my_student_id
Invalid examples point&score total-number 9points