programming

53
Programming Loops & Simulation

Upload: frey

Post on 16-Feb-2016

34 views

Category:

Documents


0 download

DESCRIPTION

Programming. Loops & Simulation. Some General Tips on Programming. Use Debugger to follow your execution flow and find what went wrong Understanding is good but not enough - you must practice!. 2. Example 1 - Fibonacci series. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Programming

Programming

Loops & Simulation

Page 2: Programming

2

Some General Tips on Programming

Use Debugger to follow your execution flow and find what went wrong

Understanding is good but not enough - you must practice!

Page 3: Programming

Example 1 - Fibonacci series

Write a program that prints Fibonacci series elements which are smaller than 5.

Page 4: Programming

Example 1 - Fibonacci seriesF(0)=0F(1)=1F(n)=F(n-1)+F(n-2)

http://www.dorbanot.com/3757

Page 5: Programming

Example 1 - Fibonacci series

0 1 1 2 3 5 8 13 21 34 …

0 10 1 1 2 3 5 8 13 210 1 1 20 1 1+ +0 1 1 2 3 5 8 13 21 34+

F(0)=0F(1)=1F(n)=F(n-1)+F(n-2)

Page 6: Programming

int fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

Fibonacci – step by step

0

screen:

5lim

0fib1

1fib2

0fib_next

Page 7: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0

screen:

5lim

0fib1

1fib2

0fib_next

Page 8: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1

screen:

5lim

0fib1

1fib2

0fib_next

Page 9: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1

screen:

5lim

0fib1

1fib2

1fib_next

Page 10: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1

screen:

5lim

1fib1

1fib2

1fib_next

Page 11: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1

screen:

5lim

1fib1

1fib2

1fib_next

Page 12: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1

screen:

5lim

1fib1

1fib2

1fib_next

Page 13: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1

screen:

5lim

1fib1

1fib2

1fib_next

Page 14: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1

screen:

5lim

1fib1

1fib2

2fib_next

Page 15: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1

screen:

5lim

1fib1

1fib2

2fib_next

Page 16: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1

screen:

5lim

1fib1

2fib2

2fib_next

Page 17: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1

screen:

5lim

1fib1

2fib2

2fib_next

Page 18: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2

screen:

5lim

1fib1

2fib2

2fib_next

Page 19: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2

screen:

5lim

1fib1

2fib2

3fib_next

Page 20: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2

screen:

5lim

2fib1

2fib2

3fib_next

Page 21: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2

screen:

5lim

2fib1

3fib2

3fib_next

Page 22: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2

screen:

5lim

2fib1

3fib2

3fib_next

Page 23: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3

screen:

5lim

2fib1

3fib2

3fib_next

Page 24: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3 3

screen:

5lim

2fib1

3fib2

5fib_next

Page 25: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3 3

screen:

5lim

3fib1

3fib2

5fib_next

Page 26: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3 3

screen:

5lim

3fib1

5fib2

5fib_next

Page 27: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3 3

screen:

5lim

3fib1

5fib2

5fib_next

Page 28: Programming

Fibonacci – step by stepint fib1 = 0, fib2 = 1, fib_next = 0, lim = 5;

...printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 +

fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3 3

screen:

5lim

3fib1

5fib2

5fib_next

Page 29: Programming

We first perform the mathematical operation with the variable (=) and then evaluate it (++).

We first evaluate the variable (++) and then perform the mathematical operation with it (=).

Shorter lines

e = e + 1;e;++

f = f + 1;++f;

j=5i=6

i = 5;j = i;++

i=j=6i = 5;j = ++i;

Page 30: Programming

We first perform the mathematical operation with the variable (=) and then evaluate it (--).

We first evaluate the variable (--) and then perform the mathematical operation with it (=).

Shorter lines

e = e - 1;e;--

f = f - 1;--f;

j=5i=4

i = 5;j = i;--

i=j=4i = 5;j = --i;

Page 31: Programming

Now your turn!Class exercise: print a series Write a program that prints the first 10

elements in the following series : 1 -2 3 -4 5 -6 7 -8 9 -10……

Page 32: Programming

Example 2: increasing series Write a program that gets positive integers

from the user and prints an increasing series (without duplications):

Every number is compared to its previous as follows:

Number > previous print it. Number = previous ignore it. Number < previous exit.

Page 33: Programming

Solution 2: increasing series#include <stdio.h>int main() {

int x, prev = -1;printf ("please enter a series of positive integers :\n");do {

scanf("%d", &x );if (x < prev)

break;if (x == prev)

continue;printf("%d ", x);prev = x;

} while (x >= prev);return 0;

}

Page 34: Programming

Example 3: Prime Numbers

Write a program that prints all the prime numbers smaller than or equal to N.

N is given by the user (input)

How do we go over all the numbers <= N? How do we know if a number is prime?

Page 35: Programming

Solution 3: Prime Numbersint main(){

int candidate = 0, divisor = 0, last = 0;printf("Enter a number\n");scanf("%d", &last);for (candidate = 2; candidate <= last; ++candidate){

for (divisor = 2; divisor < candidate; ++divisor) if (candidate % divisor == 0)

break;

if (divisor == candidate) printf("the number %d is prime\n",

candidate); }return 0;

}

Page 36: Programming

getchar

getchar() gets a single character from the user.

Requires including stdio.h Returns a non-positive number on failure. Similar to scanf.

char c;c = getchar();

char c;scanf("%c", &c);

====

Page 37: Programming

putchar

putchar(char c) prints out the character inside the brackets.

Requires including stdio.h Similar to printf.

char c;putchar(c);

char c;printf("%c", c);

====

Page 38: Programming

Example 4: Copy

int main(){ int c = 0;

c = getchar(); while (c != '\n') { putchar(c); c = getchar(); }

return 0;}

Page 39: Programming

Example 5: Counting Letters

Write a program that read input from the user until end-of-line (‘\n’) is encountered.

The program counts the number of letters before the first space (or until the end-of-line , if space is not found).

Page 40: Programming

Example 5: Counting Lettersint main() {

int counter = 0, c = 0;printf("Enter a line of text:\n");c = getchar();while (c != '\n') {

if (c == ' ') /* found the first space - exit the loop */break;

++counter;c = getchar();

}

if (c == ' ') /* we found a space */printf("There are %d letters before the first space.\n",

counter);else /* we didn’t find a space */

printf("There are no spaces in the input line.\n");return 0;

}

Page 41: Programming

ctype library The <ctype.h> library contains

character classification functions. Useful functions:

int isalpha (int c) test for alphabetic character

int isdigit(int c)test for digit

int tolower(int c)convert character to lowercase

int toupper(int c)convert character to uppercase

and more… see ctype.h library

Page 42: Programming

Exercise 6: Low to Up

Read input from the user until end-of-line (‘\n’) is encountered.

Convert lowercase letters to uppercase ones.

Page 43: Programming

Solution 6: Low to Up#include <stdio.h>#include <ctype.h> // needed for the function toupper

int main(){

int c = 0;printf("Please, enter a string (end with an enter):\n");c = getchar();

while (c != '\n'){

putchar((c >= 'a' && c <= 'z') ? toupper(c) : c);c = getchar();

}putchar('\n');return 0;

}

Page 44: Programming

Simulation: estimating pi Throw darts to compute the area of a unit

circle inside a square Pi can be inferred from the ratio between the

area of a square (total hits) to the area of a circle (hits inside the circle) resides in it:

151.39007094

44 2

2

square

circle

square

circle

AA

rr

AA

r =1

Page 45: Programming

Random numbers We can generate (psuedo)-random

variables using rand() rand() returns a random integer between 0

and MAX_INT (MAX_INT may vary between implementations but it is at least 32767).

We need to include <stdlib.h> in the program.

Page 46: Programming

Example#include <stdio.h>#include <stdlib.h>

int main(){

int number=0;number=rand();printf ("Random number is %d\n",number);return 0;

}

Page 47: Programming

However, if we run it many times, it will always generate the same “random” number…

This is because the computer uses a formula to generate the “random” number.

What do we do?

Not so random…

Page 48: Programming

The algorithm is based on seed, an initial state that will always produce the same sequence of numbers.

If we change the seed, the sequence will be different, resulting in different random number.

We initialize the seed with srand(seed). It is common to initialize with computer time

srand(time(NULL)). We need to include <time.h>.

Change the seed

Page 49: Programming

Example#include <stdio.h>#include <time.h>#include <stdlib.h>

int main(){

int number=0;srand(time(NULL));number=rand();printf ("Random number is %d\n",number);return 0;

}

Page 50: Programming

Random numbers within a range

We can choose a random variable in a range using %.0 up to 100: rand()%100;50 to up to 75: rand()%25+50;double between 0 and 1:

rand()/(double)RAND_MAX;

Page 51: Programming

#include <stdio.h>#include <stdlib.h>#include <time.h>int main(){

long countAs=0, countAc=0,numDarts=1000000;double x, y;

srand(time(NULL)); // Used to initialize the random number generatorfor(countAs = 0; countAs < numDarts; countAs++){

// generate random number between -1 and 1x = -1 + 2 * ((double)rand()) / RAND_MAX;// generate random number between -1 and 1y = -1 + 2 * ((double)rand()) / RAND_MAX;// count if it hits the circle i.e. smaller than the radiusif (x*x + y*y <= 1.0)

countAc++;}printf("The estimation of pi is: %g\n", (4.0 * countAc) / countAs);return 0;

}

Page 52: Programming

Class ex. solution (using while)/* Prints the first 10 elements in the following series: * 1 -2 3 -4 5 -6 7 -8 9 -10 ... */ #include <stdio.h>

int main(){

int counter = 1, sign = 1; 

while (counter <= 10){

printf("%d ", sign*counter);sign*=-1;counter++;

}return 0;

}

Page 53: Programming

Class ex. solution (using for)/* Prints the first 10 elements in the following series: * 1 -2 3 -4 5 -6 7 -8 9 -10 ... */ #include <stdio.h> int main(){

int counter, sign; 

for(counter = 1,sign = 1; counter <= 10; counter++, sign*=-1)printf("%d ", sign*counter);

 return 0;

}