Presentation is loading. Please wait.

Presentation is loading. Please wait.

EPSII 59:006 Spring 2004. Remote and Local Computers CSS Itaniums (Unix) Windows XP (Dorm) texas.eng.uiowa.edu (Unix) HW1.c ???? ssh (terminal and file.

Similar presentations


Presentation on theme: "EPSII 59:006 Spring 2004. Remote and Local Computers CSS Itaniums (Unix) Windows XP (Dorm) texas.eng.uiowa.edu (Unix) HW1.c ???? ssh (terminal and file."— Presentation transcript:

1 EPSII 59:006 Spring 2004

2 Remote and Local Computers CSS Itaniums (Unix) Windows XP (Dorm) texas.eng.uiowa.edu (Unix) HW1.c ???? ssh (terminal and file transfer) WebCT (Weeg server) http hyper text transfer protocol www.cnn.com

3 Algorithms and Pseudocode Actions to be taken Order to take the actions Pseudocode is a visual representation of the actions

4 Algorithms Informally, an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output.  finite set of steps, one or more operations per step  An algorithm is correct if, for every input instance, it halts with the correct output.  Example: sorting Input: A sequence of n numbers (a1, a2, ….,an). Output: A permutation (reordering) (a1’, a2’, …,an’) of the input sequence such that a1’<=a2’<=…<=an’.

5 Algorithms How to validate?  Mathematically prove (usually impractical)  Case base proving/testing How to devise?  mimic a human procedure  follow a template  create How to analyze?  complexity analysis  profiling

6 …Algorithms Testing programs  debugging  testing (exhaustive or case-based)  example – number guessing game program that generates random number between 1 and 100 inclusive you enter numbers, trying to “guess” the random number, and program responds with “higher”, and “lower”

7 Testing session example What is your guess? %50 Lower %25 Higher %33 Correct! What is your guess? How would you debug this program?

8 Algorithms - Testing input ranges  what happens if I put in -5, or 999, or “Bob” ensure program terminates  verify the program generates numbers between 0 and 101  test unlikely scenarios guess number on first try? guess number outside of range (“75” after lower) change range from 1-100 to 1 and see what happens

9 What is in an Algorithm? Everything boils down to 3 parts:  Sequence (a series of steps)  Selection (if condition is true, then do this)  Repetition (while condition is true) yes no

10 Make An Algorithm How do you know if you understand?  Take the program apart piece by piece  Write a Flow Chart  Add printf () statements to view variables

11 Pseudocode Generate random number Get first guess If guess is correct, done If first guess is high, report low else report high Repeat

12 main() { int num=0; int guess =-1; srand(time()); num = (rand()%100); printf("Guess a number:\n"); while(guess != num){ scanf("%d",&guess); if(guess > num) { printf("lower\n"); } else { printf("higher\n"); } // Since I am out of the loop, I must have guessed correctly printf("Correct: The number was %d\n",num); }

13 Testing – How can we be sure numbers are between 1 and 100 inclusive? Examine the numbers being generated

14 main() { int num=0; int guess =-1; srand(time()); num = (rand()%100); /* test the numbers generated */ while(1) { printf("num = %d\n",num); num = (rand()%100); if(num == 100) { exit(1); } printf("Guess a number:\n"); while(guess != num){ scanf("%d",&guess); if(guess > num) { printf("lower\n"); } else { printf("higher\n"); } // Since I am out of the loop, I must have guessed correctly printf("Correct: The number was %d\n",num); }

15 Testing – what happens on inappropriate input (500 or -37) Check for valid input

16 main() { int num=0; int guess =-1; srand(time()); num = (rand()%100); printf("Guess a number(0-99):\n"); while(guess != num){ scanf("%d",&guess); /* test input for range */ if(guess > 99) { printf("Too high (< 99 please)\n"); } if(guess < 0) { printf("Too low (> 0 please)\n"); } if(guess > num) { printf("lower\n"); } else { printf("higher\n"); } // Since I am out of the loop, I must have guessed correctly printf("Correct: The number was %d\n",num); }

17 Check for Bugs What happens if the number 500 is entered? Guess a number(0-99): 500 Too high (< 99 please) lower Both the error message and the "lower" statement is printed – did we really want/need both???

18 main() { int num=0; int guess =-1; srand(time()); num = (rand()%100); printf("Guess a number(0-99):\n"); while(guess != num){ scanf("%d",&guess); /* test input for range */ if(guess > 99) { printf("Too high (< 99 please)\n"); } else if(guess < 0) { printf("Too low (> 0 please)\n"); } else if(guess > num) { printf("lower\n"); } else { printf("higher\n"); } // Since I am out of the loop, I must have guessed correctly printf("Correct: The number was %d\n",num); }

19 Other problems Wanted 1 to 100 (not 0 to 99) What happens on input of "Bob"?  will address how to handle in later sections

20 More examples…

21 #include int main (void) { int IsRaining=0, IsSunny=1; int IsJuly=1, IsFebruary=0; if (IsFebruary) printf(“It’s February and”) ; else if (IsJuly) printf(“It’s July and”) ; else printf(“I don’t know what month it is and”) ; if (IsRaining) printf(“ it’s raining\n”) ; else if (IsSunny) printf(“ it’s sunny\n”) ; else printf(“ I don’t know what the weather is\n”); return 0; }

22 #include int main (void) { int IsRaining=0, IsSunny=1 ; int IsJuly=1, IsFebruary=0 ; if (IsFebruary && IsSunny) printf (“It’s February and it’s sunny\n”) ; else if (IsFebruary && IsRaining) printf (“It’s February and it’s raining\n”) ; else if (IsJuly && IsSunny) printf (“It’s July and it’s sunny\n”) ; else if (IsJuly && IsRaining) printf (“It’s July and it’s raining\n”) ; else printf (“I don’t know what month it is and I don’t know what the weather is\n”) ; return 0; }

23 /* sum.c – do some arithmetic with floating point numbers */ #include int main (void) { int counter = 0 ; double sum=0.0, A=1.0, B=1.0 ; while (counter++ < 10) /* repeat ten times */ { sum += A/B ; /* add the quotient A/B to the variable sum */ B -= 0.1 * A ; /* decrement B */ } printf (“The total is %f\n”, sum) ; /* output results */ return 0 ; }

24 /* sum2.c – do some arithmetic with floating point numbers this program contains math errors */ #include int main (void) { int counter = 0 ; double sum=0.0, A=1.0, B=0.9 ; while (counter++ < 10) /* repeat ten times */ { sum += A/B ; /* add the quotient A/B to the variable sum */ B -= 0.1 * A ; /* decrement B */ } printf (“The total is %f\n”, sum) ; /* output results */ return 0 ; }

25 /* sum3.c – do some arithmetic with floating point numbers this program contains math errors */ #include int main (void) { int counter = 0 ; double sum=0.0, A=1.0, B=0.9 ; while (counter++ < 10) /* repeat ten times */ { sum += A/B ; /* add the quotient A/B to the variable sum */ printf (“Iteration %d: for A=%f, B=%f, sum is now %f\n”, counter, A, B, sum) ; B -= 0.1 * A ; /* decrement B */ } printf (“The total is %f\n”, sum) ; /* output results */ return 0 ; }

26 /* sum4.c – do some arithmetic with floating point numbers final fixed version */ #include int main (void) { int counter = 0 ; double sum=0.0, A=1.0, B=0.9 ; while (counter++ < 10) /* repeat ten times */ { sum += A/B ; /* add the quotient A/B to the variable sum */ printf (“Iteration %d: for A=%f, B=%f, sum is now %f\n”, counter, A, B, sum) ; B -= 0.1 * A ; /* decrement B */ if (B < 0.1) /* check for our error */ counter=10; } printf (“The total is %f\n”, sum) ; /* output results */ return 0 ; }


Download ppt "EPSII 59:006 Spring 2004. Remote and Local Computers CSS Itaniums (Unix) Windows XP (Dorm) texas.eng.uiowa.edu (Unix) HW1.c ???? ssh (terminal and file."

Similar presentations


Ads by Google