epsii 59:006 spring 2004. remote and local computers css itaniums (unix) windows xp (dorm)...

26
EPSII 59:006 Spring 2004

Upload: william-daniels

Post on 29-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

EPSII

59:006

Spring 2004

Page 2: 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

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

Page 3: 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

Algorithms and Pseudocode

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

the actions

Page 4: 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

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’.

Page 5: 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

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

Page 6: 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

…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”

Page 7: 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

Testing

session exampleWhat is your guess?%50Lower%25Higher%33Correct!What is your guess?

How would you debug this program?

Page 8: 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

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

Page 9: 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

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

Page 10: 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

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

Page 11: 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

Pseudocode

Generate random number

Get first guess

If guess is correct, done

If first guess is high, report low

else report high

Repeat

Page 12: 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

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);}

Page 13: 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

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

Page 14: 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

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);}

Page 15: 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

Testing – what happens on inappropriate input (500 or -37)

Check for valid input

Page 16: 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

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);}

Page 17: 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

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???

Page 18: 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

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);}

Page 19: 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

Other problems

Wanted 1 to 100 (not 0 to 99) What happens on input of "Bob"?

will address how to handle in later sections

Page 20: 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

More examples…

Page 21: 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

#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;}

Page 22: 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

#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;}

Page 23: 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

/* 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 ;}

Page 24: 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

/* 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 ;}

Page 25: 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

/* 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 ;}

Page 26: 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

/* 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 ;}