computer science 1000

73
Computer Science 1000 Algorithms

Upload: lee-lewis

Post on 30-Dec-2015

14 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Computer Science 1000

Computer Science 1000

Algorithms

Page 2: Computer Science 1000

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

Page 3: Computer Science 1000

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

Page 4: Computer Science 1000

CPU – Basic Operation read an instructionexecute that instruction repeat for next instruction

00001001000100010000101000011101000100110010100000001100001110000010010101110000

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

1

2

3

4

5

MemoryCPU

17

29

46

56

2576

00001001000100010000101000011101000100110010100000001100001110000010010101110000

00001001000100010000101000011101000100110010100000001100001110000010010101110000

00001001000100010000101000011101000100110010100000001100001110000010010101110000

00001001000100010000101000011101000100110010100000001100001110000010010101110000

00001001000100010000101000011101000100110010100000001100001110000010010101110000

Computer Program

From the term1.ppt slides.

Page 5: Computer Science 1000

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)

Page 6: Computer Science 1000

#include <iostream> using namespace std;

int main() {

return 0;}

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

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;

From the term4.ppt slides.

This is an example of a computer program written in C++.

Page 7: Computer Science 1000

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 high-level code

00001001000100010000101000011101000100110010100000001100001110000010010101110000

Compiler

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;

Page 8: Computer Science 1000

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 …

Page 9: Computer Science 1000

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

Page 10: Computer Science 1000

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

Page 11: Computer Science 1000

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)

Page 12: Computer Science 1000

Available operations.

Output

Source code (source pane)

Page 13: Computer Science 1000

Scratch “Code” in most programming languages, code is

written as text in Scratch, operations are represented as

blocksprograms are arranged as sequences of

blocks arranged together

Page 14: Computer Science 1000

Source code blocks.

Page 15: Computer Science 1000

Sprites the operations in Scratch control the

behaviour of the spritedifferent operations available

moving/turning spriteoutput (as a text bubble)sounds (drums, etc)etc …

Page 16: Computer Science 1000

Scratch – First Examplewrite a program where the sprite says

“Hello!”solution:

find the following block under the Looks category

drag this block onto the source code pane

Page 17: Computer Science 1000
Page 18: Computer Science 1000

Scratch – First Examplewrite a program where the sprite says “Hello

World”

Page 19: Computer Science 1000
Page 20: Computer Science 1000

Scratch – First Example to run the program, double click on the

block to reset the program (remove output), click

the stop sign in the top right corner

Page 21: Computer Science 1000
Page 22: Computer Science 1000
Page 23: Computer Science 1000

Outputprevious was an example of program outputeach programming language has its own

version of an output statement:C: printf(“Hello!”);Java: System.out.println(“Hello!”);Python: print “Hello!”

Page 24: Computer Science 1000

Changing Outputwe can change the output by modifying the

text in the white box for example, modify previous example so

that output becomes “Hello World!”

Page 25: Computer Science 1000
Page 26: Computer Science 1000

Movement in addition to text output, we can control the

position and direction of the sprite simple movement operations:

move in the direction that the sprite is facing turn clockwise turn counterclockwise

example: construct a program that moves the sprite 25 steps

Page 27: Computer Science 1000
Page 28: Computer Science 1000

Example #2: write a program that turns the sprite 15 degrees clockwise

Page 29: Computer Science 1000

Movement to reset the position of the sprite

double click:you may need to put zeroes in the boxes

to reset the direction of the spritedouble click:

Page 30: Computer Science 1000

Programsour programs so far have been composed

of single operations the real power of programming comes in

combining statements in a typical programming language, we

simply write more than one line of code in Scratch, we place more than one block in

our source pane, and attach them

Page 31: Computer Science 1000

Example: write a program in which the sprite moves 10 steps, and then says “Finished!”

Page 32: Computer Science 1000

Example: write a program that moves 100 steps, and then turns 45 degrees clockwise

Page 33: Computer Science 1000

Programsnote that the blocks execute from top to

bottom, one after the otherwe call this sequential execution

the sequence of our blocks can affect the final outcome of the program

for example, what if we reverse the order of our previous blocks?

Page 34: Computer Science 1000
Page 35: Computer Science 1000

Example #2: Write a program that writes “Hello …”, and then “… world!”

Page 36: Computer Science 1000

Timing in previous example, when we double click

on the code blocks, we only see “… world!”why?

the code blocks execute one after the other they execute so quickly, that we only see the

results of the second

Page 37: Computer Science 1000

Timinghow can we address this issue?

Solution #1 (text only): there is a block called Say Hello for 2 seconds we can set the message that we want to display, and

the amount of time that it should be displayed before executing the next block

Page 38: Computer Science 1000
Page 39: Computer Science 1000

Timinghow can we address this issue?

Solution #2 (general): there is a block in the control group called Wait 1 sec.

This block does not change the output, it simply waits the specified number of seconds before executing the next block.

note that this can be applied before any block

Page 40: Computer Science 1000
Page 41: Computer Science 1000

Example: write a program that moves the sprite around an imaginary square of size 100x100. The sprite should end up in its original position, facing the same direction remember to include some kind of delay,

otherwise, the sprite will appear not to move (because it occurs so quickly)

Page 42: Computer Science 1000
Page 43: Computer Science 1000

Entry Point most programs (such as those you are used to)

have an entry point tells the operating system where to start executing

the program in Scratch, we can designate the entry point to a

program using the When <green flag> clicked block program begins when green flag

clicked

Page 44: Computer Science 1000
Page 45: Computer Science 1000

Source vs. Executablewhen a programmer creates a program,

rarely do we ever see the source codewe only see, and run, the executable program

Scratch also allows us to run a program without seeing the source code

presentation modeclick the symbol in top-right corner

Page 46: Computer Science 1000
Page 47: Computer Science 1000

Arithmetic Operationsevery major computer language permits at

least some mathematical operationssimple: +, -, *, /not so simple: sin, cos, log …

Page 48: Computer Science 1000

Operators four basic arithmetic operators

these operators behave in the same way as they do in Excel

Operator Symbol

Addition +

Subtraction -

Multiplication *

Division /

Page 49: Computer Science 1000

Arithmetic Expression to use an arithmetic operator, use one of the

following blocks (from the operators group): in each white space, you put:

a numberanother arithmetic expression

for example, use Scratch to compute the value 86 + 43

Page 50: Computer Science 1000
Page 51: Computer Science 1000

The block:

in Scratch creates an expressionmore specifically, an arithmetic expression the value of the expression is:

whatever the formula evaluates to in the above example, the value of the

expression is 129

Page 52: Computer Science 1000

Where are arithmetic expressions used? anywhere an expression is valid in Scratch, this is usually in the white box

(input) of another block for example, we can have our sprite output

the value of our arithmetic expression place the expression block in the white box of

the Say block

Page 53: Computer Science 1000
Page 54: Computer Science 1000

Example 2: Write a program that moves the sprite 117+43 steps, and turns 46-19 degrees

instead of doing the math yourself, let Scratch do it

Page 55: Computer Science 1000
Page 56: Computer Science 1000

Math Expressions what will the result of running the following

program be?

Math expressions are only evaluated in their corresponding operator blocks.

Page 57: Computer Science 1000

Math Expressionssuppose I wanted to compute 1314 + 829 +

74 using Scratchhow would I do it?

1314 + 829 + 74 = (1314 + 829) + 74 in other words, there are two plus operators the value of the first plus operator becomes the

left input to the second operator

Page 58: Computer Science 1000
Page 59: Computer Science 1000

Math Expressionswrite a program in which the sprite tells us

the value of (22+13) / 72

Note that Scratch rounded our answer to 2 decimal places. The actual answer is 0.486111…

Page 60: Computer Science 1000

Joins an operator that allows us to concatenate two

values

the result of this operator is the left text, followed by the right text

Page 61: Computer Science 1000

Example: Write a program that prints 34x78 = , followed by the value of that expressionoutput should come from the sprite

Page 62: Computer Science 1000

A more useful example: write a program that converts 20 degrees

Fahrenheit to Celsius

Formula: )32(9

5 FC

Page 63: Computer Science 1000

A more useful example: write a program that converts 20 degrees

Fahrenheit to Celsius

Formula: )32(9

5 FC

Page 64: Computer Science 1000

What do you notice about the previous program?what happens if the temperature is 15

degrees? the source code itself would have to be edited

that is, all of our programs so far, produce exactly the same output unless we change the source code

Page 65: Computer Science 1000

InputScratch allows us to ask the user for a valueonce the user has entered a value, it can be

used in the program, like any other valueuse the following block:

Sprite will ask this question, and then wait for the user to input a value.

Page 66: Computer Science 1000
Page 67: Computer Science 1000

Input note that you can ask any question that you like

Page 68: Computer Science 1000

Inputour program is reading a value from the

userwhere does it go?

into a block called answer this block can be added to the input of another

block, and will be replaced with the user’s input for example: write a program that asks for the

user’s name, and then prints it on the screen.

Page 69: Computer Science 1000
Page 70: Computer Science 1000

Example: Convert the previous program, so that the output becomes Hello followed by the user’s name

Page 71: Computer Science 1000

Example: Convert the previous program, so that the output becomes Hello followed by the user’s name

Page 72: Computer Science 1000

Back to our temperature example: convert this program to:

ask the user for a temperature, in Fahrenheit print the corresponding value, in Celsius

Formula: )32(9

5 FC

Page 73: Computer Science 1000