For Loop Lesson 3 CS1313 Spring 2015 1 for Loop Lesson 3 Outline 1. for Loop Lesson 3 Outline 2. for Loop with a float Counter: BAD! 3. float Counter Example.

Slides:



Advertisements
Similar presentations
Standard I/O Lesson Outline
Advertisements

Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.
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.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
CS Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)
Idiotproofing Lesson CS1313 Spring Idiotproofing Outline 1.Idiotproofing Outline 2.Idiotproofing 3.Idiotproofing Quotes 4.An Idiotproof Website.
User Defined Functions Lesson 2 CS1313 Spring User Defined Functions 2 Outline 1.User Defined Functions 2 Outline 2.Argument Order When Passing.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
For Loop Lesson 2 CS1313 Spring for Loop Lesson 2 Outline 1. for Loop Lesson 2 Outline 2. for Loop Application 3. Factorial 4. Factorial Program.
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,
Numeric Data Types Lesson CS1313 Spring Numeric Data Types Outline 1.Numeric Data Types Outline 2.Data Types 3.Integers in Mathematics 4.Integers.
Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical.
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.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Structures Lesson CS1313 Spring Structures Lesson Outline 1.Structures Lesson Outline 2.Beyond Arrays 3.A Company and Its Employees #1 4.A Company.
Variables & Constants Lesson 2 Outline
1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i
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.
User Defined Functions Lesson 1 CS1313 Spring User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Arithmetic Expressions Lesson #2 Outline 1.Arithmetic Expressions Lesson #2 Outline 2.Named.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Idiotproofing Lesson CS1313 Spring Idiotproofing Outline 1.Idiotproofing Outline 2.Idiotproofing 3.Idiotproofing Quotes 4.Idiotproofing Example.
Sorting Lesson CS1313 Spring Sorting Lesson Outline 1.Sorting Lesson Outline 2.How to Sort an Array? 3.Many Sorting Algorithms 4.Bubble Sort #1.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
ESC101: Introduction to Computing
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Lecture 7: Repeating a Known Number of Times
for Loop 1 Outline for Loop 1 Outline for Loop
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
CS1010 Programming Methodology
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Idiotproofing Outline
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Array Lesson 1 Outline Array Lesson 1 Array Lesson 1 Outline
while Loop Outline while Loop Outline while Loop Example #1
- Additional C Statements
Sorting Lesson Outline
User Defined Functions 2 Outline
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Lec 6.
Chapter 4 - Program Control
Nested if Lesson Outline
for Loop Lesson 3 Outline
The while Looping Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Week 6 CPS125.
UMBC CMSC 104 – Section 01, Fall 2016
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arithmetic Expressions Lesson #2 Outline
Dale Roberts, Lecturer IUPUI
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
The while Looping Structure
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arithmetic Expressions Lesson #1 Outline
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CSCE 206 Lab Structured Programming in C
The while Looping Structure
EECE.2160 ECE Application Programming
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

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 #1 4. float Counter Example #2 5. Why float Counters are BAD BAD BAD 6. BAD float Counter Example #1 7. BAD float Counter Example #2 8. Replace float Counter with int #1 9. Replace float Counter with int #2 10.Debugging a for Loop #1 11.Debugging a for Loop #2 12.Debugging a for Loop #3 13.Debugging a for Loop #4 14.Debugging a for Loop #5 15.Changing Loop Bounds Inside Loop #1: BAD 16.Changing Loop Bounds Inside Loop #2: BAD 17.Changing Loop Index Inside Loop #1: BAD 18.Changing Loop Index Inside Loop #2: BAD

for Loop Lesson 3 CS1313 Spring for Loop with a float Counter: BAD! All of the examples of for loops that we’ve seen so far have used int counters. In principle, C also allows float counters. But, using a float counter is BAD BAD BAD.

for Loop Lesson 3 CS1313 Spring float Counter Example #1 #include int main () { /* main */ const int program_success_code = 0; float real_count; float sum = 0.0; for (real_count = 1.0; real_count <= 10.0; real_count++) { sum = sum + real_count; } /* for real_count */ printf("After the loop:\n"); printf(" real_count = %f, sum = %f\n", real_count, sum); return program_success_code; } /* main */

for Loop Lesson 3 CS1313 Spring float Counter Example #2 % gcc -o forreal forreal.c % forreal After the loop: real_count = , sum = This is BAD BAD BAD. Why?

for Loop Lesson 3 CS1313 Spring Why float Counters are BAD BAD BAD float counters are generally considered to be BAD BAD BAD programming practice, because: a float counter is an approximation, and therefore a loop with lots of iterations will accumulate a lot of error in the counter variable, as the error from each approximation adds up.

for Loop Lesson 3 CS1313 Spring BAD float Counter Example #1 #include int main () { /* main */ const float pi = ; const int program_success_code = 0; float radians; for (radians = 0; radians <= * pi; radians = radians + pi / 5.0) { printf("radians = %19.15f\n", radians); } /* for radians */ printf("After the loop:\n"); printf(" 100 * pi = %19.15f\n", * pi); printf(" radians = %19.15f\n", radians); return program_success_code; } /* main */

for Loop Lesson 3 CS1313 Spring BAD float Counter Example #2 % gcc -o forreal2 forreal2.c % forreal2 radians = radians = radians = radians = radians = radians = radians = radians = After the loop: 100 * pi = radians = This has been a deadly problem in real life. See:

for Loop Lesson 3 CS1313 Spring Replace float Counter with int #1 #include int main () { /* main */ const int program_success_code = 0; const float pi = ; float radians; int radians_counter; for (radians_counter = 0; radians_counter <= 500; radians_counter++) { radians = radians_counter * pi / 5.0; printf("radians = %19.15f\n", radians); } /* for radians */ printf("After the loop:\n"); printf(" * pi = %19.15f\n", * pi); printf(" radians = %19.15f\n", radians); printf(" radians_counter = %3d\n", radians_counter); return program_success_code; } /* main */

for Loop Lesson 3 CS1313 Spring Replace float Counter with int #2 % gcc -o forreal2int forreal2int.c % forreal2int radians = radians = radians = radians = radians = radians = radians = radians = After the loop: * pi = radians = radians_counter = 501 Notice that there’s no accumulated error from approximating float quantities, because each approximation is independent of the others.

for Loop Lesson 3 CS1313 Spring Debugging a for Loop #1 Suppose you have a program that has a for loop, and it looks like the for loop has a bug in it. Assuming that the bug isn’t obvious just from looking, how do we figure out where the bug is? One thing we can try is to put some printf statements inside the loop body. Often, the output of the loop body printf statements will tell us where to find the bug. When we’ve made a change, we can check to make sure things are going well using the same printf statements inside the loop body. Once we know that the loop is debugged, we can delete the printf statements inside the loop body.

for Loop Lesson 3 CS1313 Spring Debugging a for Loop #2 #include int main () { /* main */ int initial_value, final_value, count; int sum = 0; printf("What are the summation limits?\n"); scanf("%d %d", &initial_value, &final_value); for (count = initial_value; count <= final_value; count++) { sum = sum * count; } /* for count */ printf("The sum from %d to %d is %d.\n", initial_value, final_value, sum); return 0; } /* main */ % gcc -o sumbad sumbad.c % sumbad What are the summation limits? 1 5 The sum from 1 to 5 is 0.

for Loop Lesson 3 CS1313 Spring Debugging a for Loop #3 #include int main () { /* main */ int initial_value, final_value, count; int sum = 0; printf("What are the summation limits?\n"); scanf("%d %d", &initial_value, &final_value); for (count = initial_value; count <= final_value; count++) { sum = sum * count; printf("count = %d, sum = %d\n", count, sum); } /* for count */ printf("The sum from %d to %d is %d.\n", initial_value, final_value, sum); return 0; } /* main */ % gcc -o sumbad sumbad.c % sumbad What are the summation limits? 1 5 count = 1, sum = 0 count = 2, sum = 0 count = 3, sum = 0 count = 4, sum = 0 count = 5, sum = 0 The sum from 1 to 5 is 0.

for Loop Lesson 3 CS1313 Spring Debugging a for Loop #4 #include int main () { /* main */ int initial_value, final_value, count; int sum = 0; printf("What are the summation limits?\n"); scanf("%d %d", &initial_value, &final_value); for (count = initial_value; count <= final_value; count++) { sum = sum + count; printf("count = %d, sum = %d\n", count, sum); } /* for count */ printf("The sum from %d to %d is %d.\n", initial_value, final_value, sum); return 0; } /* main */ % gcc -o sumbad sumbad.c % sumbad What are the summation limits? 1 5 count = 1, sum = 1 count = 2, sum = 3 count = 3, sum = 6 count = 4, sum = 10 count = 5, sum = 15 The sum from 1 to 5 is 15.

for Loop Lesson 3 CS1313 Spring Debugging a for Loop #5 #include int main () { /* main */ int initial_value, final_value, count; int sum = 0; printf("What are the summation limits?\n"); scanf("%d %d", &initial_value, &final_value); for (count = initial_value; count <= final_value; count++) { sum = sum + count; } /* for count */ printf("The sum from %d to %d is %d.\n", initial_value, final_value, sum); return 0; } /* main */ % gcc -o sumbad sumbad.c % sumbad What are the summation limits? 1 5 The sum from 1 to 5 is 15.

for Loop Lesson 3 CS1313 Spring Changing Loop Bounds Inside Loop #1: BAD #include int main () { /* main */ int initial_value, final_value, maximum_value, count; int sum = 0; printf("What are the initial, final and "); printf("maximum values?\n"); scanf("%d %d %d", &initial_value, &final_value, &maximum_value); for (count = initial_value; count <= final_value; count++) { sum = sum + count; if (sum > maximum_value) { final_value = final_value - 1; } /* if (sum > maximum_value) */ printf("count = %d, sum = %d, final_value = %d\n", count, sum, final_value); } /* for count */ return 0; } /* main */ BAD!

for Loop Lesson 3 CS1313 Spring Changing Loop Bounds Inside Loop #2: BAD % gcc -o loopbndschg loopbndschg.c % loopbndschg What are the initial, final and maximum values? count = 1, sum = 1, final_value = 5 count = 2, sum = 3, final_value = 5 count = 3, sum = 6, final_value = 4 count = 4, sum = 10, final_value = 3

for Loop Lesson 3 CS1313 Spring Changing Loop Index Inside Loop #1: BAD #include int main () { /* main */ int initial_value, final_value, maximum_value, count; int sum = 0; printf("What are the initial, final and "); printf("maximum values?\n"); scanf("%d %d %d", &initial_value, &final_value, &maximum_value); for (count = initial_value; count <= final_value; count++) { sum = sum + count; if (sum > maximum_value) { count = count + 1; } /* if (sum > maximum_value) */ printf("count = %d, sum = %d, final_value = %d\n", count, sum, final_value); } /* for count */ return 0; } /* main */ BAD!

for Loop Lesson 3 CS1313 Spring Changing Loop Index Inside Loop #2: BAD % gcc -o loopidxchg loopidxchg.c % loopidxchg What are the initial, final and maximum values? count = 1, sum = 1, final_value = 5 count = 2, sum = 3, final_value = 5 count = 4, sum = 6, final_value = 5 count = 6, sum = 11, final_value = 5