20
Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

Embed Size (px)

Citation preview

Page 1: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

Comparisonthe RANGE block

Day 8

Computer Programming through Robotics

CPST 410

Summer 2009

Page 2: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

2

Course organization

Course home page (http://robolab.tulane.edu/CPST410/)

Lab (Newcomb 442) will be open for practice with 3-4 Macs, but you can bring your own laptop and all robots.

Page 3: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

Comparison in NXC

Page 4: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

4

Comparison and strings

Comparison operators: >, <, ==String operators

string [declares a variable of type string]NumtoStr [converts a number to a string]StrCat [concatenates strings]TextOut [displays strings]

Page 5: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

5

The challenge, again

Tribot, 1. create two random numbers between 0 and 9

(call them A and B),

2. display them on the screen,

3. and tell me if A is greater than B.

Page 6: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

6

Parts 1 and 2

int A, B;string a, b, combined;task main(){

A = random(10);B = random(10);a = NumtoStr(A);b = NumtoStr(B);combined = StrCat(a, “ < “, b);TextOut(0, 0, combined);Wait(1000);

}

Page 7: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

7

Part 3, literal option

int A, B;string a, b, combined;task main(){

A = random(10);B = random(10);a = NumtoStr(A);b = NumtoStr(B);if (A > B) combined = StrCat(a, “ > “, b);else if (B > A) combined = StrCat(a, “ < “, b);TextOut(0, 0, combined);Wait(1000);

}

Page 8: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

8

Part 3, more complete option

int A, B;string a, b, combined;task main(){

A = random(10);B = random(10);a = NumtoStr(A);b = NumtoStr(B);if (A > B) combined = StrCat(a, “ > “, b);else if (B > A) combined = StrCat(a, “ < “, b);else combined = StrCat(a, “ = “, b);TextOut(0, 0, combined);Wait(1000);

}

Page 9: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

9

Question

Could you use an NXC switch?No, an an NXC switch only takes numbers

(integers) as inputs.

Page 10: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

Inside or out?

Kelly §16

Page 11: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

11

The challenge

Tribot, 1. Create a random number between 0 and 100,

and show it on the screen.

2. Tell me if it is between 40 and 60.

Page 12: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

12

DisplayRange1.rbt

displays random

number on line 7

displays "between 40 and 60" on

line 5 - don't clear the screen!

Page 13: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

13

The RANGE block

Robots respond to statements with True or FalseA is inside the range of numbers beginning

with B and ending with C. A is outside the range of numbers beginning

with B and ending with C.

Page 14: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

14

Part 2

Drag a RANGE block out of the Data row and drop it at the end of the beam.Operation = InsideA = 40B = 60Where does its input come from?From RANDOM.

Page 15: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

15

Part 2, cont.

We again want to display both the True and False responsesDrop a SWITCH block at the end of the beam and set it

up.Control = ValueType = LogicUncheck Flat view (to save screen space)

Drop a DISPLAY block into each choice = "True"x = "False"

Drop a NXT button WAIT block at the end to keep the results on the screen

Page 16: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

16

DisplayRange2.rbt

Page 17: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

17

Range in NXC

There is no such operation as RANGE in NXC,

so you have to break it down into its components,

using the logical operators:&& [logical AND]|| [logical OR]! [logical negation]

Page 18: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

18

The challenge, again

Tribot, 1. Create a random number between 0 and 100,

and show it on the screen.

2. Tell me if it is between 40 and 60.

Page 19: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

19

NXC version

int A;string a, combined;task main(){

A = random(10);a = NumtoStr(A);if (A > 40) && (A < 60) combined = StrCat(a, “ is between 40 and 60”);else combined = StrCat(a, “ is not between 40 and 60”);TextOut(0, 0, combined);Wait(1000);

}

Page 20: Comparison the RANGE block Day 8 Computer Programming through Robotics CPST 410 Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

20

Next time

The LOGIC block.Math, power, files: Kelly 20-22.Bluetooth messaging: Kelly 25.Tasks, routines, subroutines: Kelly 26.