CSI 1100 Lab - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

CSI 1100 Lab

Description:

Assignment 3 was made available on Oct. 1, and is due on Tuesday Oct. 12 at noon. Note that Mon. Oct. 11 is the Thanksgiving holiday, and that on Fri. Oct. 22, ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 24
Provided by: RPl5
Category:
Tags: csi | lab

less

Transcript and Presenter's Notes

Title: CSI 1100 Lab


1
CSI 1100Lab 4October 4-8
  • Adapted by Alan Williams
  • from slides by Romelia Plesa

2
Schedule Notes
  • Assignment 3 was made available on Oct. 1, and is
    due on Tuesday Oct. 12 at noon.
  • Note that Mon. Oct. 11 is the Thanksgiving
    holiday, and that on Fri. Oct. 22, all
    Engineering faculty lectures and labs are
    cancelled for the day.
  • Labs for next 2 weeks
  • Sections 1, 2, and 3 (Monday/Tuesday)
  • No labs next week (Oct. 11-12)
  • Regular labs on the following week (Oct. 18-19)
  • Sections 4, 5 (Thursday/Friday)
  • Regular labs next week (Oct. 14-15)
  • No labs on the following week (Oct. 21-22)

3
Labs Agenda
  • Boolean expressions
  • Branching and loop structures
  • Questions

4
Boolean Expressions
  • Evaluate to true or false
  • Translations from pseudocode to Java for
  • Pseudocode Java
  • ? (not a boolean expression)
  • AND
  • OR
  • NOT !
  • A B A B
  • A B A lt B
  • A ? B A gt B
  • A ? B A ! B

5
Boolean Expressions, Example 1
  • Write a test that returns TRUE if integer I is
    odd the test should return FALSE otherwise.

Pseudocode
Java // assume i has a value boolean odd if (i
2 0) odd false else odd
true
I mod 2 0 ?
odd ? TRUE
odd ? FALSE
6
Boolean Expressions Example 2
  • Write a test that returns TRUE if integer I is a
    multiple of positive integer K the test should
    return FALSE otherwise.

Pseudocode
Java // assume i, k have values boolean
multiple if (i k 0) multiple
true else multiple false
I mod K 0 ?
false
true
multiple ? TRUE
multiple ? FALSE
7
AND and OR
  • Used for combining conditions
  • Use brackets to make sure compound expressions
    mean what you want them to mean.
  • Anywhere our pseudocode language calls for a
    "test" you may use ANY Boolean expression
  • What is the value of the following expressions?

((room STE0131) OR (room STE0130)) AND (Lab
CSI1100)
TRUE
(I am at home) OR (I am in the office)
TRUE
(I am at home) AND (I am in the office)
FALSE
8
Boolean Expressions Example 3
  • Write a test that returns TRUE if x is between 10
    and 20 (inclusive) the test should return FALSE
    otherwise

Pseudocode
Java // assume x has a value boolean inRange if
( (xgt10) (xlt20) ) inRange
true else inRange false
X ? 10 AND X ? 20 ?
false
true
inRange ? TRUE
inRange ? FALSE
9
AND versus OR
  • In the last slide
  • We used ((xgt10) (xlt20)) to test whether x
    is between 10 and 20.
  • What if we used OR instead of AND
  • Suppose x is 7.
  • If we had ((xgt10) (xlt20))
  • xlt20 is TRUE, and so the entire expression is
    TRUE but x is not between 10 and 20.

10
Boolean Expressions, Example 4
  • Write a test that is TRUE if B's value is in
    between A's value and C's value (but, we don't
    know whether A is bigger than C or vice versa).

Pseudocode
Java if (((bgta) (bltc)) ((bgtc)
(blta))) // b is between a and c else
// b is outside range
(((B ? A) AND (B ? C )) OR ((B ? C) AND (B ? A)))
false
true
11
Example Fibonacci Numbers
  • Create an array containing N values (N is 1 or
    greater) such that
  • A0 1
  • A1 1
  • AI AI-1 AI-2
  • for all I greater than 1 lt I lt N.
  • For example, the array of length 7 would be
  • A 1 1 2 3 5 8 13

12
Fibonacci Algorithm
  • GIVENS N (a number ? 1)
  • RESULTS A (an array of Fibonacci numbers)
  • INTERMEDIATES I (index for the array)
  • HEADER
  • A ? Fibonacci(N)
  • BODY

Fibonacci Rule A0 1 A1 1 AI AI-1
AI-2
A0 ? 1 A1 ? 1 I ? 2
I lt N ?
true
false
I ? I 1 AI ? AI-1AI-2
13
Trace for N5
14
Array creation bug
  • Any algorithm that creates an array must call an
    array-creation algorithm
  • A ?MakeNewArray( size )
  • This creates an array with "size" positions.
  • In our example, it must be done as the very first
    statement.

15
Fibonacci Algorithm, version 2
  • GIVENS N (a number ? 1)
  • RESULTS A (an array of Fibonacci numbers)
  • INTERMEDIATES I (index for the array)
  • HEADER
  • A ? Fibonacci(N)
  • BODY

A ? MakeNewArray(5) A0 ? 1 A1 ? 1 I ? 2
Fibonacci Rule A0 1 A1 1 AI AI-1
AI-2
I lt N ?
true
false
I ? I 1 AI ? AI-1AI-2
16
Trace for N5
17
Fibonacci Algorithm, version 3
  • GIVENS N (a number ? 1)
  • RESULTS A (an array of Fibonacci numbers)
  • INTERMEDIATES I (index for the array)
  • HEADER
  • A ? Fibonacci(N)
  • BODY

A ? MakeNewArray(5) A0 ? 1 A1 ? 1 I ? 2
Fibonacci Rule A0 1 A1 1 AI AI-1
AI-2
I lt N ?
true
false
AI ? AI-1AI-2 I ? I 1
18
Trace for N5
19
Trace for N5
20
More loop examples . . .
  • Write a algorithm that calculates the sum of the
    first N terms of the series
  • 12 22 32 42 ...
  • Write a algorithm that returns the sum of odd
    integers from 1 up to no larger than N
  • 1 3 5
  • See if you can do this without a branch!

21
(No Transcript)
22
Sum of Squares
  • GIVENS N (a number ? 1)
  • RESULTS Sum (sum of squares up to N)
  • INTERMEDIATES Index (current value to square)
  • HEADER
  • A ? SumOfNSquared(N)
  • BODY

Sum ? 0 Index ? 1
Index ? N ?
true
false
Sum ? Sum Index Index Index ? Index 1
23
Sum of Odd Integers
  • GIVENS N (a number ? 1)
  • RESULTS Sum (sum of odd integers up to N)
  • INTERMEDIATES Index (current odd integer)
  • HEADER
  • A ? SumOddIntegers(N)
  • BODY

Sum ? 0 Index ? 1
Index ? N ?
true
false
Sum ? Sum Index Index ? Index 2
Write a Comment
User Comments (0)
About PowerShow.com