Tải bản đầy đủ (.ppt) (73 trang)

Computer Science 1000

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.74 MB, 73 trang )

Computer Science 1000
Algorithms


 Programs


we now have some idea on how computers execute
an instruction




a system of gates is connected to process a series of bits
in some fashion (e.g. adding binary numbers)

your processor has a list of instructions that it can
perform
arithmetic: adding, subtracting, etc …
 memory operations: loading data from memory, saving
data to memory
 and many others



 Programs
 the

language of the processor is binary

 that



is, each instruction is a sequence of binary
digits
 e.g. consider our example from term1.ppt


From the term1.ppt
slides.

 CPU

– Basic Operation

 read

an instruction
 execute that instruction
 repeat for next instruction

CPU

Memory
1

17

2

29


3

46

4

56

5

2576

Computer Program
0000100100010001
0000101000011101
0001001100101000
0000110000111000
0010010101110000

place value "17" in memory location 1
place value "29" in memory location 2
add values in loc. 1 and 2, place in loc. 3
place value "56" in memory location 4
multiply values in loc. 3 and 4, place in loc. 5


 Programming

programming in binary is considerably difficult
 fortunately, most software development today is

done using a high level programming language






C/C++, Java, Python, Visual Basic, etc.

a programming language represents a compromise
between natural language (human) and binary code
(computer)




Write program to compute the perimeter and area of a rectangle. The size of the
rectangle will be specified by the user.

#include <iostream>
using namespace std;
int main() {
int length;
cout << "Please enter the rectangle's length: ";
cin >> length;

From the term4.ppt
slides.
This is an example of a
computer program

written in C++.

int width;
cout << "Please enter the rectangle's width: ";
cin >> width;
int perimeter = 2 * (length + width);
int area = length * width;
cout << "The perimeter of the rectangle is " << perimeter << endl;
cout << "The area of the rectangle is " << area << endl;
return 0;
}




Algorithms




how does a program written in a programming language
become binary code that a processor can understand?
Answer: a compiler


a compiler is a program that creates other programs from highlevel code

int length;
cout << "Please enter the rectangle's
length: ";

cin >> length;
int width;
cout << "Please enter the rectangle's
width: ";
cin >> width;
int perimeter = 2 * (length + width);
int area = length * width;
cout << "The perimeter of the rectangle
is " << perimeter << endl;
cout << "The area of the rectangle is "
<< area << endl;

Compiler

0000100100010001
0000101000011101
0001001100101000
0000110000111000
0010010101110000


 Programming

programs written in a programming language are
typically made up of a set of statements
 roughly speaking, each statement defines an
operation that you would like the computer to
perform



output something to screen
 perform a mathematical operation
 save information to a file
 send a request for a webpage to a server
 etc …



 Programming


Language

in many respects, programming languages are
similar to a natural language


only accepts certain keywords




for example, the word while in C++ creates a loop, the word
kev has no meaning to C++

statements have a particular syntax that must be followed



blocks of code must be enclosed in { }

statements must end with a semicolon


 Programming


given a problem, a programmer’s task is to:
devise an algorithm for solving that problem
 translate that algorithm into source code
 compile the source code into a program that the computer
can understand




our topic for this week is to consider this task
in other words, a light introduction to programming
 our programming language: Scratch



 Scratch

a programming language and
environment written by MIT Labs
 originally intended to introduce
children to programming, Scratch
has become a popular choice for
introducing programming in other
settings (e.g. universities)

 freely available (GPL)



Source code
(source pane)

Available
operations.

Output


 Scratch
 in

“Code”

most programming languages, code is
written as text
 in Scratch, operations are represented as
blocks
 programs are arranged as sequences of
blocks arranged together


Source code
blocks.



 Sprites
 the

operations in Scratch control the
behaviour of the sprite
 different operations available
 moving/turning

sprite
 output (as a text bubble)
 sounds (drums, etc)
 etc …


 Scratch

– First Example

 write

a program where the sprite says
“Hello!”
 solution:
 find


the following block

under the Looks category


 drag

this block onto the source code pane



 Scratch
 write

– First Example

a program where the sprite says “Hello
World”



 Scratch
 to

– First Example

run the program, double click on the
block
 to reset the program (remove output), click
the stop sign in the top right corner



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×