Idiotproofing Lesson CS1313 Spring 2015 1 Idiotproofing Outline 1.Idiotproofing Outline 2.Idiotproofing 3.Idiotproofing Quotes 4.An Idiotproof Website.

Slides:



Advertisements
Similar presentations
Standard I/O Lesson Outline
Advertisements

Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
While Loop Lesson CS1313 Spring while Loop Outline 1.while Loop Outline 2.while Loop Example #1 3.while Loop Example #2 4.Repetition and Looping.
CS0004: Introduction to Programming Repetition – Do Loops.
C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.
C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.
Introduction to C Programming
File I/O Lesson CS1313 Spring File I/O Lesson Outline 1.File I/O Lesson Outline 2.File I/O Using Redirection #1 3.File I/O Using Redirection #2.
Software Lesson #1 CS1313 Spring Software Lesson 1 Outline 1.Software Lesson 1 Outline 2.What is Software? A Program? Data? 3.What are Instructions?
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
CS 201 Functions Debzani Deb.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Introduction to C Programming
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
User Defined Functions Lesson 2 CS1313 Spring User Defined Functions 2 Outline 1.User Defined Functions 2 Outline 2.Argument Order When Passing.
For Loop Lesson 3 CS1313 Spring for Loop Lesson 3 Outline 1. for Loop Lesson 3 Outline 2. for Loop with a float Counter: BAD! 3. float Counter Example.
Characters & Strings Lesson 2 CS1313 Spring Characters & Strings Lesson 2 Outline 1.Characters & Strings Lesson 2 Outline 2.Character String Declaration.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Array Lesson 1 CS1313 Spring Array Lesson 1 Outline 1.Array Lesson 1 Outline 2.Mean of a List of Numbers 3.mean: Declarations 4.mean: Greeting,
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Numeric Data Types Lesson CS1313 Spring Numeric Data Types Outline 1.Numeric Data Types Outline 2.Data Types 3.Integers in Mathematics 4.Integers.
Array Lesson 2 CS1313 Spring Array Lesson 2 Outline 1.Array Lesson 2 Outline 2.Reading Array Values Using for Loop #1 3.Reading Array Values Using.
For Loop Lesson 1 CS1313 Spring for Loop Lesson 1 Outline 1. for Loop Lesson 1 Outline 2.A while Loop That Counts #1 3.A while Loop That Counts.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to.
1 CS161 Introduction to Computer Science Topic #9.
Variables & Constants Lesson 2 Outline
Boolean Data Lesson CS1313 Fall Boolean Data Outline 1.Boolean Data Outline 2.Data Types 3.C Boolean Data Type: char or int 4.C Built-In Boolean.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
User Defined Functions Lesson 1 CS1313 Spring User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough.
CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Arithmetic Expressions Lesson #2 Outline 1.Arithmetic Expressions Lesson #2 Outline 2.Named.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
If Lesson 1 CS1313 Fall if Lesson 1 Outline 1.if Lesson 1 Outline 2.Absolute Value 3.Absolute Value Definition 4.Absolute Value Implementation 5.What.
Idiotproofing Lesson CS1313 Spring Idiotproofing Outline 1.Idiotproofing Outline 2.Idiotproofing 3.Idiotproofing Quotes 4.Idiotproofing Example.
Algorithm: procedure in terms of
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
for Loop 1 Outline for Loop 1 Outline for Loop
CS1061 C Prgramming Lecture 11: Functions
Idiotproofing Outline
Character Strings Lesson Outline
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Array Lesson 1 Outline Array Lesson 1 Array Lesson 1 Outline
while Loop Outline while Loop Outline while Loop Example #1
User Defined Functions 2 Outline
File I/O Lesson Outline
Structured Program
Chapter 3 - Structured Program Development
Nested if Lesson Outline
for Loop Lesson 3 Outline
Chapter 3 - Structured Program Development
© 2016 Pearson Education, Ltd. All rights reserved.
Arithmetic Expressions Lesson #2 Outline
More Loops Topics Counter-Controlled (Definite) Repetition
Variables Lesson Outline
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Relational Operators Logical Operators for Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Arithmetic Expressions Lesson #1 Outline
CPS125.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Idiotproofing Lesson CS1313 Spring Idiotproofing Outline 1.Idiotproofing Outline 2.Idiotproofing 3.Idiotproofing Quotes 4.An Idiotproof Website 5.Idiotproofing Example #1 6.Idiotproofing Example #2 7.Idiotproofing Example #3 8.Idiotproofing Example #4 9.Why We Idiotproof 10.The exit Statement #1 11.The exit Statement #2 12.The exit Statement #3 13.The exit Statement #4 14. exit Example’s Flowchart 15.A New File to #include 16. exit Statement Inside an if Block #1 17. exit Statement Inside an if Block #2 18. exit Statement Inside an if Block #3 19. exit Statement Inside an if Block #4 20.Idiotproofing Example’s Flowchart

Idiotproofing Lesson CS1313 Spring Idiotproofing Idiotproofing means ensuring that a user’s input is valid.

Idiotproofing Lesson CS1313 Spring Idiotproofing Quotes “Idiotproofing is difficult because idiots are so clever.” “You can't make anything idiot proof because idiots are so ingenious.” ― Ron Burns “Idiotproofing causes evolutionary selection of more ingenious idiots.” “Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” – Rich Cook “It doesn’t really matter what effort you put into idiot-proofing a product or procedure. They will always build a better idiot.” “Idiot-proofing assumes a finite number of idiots.” “Campaigns to bearproof all garbage containers in wild areas have been difficult because, as one biologist put it, ‘There is a considerable overlap between the intelligence levels of the smartest bears and the dumbest tourists’.”

An Idiotproof Website Idiotproofing Lesson CS1313 Spring

Idiotproofing Lesson CS1313 Spring Idiotproofing Example #1 #include int main () { /* main */ const float minimum_area = 0; const int program_success_code = 0; const int program_failure_code = -1; float area_in_acres;

Idiotproofing Lesson CS1313 Spring Idiotproofing Example #2 printf("What is the area "); printf("in acres?\n"); scanf("%f", &area_in_acres); if (area_in_acres < minimum_area) { printf("ERROR: you can’t have a negative"); printf(" area!\n"); exit(program_failure_code); /* <--- NOTICE! */ } /* if (area_in_acres < minimum_area) */

Idiotproofing Lesson CS1313 Spring Idiotproofing Example #3 /* * ASSERT: By the time the program gets to here, * the area must be valid. */ printf("The area is valid.\n"); return program_success_code; } /* main */

Idiotproofing Lesson CS1313 Spring Idiotproofing Example #4 % gcc -o conversions_idiot conversions_idiot.c % conversions_idiot What is the area in acres? -100 ERROR: you can't have a negative area! % conversions_idiot What is the area in acres? 100 The area is valid.

Idiotproofing Lesson CS1313 Spring Why We Idiotproof Idiotproofing ensures that input data are valid, which means that, if our program is otherwise correct, then the output will be valid as well. Idiotproofing allows us to assert certain properties of the data. For example, in the conversions program, properly idiotproofed input data allow us to assert that, in the calculation section, the area in acres is valid. So, our calculations can assume this fact, which sometimes can be more convenient.

Idiotproofing Lesson CS1313 Spring The exit Statement #1 % cat exitexample.c #include int main () { /* main */ const int program_failure_code = -1; printf("This statement will be always be executed.\n"); exit(program_failure_code); printf("This statement will be never be executed.\n"); } /* main */ % gcc -o exitexample exitexample.c % exitexample This statement will be always be executed. The exit statement terminates execution of a given run of a program. NOTICE!

Idiotproofing Lesson CS1313 Spring The exit Statement #2 % cat exitexample.c #include int main () { /* main */ const int program_failure_code = -1; printf("This statement will be always be executed.\n"); exit(program_failure_code); printf("This statement will be never be executed.\n"); } /* main */ % gcc -o exitexample exitexample.c % exitexample This statement will be always be executed. The program terminates in a controlled, graceful way – that is, it doesn’t actually crash – without executing the remaining executable statements. NOTICE!

Idiotproofing Lesson CS1313 Spring The exit Statement #3 % cat exitexample.c #include int main () { /* main */ const int program_failure_code = -1; printf("This statement will be always be executed.\n"); exit(program_failure_code); printf("This statement will be never be executed.\n"); } /* main */ % gcc -o exitexample exitexample.c % exitexample This statement will be always be executed. Notice that the exit statement takes an int argument. This argument represents the value that will be returned by the program to the operating system (for example, Linux). By convention, returning 0 from a program to the OS means that the program completed successfully, so if the program is exiting prematurely, then you should return a non-zero value. NOTICE!

Idiotproofing Lesson CS1313 Spring The exit Statement #4 % cat exitexample.c #include int main () { /* main */ const int program_failure_code = -1; printf("This statement will be always be executed.\n"); exit(program_failure_code); printf("This statement will be never be executed.\n"); } /* main */ % gcc -o exitexample exitexample.c % exitexample This statement will be always be executed. Jargon: In the context of running a program, all of the following terms are generally used to mean the same thing: exit, stop, halt, terminate, abort. NOTICE!

Idiotproofing Lesson CS1313 Spring exit Example’s Flowchart printf("This statement will be always be executed.\n"); exit(program_failure_code); printf("This statement will be never be executed.\n"); Apparent FlowchartActual Flowchart Notice that the symbol for an exit is also an oval.

Idiotproofing Lesson CS1313 Spring A New File to #include % cat exitexample.c #include int main () { /* main */ const int program_failure_code = -1; printf("This statement will be always be executed.\n"); exit(program_failure_code); printf("This statement will be never be executed.\n"); } /* main */ % gcc -o exitexample exitexample.c % exitexample This statement will be always be executed. To use an exit statement, you MUST include an additional header file, IMMEDIATELY AFTER stdio.h : #include NOTICE!

Idiotproofing Lesson CS1313 Spring exit Statement Inside an if Block #1 if (area_in_acres < minimum_area) { printf("ERROR: you can’t have a negative"); printf(" area!\n"); exit(program_failure_code); /* <--- NOTICE! */ } /* if (area_in_acres < minimum_area) */ When you put an exit statement inside an if block, the exit statement will be executed only in the event that the appropriate clause of the if block is entered, and then only after all prior statements in that clause of the if block have already been executed.

Idiotproofing Lesson CS1313 Spring exit Statement Inside an if Block #2 if (area_in_acres < minimum_area) { printf("ERROR: you can’t have a negative"); printf(" area!\n"); exit(program_failure_code); /* <--- NOTICE! */ } /* if (area_in_acres < minimum_area) */ In the above example, the exit statement is executed only in the event that the area is negative, and only after executing the printf statement that precedes it.

Idiotproofing Lesson CS1313 Spring exit Statement Inside an if Block #3 if (area_in_acres < minimum_area) { printf("ERROR: you can’t have a negative"); printf(" area!\n"); exit(program_failure_code); /* <--- NOTICE! */ } /* if (area_in_acres < minimum_area) */ Notice that the exit statement DOESN’T have to have a comment after it.

Idiotproofing Lesson CS1313 Spring exit Statement Inside an if Block #4 if (area_in_acres < minimum_area) { printf("ERROR: you can’t have a negative"); printf(" area!\n"); exit(program_failure_code); /* <--- NOTICE! */ } /* if (area_in_acres < minimum_area) */ Notice that the exit statement is inside the if block and therefore is indented MORE than the if statement.

Idiotproofing Lesson CS1313 Spring Idiotproofing Example’s Flowchart if (area_in_acres < minimum_area) { printf("ERROR: you can’t have a negative"); printf(" area!\n"); exit(program_failure_code); } /* if (area_in_acres < minimum_area) */ area < 0?