int main snapshot return 0 void snapshot find

  • Slides: 34
Download presentation

 § דוגמא – צילום תמונה דיגיטלית int main() { snapshot(); return 0; }

§ דוגמא – צילום תמונה דיגיטלית int main() { snapshot(); return 0; } void snapshot () { find. Memory(); empty. Sensors(); click(); compress(); save. To. Memory(); }. © כל הזכויות שמורות. נכתב ע"י יעל ארז 4

 הכרזה הגדרה int gcd(int, int); int main() { int a, b, c, g_abc;

הכרזה הגדרה int gcd(int, int); int main() { int a, b, c, g_abc; scanf("%d%d%d", &a, &b, &c); g_ab = gcd(a, b); g_abc = gcd(g_ab, c); printf("gcd: %dn“, g_abc); return 0; } קריאה int gcd(int a, int b) { int t; while (b != 0) { t = b; b = a%t; a = t; } return a; }. © כל הזכויות שמורות. נכתב ע"י יעל ארז 8

return-type function-name(parameters-list) { variable definitions statements גוף הפונקציה return statement } . © כל

return-type function-name(parameters-list) { variable definitions statements גוף הפונקציה return statement } . © כל הזכויות שמורות. נכתב ע"י יעל ארז 9

return-type function-name parameters-list int gcd(int a, int b) { int t; while (b !=

return-type function-name parameters-list int gcd(int a, int b) { int t; while (b != 0) { t = b; b = a%t; a = t; } return statement גוף הפונקציה return a; } . © כל הזכויות שמורות. נכתב ע"י יעל ארז 10

double avg(int, int); #define N 2. 0 int main() { int a=100, b=91; double

double avg(int, int); #define N 2. 0 int main() { int a=100, b=91; double res = avg(a, b+3); return 0; ? האם יש הבדל } double avg(int a, int b) { return (a+b)/N; } . © כל הזכויות שמורות. נכתב ע"י יעל ארז 13

#include <stdio. h> typedef unsigned int uint; uint gcd(uint a, uint b); uint lcm(uint

#include <stdio. h> typedef unsigned int uint; uint gcd(uint a, uint b); uint lcm(uint a, uint b); int main(void) // find reduced x/y + m/n { while(1) { printf("Enter x/y m/n (x, y, m, n>0) [any=0 for exit] “); uint x, y, m, n; scanf(“%d%d”, x, y, m, n); if(x==0 || y==0 || m==0|| n==0) break; uint gcd(uint a, uint b) //find gcd(a, b) using // Euclide algo. b>0 { uint tmp; while((tmp = a%b) != 0) { a = b; uint c_d = lcm(y, n); // c_d=minimal common denominator b = tmp; uint numerator = (x*c_d/y + m*c_d/n); } printf(“%d/%d+%d/%d=“, x , y, m, n); return b; printf("%d/%d", numerator, c_d); } int tmp = gcd(numerator, c_d); if(tmp != 1) printf( " = %d/%dn“, numerator/gcd, tmp/gcd); } } return 0; uint lcm(uint a, uint b) //Least commom multiplication { return (a*b) / gcd(a, b); }. © כל הזכויות שמורות. נכתב ע"י יעל ארז 18

? מה יקרה const int max. Delay = 1000; int delay() { static int

? מה יקרה const int max. Delay = 1000; int delay() { static int c = max. Delay; return c--; } int main() { while (delay()); return 0; }. © כל הזכויות שמורות. נכתב ע"י יעל ארז 27

 פונקציות #include <math. h> #include <time. h> #include <stdio. h> typedef unsigned long

פונקציות #include <math. h> #include <time. h> #include <stdio. h> typedef unsigned long ull; ull gcd(ull a, ull b)//b>0 //Greatest commom divisor(a, b) by Euclide algo. { ull gcd(ull a, ull b); bool mersenne(ull p); bool prime(ull p); } ull tmp; while((tmp=a%b) != 0) { a=b; b=tmp; } return b; . © כל הזכויות שמורות. נכתב ע"י יעל ארז 30

 פונקציות bool mersenne(ull p)//p odd >=3 //True if p Mersenne number 0. .

פונקציות bool mersenne(ull p)//p odd >=3 //True if p Mersenne number 0. . 01. . 1 { while(p) { if(p%2 == 0) return false; //l. s. b. =0 but p!=0 p/=2; } return true; } bool prime(ull p)//p odd >=3 //True if p prime { ull n=3, sp=sqrt((double)p); while(n<=sp) { if(p%n == 0) return false; n+=2; } return true; }. © כל הזכויות שמורות. נכתב ע"י יעל ארז 31

 אתחול int main() { cout << "Mersenne primes in range: nn"; int mersenne_counter=1;

אתחול int main() { cout << "Mersenne primes in range: nn"; int mersenne_counter=1; // 1 st is 7 cout << "Mersenne prime number " <<mersenne_counter << " is " << 7; cout << endl; ull gcd_total=gcd(7*7*7*7 -1, 11*11*11*11 -1); //first two ull p=11, count=2/*count primeries*/, last_p=11/*last primary*/; int start=clock(); … } . © כל הזכויות שמורות. נכתב ע"י יעל ארז 32

int main() { … לולאה מרכזית while(1) { //find next prime p+=2; double pd=p;

int main() { … לולאה מרכזית while(1) { //find next prime p+=2; double pd=p; double tmp=pd*pd; //2 multiplications instead of 3 pd*pd*pd*pd tmp=tmp*tmp-1; if(tmp > 0 xffffffff) break; //tmp > MAX ull if(prime(p)!= true) continue; //try next odd if(mersenne(p)) { mersenne_counter++; cout << "Mersenne prime number " << mersenne_counter << " is " << p; } last_p=p; count++; ull tmpull=p*p; tmpull=tmpull*tmpull-1; gcd_total=gcd(gcd_total, tmpull); }. © כל הזכויות שמורות. נכתב ע"י יעל ארז … } 33

 הדפסות וסיום תוכנית int main() { … int end=clock(); cout << "n. Number

הדפסות וסיום תוכנית int main() { … int end=clock(); cout << "n. Number of tested primes = " << count << endl; cout << "n. Last prime was " << last_p << endl; cout << "n. Over all gcd=" << gcd_total << endl; cout << "n. Elapsed time=" << end-start << "m. Sec. n"; } return 0; . © כל הזכויות שמורות. נכתב ע"י יעל ארז 34