academic misconduct input/output conditionals writing a simple algorithm

40
Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Upload: charlotte-spencer

Post on 17-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Academic MisconductInput/OutputConditionals

Writing a Simple Algorithm

Page 2: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Who are these people and why

are they looking at me?

Page 3: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Academic Misconduct

Page 4: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

From the syllabus...

All assignments must reflect an individual effort, and must be completed "from scratch". It is a violation of the honor code to copy or derive solutions from textbooks, internet resources or previous instances of this course. Copying of solutions from other students, including those who previously took the course is prohibited. A good guideline is that you must be able to explain and/or reproduce anything that you submit.

Page 5: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Dear CS1311 Students,Be forewarned about the consequences of cheating in CS 1311, and give them serious thought. In the end your costs strongly outweigh your benefits. Even though the class can be confusing and sometimes even overwhelming, you must not let your frustration get the best of you. When and if you are still unsure just turn in what you have, don’t take the risk of copying someone else’s work. Taking the attitude that you are invincible and that you will never get caught could land you in the Dean’s office at the end of the quarter. So don’t take the cheat finder lightly because it really does exist (for homework and lab projects!) Unfortunately, as a former CS 1311 student, I can personally assure that if you are going to cheat you are going to pay a heavy toll for it. “Smart people learn from their own mistakes, geniuses learn from other’s mistakes.”

Sincerely,

Anonymous CS 1311 Student

Page 6: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

What is cheating?

• Cheating is gaining an unfair advantage over others and/or misrepresenting yourself on your work for academic gain.

• The intent to cheat is not a prerequisite for cheating to occur.

Page 7: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Consequences

• F in the course is probable• University probation• Mandatory participation in community service

hours• Loss of Hope Scholarship or any other

scholarship• Lowered GPA- with possible probation or even

expulsion from the university• Loss of job opportunities• Possible ineligibility for the co-op program

Page 8: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Questions?

Page 9: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Input and Output

Page 10: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Input and Output

I/O Operators allow us to communicate with the “outside world,” the world beyond the algorithm itself.

We’re not concerned with formatting.

Page 11: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Print

Displays output items to the user

Syntax:print(item_1, item_2, ..., item_n)

Examples:print(“Please enter your info.”)print(num_one, my_char, is_Student)

Page 12: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Read

Obtains input items from the user

Syntax:read(item_1, item_2, ..., item_n)

Examples:read(menu_choice)read(is_Student, name, age)

No automatic prompting!

Page 13: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Input and Output Examplesalgorithm IO_Example num_one, num_two, average isoftype Num

// obtain two numbers print(“Please enter two numbers”) read (num_one, num_two)

// output a literal text message and the// value of the sum print (“Sum = ”, num_one + num_two)

// output a string literal and average average <- (num_one + num_two) / 2 print (“Average = “, average)endalgorithm

Page 14: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

ESP Formatting

• Conventional languages require you to learn complex, intricate and often tricky formatting techniques.

• With pseudocode the situation is much simpler• All tricky formatting manipulations are

accomplished by just thinking about it and it happens

• Leading edge pseudocode...

...it’s what’s for dinner.

LB

Page 15: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Questions?

Page 16: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Conditionals

Page 17: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Relational Operators

• Greater than >• Greater or equal >=• Equal to =• Less than or equal <=• Less than <• Not equal to < >

Page 18: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Using Relational Operators

Combining relation operators with operands generates a boolean value

Examples:

4 > 5 FALSE

“apple” <= “cat” TRUE (alphabetic)

5 <> 6 TRUE

42 = 42 TRUE

‘g’ > ‘x’ FALSE LB

Page 19: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Boolean Operators

Boolean operators allow us to express compound expressions.

– AND

– OR

– NOT

<Boolean Value> <Boolean Operator> <Boolean Value>

LB

Page 20: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

AND

AND - all conditions must be true

((5>4) AND (4>7)) is FALSE TRUE FALSE

AND F T

F F F

T F T

In use there wouldbe some variables!

Page 21: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

OR

OR - only one condition must be true

((5>4) OR (4>7)) is TRUE TRUE FALSE

OR F T

F F T

T T T

Page 22: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

NOT

• NOT - reverses the boolean value

NOT (4>7) is TRUE FALSE

NOT

F T

T F

Page 23: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Boolean Expressions

Boolean expressions evaluate to TRUE or FALSE and are composed of- boolean variables(is_too_young) // a boolean

- expressions with relational operator(10 < y)

- expression with logical operators((10 >= x) AND (x < 20)) OR (y = -4)

(0 < age < 20) is not legal and should be written ((0 < age) AND (age < 20)).

Page 24: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

LB

• Names should be chosen to make clear what the true or false value represents

Naming Boolean Variables

• Bad Examples

red_or_green

gender

pass_fail

on_off

• Good Examples

is_red

is_male

did_pass

is_on

Page 25: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Decision StatementsConditionals allow us to make decisions based upon simple

logic. Results of conditionals are always TRUE or FALSE, i.e., boolean values.

Natural Language:“The number of hours is greater than or equal to 15.”

Picture:

num_of_hours >= 15

Code:

if (num_of_hours >= 15) then...

TRUE

FALSE

Page 26: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Simple Conditionals

Allows for some instructions to be conditionally executed.

Basic template:

if ( BOOLEAN_EXPRESSION ) then any instructionsendif

Page 27: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

If-Then-Endif

if ( BOOLEAN_EXPRESSION ) then any instructionsendif

If the BOOLEAN_EXPRESSION is TRUE, then execute all of the instructions in the then clause and continue the algorithm.

If the BOOLEAN_EXPRESSION is FALSE, then skip to the endif and continue the algorithm.

Page 28: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

If-Then-Endif Example

wake up

if ( is_raining ) then stay inside and play board games go to see a movie read a book call friendsendif

go to sleep

Page 29: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

If-Then-Else-Endif

if ( BOOLEAN_EXPRESSION ) then any instructions to execute if trueelse any instructions to execute if falseendif

If the BOOLEAN_EXPRESSION is TRUE, then execute all of the instructions in the then clause until you reach else, then skip down to the endif and continue the algorithm.

If the BOOLEAN_EXPRESSION is FALSE, then execute all of the instructions in the else clause and continue the algorithm.

Cannot do both the if clause and the else clause – only one or the other.

Page 30: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

If-Then-Else-Endif Example

if ( is_raining ) then

stay inside and read a book

go to see a movie

else

go to the park

go swimming

endif

go to sleep

Page 31: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Nested Conditionals

if (today = Sunday) then if (NOT raining) then go to the park else stay home & read a book endifelse go to workendif

Conditionals can reside inside other conditionals:

Page 32: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

The Optional Elseif Clause

Often we want a way to select one from many:

if (grade >= 90) then print(“You get an ‘A’”)elseif (grade >= 80) then print(“You get a ‘B’”)elseif (grade >= 70) then print(“You get a ‘C’”)elseif (grade >= 60) then print(“You get a ‘D’”)else // the default print(“Sorry, you get an ‘F’”)endif

Why not:grade>= 80 AND < 90

?

Page 33: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

A Complex Conditional

if (withdrew_from_course = TRUE) then

print(“Sorry to see you leave”)

elseif (((quiz_grade >= 60) or

(final_exam >= 60)) and

(did_homework)) then

print(“You pass the course”)

else

print(“Sorry, see you next term”)

endif

Page 34: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

A Complex Conditional

if (withdrew_from_course) then

print(“Sorry to see you leave”)

elseif (((quiz_grade >= 60) or

(final_exam >= 60)) and

(did_homework)) then

print(“You pass the course”)

else

print(“Sorry, see you next term”)

endif

Page 35: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Questions?

Page 36: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Writing Algorithms

Page 37: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Recipe for Writing Algorithms

• Write the shell• Outline the logic• Code the main ideas

• Verify declarations• Verify constants• “Walk” through the code

- syntax errors- logic errors

Note that you still have work to doafter you’ve finished writing the code!

Page 38: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

radius isoftype Numdiameter isoftype Numcircumf isoftype Num

print(“Diameter is: “, diameter)print(“Circumference is: “, circumf )

Writing a Simple Algorithm

algorithm// constants

// declarations

// program

endalgorithm

• Write the shell

Get_Circumference

// Get_Cirumference

• Name the algorithm

// Get the data

// Compute circumference

// Output the answer

• Outline the logic

• Fill in the algorithm

print(“Enter the radius”)read( radius )

• Declare the variables

• Define the constants

PI is 3.14159265

• Walk the code:

Enter the radius:

= 3= 6

= 18.85

Diameter is: 6Circumference is: 18.85

3diameter <- radius * 2circumf <- diameter * PI

Done!

Page 39: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm

Questions?

Page 40: Academic Misconduct Input/Output Conditionals Writing a Simple Algorithm