View
217
Download
0
Category
Preview:
Citation preview
EPSII
59:006
Spring 2004
Remote and Local Computers
CSS Itaniums (Unix)
Windows XP(Dorm)
texas.eng.uiowa.edu(Unix)
HW1.c ????ssh (terminal andfile transfer)
WebCT
(Weeg server)
httphyper texttransferprotocolwww.cnn.com
Algorithms and Pseudocode
Actions to be taken Order to take the actions Pseudocode is a visual representation of
the actions
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 stepAn 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’.
Algorithms How to validate?
Mathematically prove (usually impractical)Case base proving/testing
How to devise?mimic a human procedure follow a templatecreate
How to analyze?complexity analysisprofiling
…Algorithms
Testing programsdebugging 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”
Testing
session exampleWhat is your guess?%50Lower%25Higher%33Correct!What is your guess?
How would you debug this program?
Algorithms - Testing
input rangeswhat happens if I put in -5, or 999, or “Bob”
ensure program terminatesverify 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
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)
yesno
Make An Algorithm
How do you know if you understand?Take the program apart piece by pieceWrite a Flow ChartAdd printf () statements to view variables
Pseudocode
Generate random number
Get first guess
If guess is correct, done
If first guess is high, report low
else report high
Repeat
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 correctlyprintf("Correct: The number was %d\n",num);}
Testing – How can we be sure numbers are between 1 and 100 inclusive? Examine the numbers being generated
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 correctlyprintf("Correct: The number was %d\n",num);}
Testing – what happens on inappropriate input (500 or -37)
Check for valid input
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 correctlyprintf("Correct: The number was %d\n",num);}
Check for Bugs
What happens if the number 500 is entered?
Guess a number(0-99):500Too high (< 99 please)lower
Both the error message and the "lower" statement is printed – did we really want/need both???
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 correctlyprintf("Correct: The number was %d\n",num);}
Other problems
Wanted 1 to 100 (not 0 to 99) What happens on input of "Bob"?
will address how to handle in later sections
More examples…
#include <stdio.h>
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;}
#include <stdio.h>
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;}
/* sum.c – do some arithmetic with floating point numbers*/
#include <stdio.h>
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 ;}
/* sum2.c – do some arithmetic with floating point numbers this program contains math errors*/
#include <stdio.h>
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 ;}
/* sum3.c – do some arithmetic with floating point numbers this program contains math errors*/
#include <stdio.h>
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 ;}
/* sum4.c – do some arithmetic with floating point numbers final fixed version*/#include <stdio.h>
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 ;}
Recommended