Presentation is loading. Please wait.

Presentation is loading. Please wait.

Real World Applications: Generating Prime Numbers  Problem Write a program that prints all positive prime integers less than or equal to n. A positive.

Similar presentations


Presentation on theme: "Real World Applications: Generating Prime Numbers  Problem Write a program that prints all positive prime integers less than or equal to n. A positive."— Presentation transcript:

1 Real World Applications: Generating Prime Numbers  Problem Write a program that prints all positive prime integers less than or equal to n. A positive integer p is a prime if p > 1 and the only divisors of p is 1 and p itself. The value n is supplied by the user.  Sample Input/Output This program lists all primes <= n Input n: 20 Primes <= 20: 2, 3, 5, 7, 11, 13, 17, 19,

2 Finding Primes ôSolution çPrompt the user about the purpose of the program. And ask the user for input n. çRun a loop from 2 to n to try each number p as a candidate for prime. çRun inside the first loop a second loop to see if p can be divided exactly by d for d = 2, 3, …, p-1. çA prime is the one which can not be divided by any of the d. Print the prime.

3 Prime Numbers #include main() { int p, n, div; printf("\tThis program lists all" " primes <= n\n\n"); printf("Input n: "); scanf("%d", &n); printf("\n\n\tPrimes <= %d:\n\n", n); for( p = 2; p <= n; p++) { /* try to find a divisor of p */ for(div = 2; div < p; div++) { if( p % div == 0) /* found a divisor */ break; /* so p is not a prime */ } if (div == p) /* exhausted possible divisors */ printf("%d, ", p); /* so p is prime */ } printf("\n"); }

4 switch Statement switch(int expression){ case constant1: statements1 case constant2: statements2... case constantn: statementsn } The value of the int expression is compared with constanti in turn. If constanti equals the value, execution starts from statementsi to statementn, unless break is encountered. Break courses a jump out of the switch body immediately.

5 A switch example char element_code; element_code = 'm'; switch( element_code) { case 's': printf("Selenium\n"); break; case 'm': printf("Magnesium\n"); break; case 'h': printf("Hydrogen\n"); break; } printf("*** End ***\n"); the output is Magnesium *** End ***

6 switch Switch( ) { case c1: case c2: case cn: } expr Break;

7 switch Statement switch(int expression){ case constant1: statements1 case constant2: statements2... case constantn: statementsn default: statements } If all the cases don't match the value of the exppression, execute the statements under the default.

8 switch example 2 char element_code; element_code = 'z'; switch( element_code) { case 's': printf("Selenium\n"); break; case 'm': printf("Magnesium\n"); break; case 'h': printf("Hydrogen\n"); break; default: printf("No such atom\n"); } printf("*** End ***\n"); the output is No such atom *** End ***

9 goto statement Main() {... If (... ) goto End;... End:... } The goto statement allows one to jump from any place to anywhere else within a function.

10 goto example for(i=0; i<5; i++) for(j=0; j<5; j++) for(k=0; k<5; k++) if(i+j+k == 2) goto out; out: printf("i=%d,j=%d,k=%d\n", i, j, k); The output is i=0, j=0, k=2

11 Conditional Expressions The statement x = flag ? y : z; is short for if (flag) x = y; else x = z;

12 Conditional Expressions 2 In general, to obtain the value of the conditional expression expr1 ? expr2 : expr3 we first determine whether expr1 is true or false (0 is false, anything else is true). If expr1 is true, the value of the conditional expression is expr2; if expr1 is false, the value is expr3. Example x = (i>j) ? j : i; set x to the minimum of i and j.

13 What is printed? #include main() { int i, j, k, x; Label: i = 1; j = 5; switch (j) { case 1: k = 0; break; case 2: k = 5; goto Label; default: k = 10; } x = (i>j) ? k : i; printf("%d %d\n", k, x); }

14 Common Programming Errors Forget & in scanf, for example, scanf("%d", x); –may give error message "segmentation fault" Used \n or space in scanf. Forget to include. This courses serious error if EOF and FILE are used. Used = instead of == in condition, e.g., if( x = 1)...

15 Common Programming Errors Used comma in for, for(i=0, i < 10, ++i)...; Forget parentheses (. ) in conditional if or switch if x > 1 {.. }

16 Reading/Home Working Read Chapter 4, page 119 to 130. Work on Problems –Section 4.3, page 126- 127, exercise 1, 3. –Section 4.5, page 130, exercise 1. Check your answers in the back of the textbook. Do not hand in.


Download ppt "Real World Applications: Generating Prime Numbers  Problem Write a program that prints all positive prime integers less than or equal to n. A positive."

Similar presentations


Ads by Google