WEEK 4 Class Activities Lecturer’s slides.

Slides:



Advertisements
Similar presentations
WEEK 5 Class Activities Lecturer’s slides.
Advertisements

CS1010 Programming Methodology
1 CSE1301 Computer Programming Lecture 8 Selection.
UNIT 12 UNIX I/O Redirection.
CS1010 Programming Methodology
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
CS1010 Programming Methodology
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
WEEK 9 Class Activities Lecturer’s slides.
CS1101: Programming Methodology
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology Aaron Tan.
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
CS1010E Programming Methodology Tutorial 3 Control Structures and Data Files C14,A15,D11,C08,C11,A02.
CS1010: Programming Methodology
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
UNIT 14 Functions with Pointer Parameters.
CS1101: Programming Methodology
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
CS1101: Programming Methodology Aaron Tan.
Homework Assignment #4 J. H. Wang Dec. 3, 2007.
WEEK 6 Class Activities Lecturer’s slides.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
UNIT 10 Multidimensional Arrays.
UNIT 11 Random Numbers.
WEEK 8 Class Activities Lecturer’s slides.
Principle Prog Revision. Question 1 (a) Identify errors in the following program segment and how the errors can be corrected. void main(){ constant int.
WEEK 1 Class Activities.
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
CS1010: Programming Methodology
WEEK 10 Class Activities Lecturer’s slides.
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Week 12 Class Activities.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
The Repetition control structure using while loop.
WEEK 1 Class Activities.
ECE Application Programming
CS1010 Programming Methodology
Introduction to Computer Algorithmics and Programming Ceng 113
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
CS1010 Programming Methodology
CS1010 Programming Methodology
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Control Structures Lecture 7.
The while Looping Structure
Lec 7.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSCE 206 Lab Structured Programming in C
Chapter 8 The Loops By: Mr. Baha Hanene.
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
UMBC CMSC 104 – Section 01, Fall 2016
Suppose I want to add all the even integers from 1 to 100 (inclusive)
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
EECE.2160 ECE Application Programming
CSCE 206 Lab Structured Programming in C
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

WEEK 4 Class Activities Lecturer’s slides

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  Exercise #3: Prime Number  Testing and Debugging (running theme) CS1010 (AY2015/6 Semester 1)Week4 - 2© NUS

Tracing while Loop (1/4) CS1010 (AY2015/6 Semester 1)Week4 - 3© NUS  Trace the following codes manually and write out their outputs int a = 1; while (a*a < 100) { printf("%d ", a); a *= 2; } printf("\n"); (a) 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); (b) 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

Tracing while Loop (2/4) CS1010 (AY2015/6 Semester 1)Week4 - 4© NUS  Example: Given a positive integer n, print out its digits from least significant to most significant.  Sample run: Enter a positive integer:

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

Tracing while Loop (4/4) CS1010 (AY2015/6 Semester 1)Week4 - 6© NUS // Precond: n > 0 void print_digits(int n) { int digit; while (n > 0) { digit = n%10; printf("%d\n", digit); n /= 10; } Week4_PrintDigits.c n initially28943 point point***34982 What are the values of n and digit after exiting the loop?

Tracing for Loop CS1010 (AY2015/6 Semester 1)Week4 - 7© NUS int i, sum = 0; for (i=0; i <= 10; i+=2) { sum += i; } printf("sum = %d\n", sum); (a) 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); (b) 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  Trace the following codes manually and write out their outputs

Warm-up: List a Range of Integers (1/3) CS1010 (AY2015/6 Semester 1)Week4 - 8© NUS  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 void list_integers(int, int); int main(void) { int a, b; printf("Enter 2 integers a and b (a<=b): "); scanf("%d %d", &a, &b); list_integers(a, b); return 0; }

Warm-up: List a Range of Integers (2/3) CS1010 (AY2015/6 Semester 1)Week4 - 9© NUS  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  Use a for loop to implement the function int num; for (num=lower; num<=upper; num++) { printf("%d ", num); } printf("\n");

Warm-up: List a Range of Integers (3/3) CS1010 (AY2015/6 Semester 1)Week4 - 10© NUS  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");

Exercise #1: Sum of Multiples of 3 (1/2) CS1010 (AY2015/6 Semester 1)Week4 - 11© NUS  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

Exercise #1: Sum of Multiples of 3 (2/2) CS1010 (AY2015/6 Semester 1)Week4 - 12© NUS  How about using a while loop instead?  Pseudo-code using a while loop: precondition: n > 0 sum  0 i  n while (i > 0) if i is a multiple of 3 sum  sum + i i  i - 1 return sum

Exercise #2: Asterisks (1/2) CS1010 (AY2015/6 Semester 1)Week4 - 13© NUS  Write a program Asterisks.c to 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: Enter n: 3 ***** Done! Enter n: 6 *********** Done! Enter n: 10 ******************* Done! Enter n: -2 Done! Think! What is the relationship between n and the number of *?

Exercise #2: Asterisks (2/2) CS1010 (AY2015/6 Semester 1)Week4 - 14© NUS  Write a program Asterisks.c to 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;

Tracing Nested Loops (1/5) CS1010 (AY2015/6 Semester 1)Week4 - 15© NUS Given the following 3 programs, hand trace each of them and write out the output without running the program.

Tracing Nested Loops (2/5) CS1010 (AY2015/6 Semester 1)Week4 - 16© NUS #include 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++; } return 0; } Week4_NestedLoop1.c 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

Tracing Nested Loops (3/5) CS1010 (AY2015/6 Semester 1)Week4 - 17© NUS #include 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; } Week4_NestedLoop2.c 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

Tracing Nested Loops (4/5) CS1010 (AY2015/6 Semester 1)Week4 - 18© NUS #include int main(void) { int p, 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; } Week4_NestedLoop3.c

Tracing Nested Loops (5/5) CS1010 (AY2015/6 Semester 1)Week4 - 19© NUS 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); } Week4_NestedLoop3.c 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

Exercise #3: Prime Number CS1010 (AY2015/6 Semester 1)Week4 - 20© NUS Primality test is a classic programming problem Given a positive integer, determine whether it is a prime A prime number 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: is a prime. Enter a positive integer: is not a prime.

Things-To-Do CS1010 (AY2015/6 Semester 1)Week Revise Chapter 4 Lessons 4.1 – 4.6, Beginning Decision Making Deadline for Lab #1 Deadline: 5 September 2015, Saturday, 9am Lab #2 released Deadline: 12 September 2015, Saturday, 9am Preparation for next week Chapter 6: Numeric Arrays Continue to do practice exercises on CodeCrunch © NUS

End of File CS1010 (AY2015/6 Semester 1)Week4 - 22© NUS