1 / 53

Programming

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.

frey
Télécharger la présentation

Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Loops & Simulation

  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! 2

  3. Example 1 - Fibonacci series Write a program that prints Fibonacci series elements which are smaller than 5.

  4. Example 1 - Fibonacci series F(0)=0 F(1)=1 F(n)=F(n-1)+F(n-2) http://www.dorbanot.com/3757

  5. Example 1 - Fibonacci series F(0)=0 F(1)=1 F(n)=F(n-1)+F(n-2) 0 1 1 2 3 5 8 13 21 34 … 0 1 0 1 1 2 3 5 8 13 21 0 1 1 2 0 1 1 0 1 1 2 3 5 8 13 21 34 + + +

  6. Fibonacci – step by step 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"); fib1 fib2 0 1 fib_next lim 0 5 screen: 0

  7. Fibonacci – step by step 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"); fib1 fib2 0 1 fib_next lim 0 5 screen: 0

  8. Fibonacci – step by step 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"); fib1 fib2 0 1 fib_next lim 0 5 screen: 0 1

  9. Fibonacci – step by step 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"); fib1 fib2 0 1 fib_next lim 1 5 screen: 0 1

  10. Fibonacci – step by step 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"); fib1 fib2 1 1 fib_next lim 1 5 screen: 0 1

  11. Fibonacci – step by step 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"); fib1 fib2 1 1 fib_next lim 1 5 screen: 0 1

  12. Fibonacci – step by step 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"); fib1 fib2 1 1 fib_next lim 1 5 screen: 0 1

  13. Fibonacci – step by step 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"); fib1 fib2 1 1 fib_next lim 1 5 screen: 0 1 1

  14. Fibonacci – step by step 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"); fib1 fib2 1 1 fib_next lim 2 5 screen: 0 1 1

  15. Fibonacci – step by step 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"); fib1 fib2 1 1 fib_next lim 2 5 screen: 0 1 1

  16. Fibonacci – step by step 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"); fib1 fib2 1 2 fib_next lim 2 5 screen: 0 1 1

  17. Fibonacci – step by step 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"); fib1 fib2 1 2 fib_next lim 2 5 screen: 0 1 1

  18. Fibonacci – step by step 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"); fib1 fib2 1 2 fib_next lim 2 5 screen: 0 1 1 2

  19. Fibonacci – step by step 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"); fib1 fib2 1 2 fib_next lim 3 5 screen: 0 1 1 2

  20. Fibonacci – step by step 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"); fib1 fib2 2 2 fib_next lim 3 5 screen: 0 1 1 2

  21. Fibonacci – step by step 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"); fib1 fib2 2 3 fib_next lim 3 5 screen: 0 1 1 2

  22. Fibonacci – step by step 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"); fib1 fib2 2 3 fib_next lim 3 5 screen: 0 1 1 2

  23. Fibonacci – step by step 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"); fib1 fib2 2 3 fib_next lim 3 5 screen: 0 1 1 2 3

  24. Fibonacci – step by step 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"); fib1 fib2 2 3 fib_next lim 5 5 screen: 0 1 1 2 3 3

  25. Fibonacci – step by step 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"); fib1 fib2 3 3 fib_next lim 5 5 screen: 0 1 1 2 3 3

  26. Fibonacci – step by step 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"); fib1 fib2 3 5 fib_next lim 5 5 screen: 0 1 1 2 3 3

  27. Fibonacci – step by step 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"); fib1 fib2 3 5 fib_next lim 5 5 screen: 0 1 1 2 3 3

  28. Fibonacci – step by step 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"); fib1 fib2 3 5 fib_next lim 5 5 screen: 0 1 1 2 3 3

  29. e++; e = e + 1;  ++f; f = f + 1; Shorter lines • 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 (=). i = 5; j = i++; j=5 i=6 i = 5; j = ++i; i=j=6

  30. 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 = e - 1;  --f; f = f - 1; i = 5; j = i--; j=5 i=4 i = 5; j = --i; i=j=4

  31. 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……

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

  33. 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; }

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

  35. Solution 3: Prime Numbers int 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; }

  36. getchar char c; c = getchar(); char c; scanf("%c", &c); ==== getchar() gets a single character from the user. Requires including stdio.h Returns a non-positive number on failure. Similar to scanf.

  37. 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); ====

  38. Example 4: Copy int main() { int c = 0; c = getchar(); while (c != '\n') { putchar(c); c = getchar(); } return 0; }

  39. 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).

  40. Example 5: Counting Letters int 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; }

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

  42. Exercise 6: Low to Up • Read input from the user until end-of-line (‘\n’) is encountered. • Convert lowercase letters to uppercase ones.

  43. 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; }

  44. Simulation: estimating pi r =1 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:

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

  46. Example #include <stdio.h> #include <stdlib.h> int main() { int number=0; number=rand(); printf ("Random number is %d\n",number); return 0; }

  47. Not so random… • 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?

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

  49. 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; }

  50. 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;

More Related