academic misconduct input/output conditionals writing a simple algorithm
TRANSCRIPT
Academic MisconductInput/OutputConditionals
Writing a Simple Algorithm
Who are these people and why
are they looking at me?
Academic Misconduct
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.
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
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.
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
Questions?
Input and Output
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.
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)
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!
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
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
Questions?
Conditionals
Relational Operators
• Greater than >• Greater or equal >=• Equal to =• Less than or equal <=• Less than <• Not equal to < >
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
Boolean Operators
Boolean operators allow us to express compound expressions.
– AND
– OR
– NOT
<Boolean Value> <Boolean Operator> <Boolean Value>
LB
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!
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
NOT
• NOT - reverses the boolean value
NOT (4>7) is TRUE FALSE
NOT
F T
T F
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)).
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
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
Simple Conditionals
Allows for some instructions to be conditionally executed.
Basic template:
if ( BOOLEAN_EXPRESSION ) then any instructionsendif
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.
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
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.
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
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:
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
?
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
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
Questions?
Writing Algorithms
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!
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!
Questions?