programming
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 PresentationTRANSCRIPT
![Page 1: Programming](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/1.jpg)
Programming
Loops & Simulation
![Page 2: Programming](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/3.jpg)
Example 1 - Fibonacci series
Write a program that prints Fibonacci series elements which are smaller than 5.
![Page 4: Programming](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/4.jpg)
Example 1 - Fibonacci seriesF(0)=0F(1)=1F(n)=F(n-1)+F(n-2)
http://www.dorbanot.com/3757
![Page 5: Programming](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/24.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/26.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/27.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/28.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/29.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/30.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/31.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/32.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/33.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/34.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/35.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/36.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/37.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/38.jpg)
Example 4: Copy
int main(){ int c = 0;
c = getchar(); while (c != '\n') { putchar(c); c = getchar(); }
return 0;}
![Page 39: Programming](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/39.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/40.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/41.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/42.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/43.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/44.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/45.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/46.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/47.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/48.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/49.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/50.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/51.jpg)
#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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/52.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815f17550346895dcde0b3/html5/thumbnails/53.jpg)
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;
}