1 / 22

comp.nus.sg/~cs1010/

Lecturer’s slides. http://www.comp.nus.edu.sg/~cs1010/. WEEK 4. Class Activities. Week 4: Repetition Statements. Tracing while Loop Tracing for Loop Warm-up: List a Range of Integers Exercise #1: Sum of Multiples of 3 Exercise #2: Asterisks Tracing Nested Loop

venus
Download Presentation

comp.nus.sg/~cs1010/

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. Lecturer’s slides http://www.comp.nus.edu.sg/~cs1010/ WEEK 4 Class Activities

  2. CS1010 (AY2014/5 Semester 1) Week 4: Repetition Statements • Tracing whileLoop • Tracing for Loop • Warm-up: List a Range of Integers • Exercise #1: Sum of Multiples of 3 • Exercise #2: Asterisks • Tracing Nested Loop • Exercise #3: Prime Number • Testing and Debugging (running theme)

  3. CS1010 (AY2014/5 Semester 1) Tracing while Loop (1/4) • Trace the following codes manually and write out their outputs (a) int a = 1; while (a*a < 100) { printf("%d ", a); a *= 2; } printf("\n"); 1 2 4 8 b=0, c=9 b=1, c=8 b=2, c=7 b=3, c=6 b=4, c=5 outside:b=5, c=4 (b) int b = 0,c = 9; while (b < c) { printf("b=%d,c=%d\n", b, c); b++; c--; } printf("outside: b=%d,c=%d\n", b, c);

  4. CS1010 (AY2014/5 Semester 1) Tracing while Loop (2/4) • Example: Given a positive integer n, print out its digits from least significant to most significant. • Sample run: Enter a positive integer: 28943 3 4 9 8 2

  5. CS1010 (AY2014/5 Semester 1) Tracing while Loop (3/4) • Example: Given a positive integer n, print out its digits from least significant to most significant. Week4_PrintDigits.c // Precond: n > 0 void print_digits(intn) { int digit; while (n > 0) { digit = n%10; printf("%d\n", digit); n /= 10; } }

  6. CS1010 (AY2014/5 Semester 1) Tracing while Loop (4/4) Week4_PrintDigits.c // Precond: n > 0 void print_digits(intn) { int digit; while (n > 0) { digit = n%10; printf("%d\n", digit); n /= 10; } } What are the values of n and digit after exiting the loop?

  7. CS1010 (AY2014/5 Semester 1) Tracing for Loop • Trace the following codes manually and write out their outputs (a) int i, sum = 0; for (i=0; i <= 10; i+=2) { sum += i; } printf("sum = %d\n", sum); sum = 30 i=1, sum=1 i=2, sum=3 i=4, sum=7 i=8, sum=15 i=16, sum=31 Final i=32 Final sum=31 (b) int i, sum = 0; for (i=1; sum < 20; i*=2) { sum += i; printf("i=%d,sum=%d\n", i, sum); } printf("Final i=%d\n", i); printf("Final sum=%d\n", sum);

  8. CS1010 (AY2014/5 Semester 1) Warm-up: List a Range of Integers (1/3) • Ask the user for 2 integers: a (the lower limit), and b (the upper limit), and print the list of integers from a to b. • Write a function list_integers(int lower, int upper) • Main function given: #include <stdio.h> void list_integers(int, int); intmain(void) { inta, b; printf("Enter 2 integers a and b (a<=b): "); scanf("%d %d", &a, &b); list_integers(a, b); return 0; }

  9. CS1010 (AY2014/5 Semester 1) Warm-up: List a Range of Integers (2/3) • What should be the pre-condition of list_integer()? // List integers in the range [lower, upper] // Precond: void list_integers(int lower, int upper) { ... } lower <= upper int num; for (num=lower; num<=upper; num++) { printf("%d", num); } printf("\n"); • Use a for loop to implement the function

  10. CS1010 (AY2014/5 Semester 1) Warm-up: List a Range of Integers (3/3) • Now, use a while loop to implement the function, without using any local variables // List integers in the range [lower, upper] // Precond: void list_integers(int lower, int upper) { ... } lower <= upper while (lower <= upper) { printf("%d", lower); lower++; } printf("\n");

  11. CS1010 (AY2014/5 Semester 1) Exercise #1: Sum of Multiples of 3 (1/2) • Modify the program Unit6_OddIntegers_v1.c to read a positive integer n and then compute the sum of all integers which are multiples of 3 between 1 and n inclusive using a for loop. Write a function called sum_multiples_of_3(int). • This problem can be solved with a formula, but we will use the for loop just for exercise. • Call this program SumMultiples3.c • Sample run: Enter a positive integer: 50 Sum = 408

  12. CS1010 (AY2014/5 Semester 1) Exercise #1: Sum of Multiples of 3 (2/2) • How about using a while loop instead? • Pseudo-code using a while loop: precondition: n > 0 sum  0 i n while (i > 0) if iis a multiple of 3 sum  sum + i i  i - 1 return sum

  13. CS1010 (AY2014/5 Semester 1) Exercise #2: Asterisks (1/2) • Write a program Asterisks.cto read an integer n and print a certain number of asterisks on a single line. Write a function print_asterisks(int). • If n is non-positive, then no asterisk should be printed. • Sample runs: Think! What is the relationship between n and the number of *? Enter n: 3 ***** Done! Enter n: 6 *********** Done! Enter n: -2 Done! Enter n: 10 ******************* Done!

  14. CS1010 (AY2014/5 Semester 1) Exercise #2: Asterisks (2/2) • Write a program Asterisks.cto read an integer n and print a certain number of asterisks on a single line. Write a function print_asterisks(int). Pseudo-code: read input n; if n is non-positive print “Done!” and end program; m compute the number of asterisks given n print_asterisks(m) end program;

  15. CS1010 (AY2014/5 Semester 1) Tracing Nested Loops (1/5) • Given the following 3 programs, hand trace each of them and write out the output without running the program.

  16. CS1010 (AY2014/5 Semester 1) Tracing Nested Loops (2/5) Week4_NestedLoop1.c #include <stdio.h> int main(void) { int a, b; a = 1; while (a <= 4) { b = a + 3; while (b <= 10) { printf("a =%d, b = %d\n", a, b); b += 3; } a++; } return0; } a = 1, b = 4 a = 1, b = 7 a = 1, b = 10 a = 2, b = 5 a = 2, b = 8 a = 3, b = 6 a = 3, b = 9 a = 4, b = 7 a = 4, b = 10

  17. CS1010 (AY2014/5 Semester 1) Tracing Nested Loops (3/5) Week4_NestedLoop2.c #include <stdio.h> int main(void) { int x, y; for (x=10; x<30; x+=5) for (y=x; y>4; y/=2) printf("x = %d, y = %d\n", x, y); return 0; } x = 10, y = 10 x = 10, y = 5 x = 15, y = 15 x = 15, y = 7 x = 20, y = 20 x = 20, y = 10 x = 20, y = 5 x = 25, y = 25 x = 25, y = 12 x = 25, y = 6

  18. CS1010 (AY2014/5 Semester 1) Tracing Nested Loops (4/5) Week4_NestedLoop3.c #include <stdio.h> int main(void) { intp, q; for (p=0; p<10; p++) { if (p%2 == 0) { for (q=4; q>0; q--) printf("p = %d, q = %d\n", p, q); } else { for (q=p; q<20; q+=5) printf("p = %d, q = %d\n", p, q); } } return 0; }

  19. CS1010 (AY2014/5 Semester 1) Tracing Nested Loops (5/5) Week4_NestedLoop3.c for (p=0; p<6; p++) { if (p%2 == 0) { for (q=4; q>0; q--) printf("p = %d, q = %d\n", p, q); } else { for(q=p; q<20; q+=5) printf("p = %d, q = %d\n", p, q); } } p = 0, q = 4 p = 0, q = 3 p = 0, q = 2 p = 0, q = 1 p = 1, q = 1 p = 1, q = 6 p = 1, q = 11 p = 1, q = 16 p = 2, q = 4 p = 2, q = 3 p = 2, q = 2 p = 2, q = 1 p = 3, q = 3 p = 3, q = 8 p = 3, q = 13 p = 3, q = 18 p = 4, q = 4 p = 4, q = 3 p = 4, q = 2 p = 4, q = 1 p = 5, q = 5 p = 5, q = 10 p = 5, q = 15

  20. CS1010 (AY2014/5 Semester 1) Exercise #3: Prime Number • Primality test is a classic programming problem • Given a positive integer, determine whether it is a prime • A primenumber has two distinct factors (divisors): 1 and itself. Examples: 2, 3, 5, 7, 11, ... (Note: 1 is not a prime!) • Write a program PrimeTest.c. You should include a function is_prime(int). (What value should the function return?) • This exercise is mounted on CodeCrunch. Sample runs: Enter a positive integer: 131 131 is a prime. Enter a positive integer: 713 713 is not a prime.

  21. CS1010 (AY2014/5 Semester 1) Things-To-Do • Revise • Chapter 4 Lessons 4.1 – 4.6, Beginning Decision Making • Deadline for Lab #1 • Deadline: 6 September 2014, Saturday, 9am • Lab #2 released • Deadline: 13 September 2014, Saturday, 9am • Preparation for next week • Chapter 6: Numeric Arrays • Continue to do practice exercises on CodeCrunch

  22. CS1010 (AY2014/5 Semester 1) End of File

More Related