1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)

Slides:



Advertisements
Similar presentations
1 CSE1301 Computer Programming Lecture 8 Selection.
Advertisements

For loops For loops are controlled by a counter variable. for( c =init_value;c
Character Input and Output C and Data Structures Baojian Hua
1 CSE1301 Computer Programming: Lecture 21 File I/O.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
1 CSE1301 Computer Programming Lecture 18 Arrays (Part 1)
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
CSE1301 Computer Programming: Lecture 19 File I/O
If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
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.
Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Real World Applications: Generating Prime Numbers  Problem Write a program that prints all positive prime integers less than or equal to n. A positive.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Introduction to C Programming CE Lecture 4 Further Control Structures in C.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
Computer Programming Control Structure
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
CSE1301 Computer Programming: Lecture 6 Input/Output.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4: Looping Structures LECTURER : MRS ROHANI HASSAN
CSE 220 – C Programming Loops.
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Condition Statements.
CSI 121 Structure Programming Language Lecture 17 Arrays (Part 1)
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Computer Programming Lecture 15 Text File I/O
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
- Additional C Statements
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Program Control Topics While loop For loop Switch statement
UMBC CMSC 104 – Section 01, Fall 2016
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Relational Operators Logical Operators for Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
CSCE 206 Lab Structured Programming in C
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)

2 Topics Infinite loops while and for Macros Input from a file Examples –CountConsonantsAndVowels –Factorization

3 Infinite Loops while ( 1 ) {...etc...etc...etc... } for ( ; 1 ; ) {...etc...etc...etc... } for ( ; ; ) {...etc...etc...etc... }

4 Infinite Loops while ( 1 ) {...etc...etc...etc... } for ( ; 1 ; ) {...etc...etc...etc... } for ( ; ; ) {...etc...etc...etc... } Use an: if ( condition ) { break; } statement to break the loop

5 while (1) { printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= 0) && (high <= 127) && (low < high)) { break; } else { printf("Bad bounds. Try again.\n"); } Example: asciiCheck.c

6 while (1) { printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= 0) && (high <= 127) && (low < high)) { break; } else { printf("Bad bounds. Try again.\n"); } Example: asciiCheck.c

7 while and for A for loop can always be rewritten as an equivalent while loop, and vice-versa The continue statement in a for loop passes control to the “update” expression

8 Example: asciiPrint Print out a section of the ASCII table for each character from the lower bound to higher bound { print its ascii value and ascii character } for ( ch = low; ch <= high; ch++ ) { printf("%d: %c\n", ch, ch); } asciiPrint1.c ch = low; while ( ch <= high ) { printf("%d: %c\n", ch, ch); ch++; } asciiPrint2.c

9 for ( ch = low; ch <= high; ch++ ) { printf("%d: %c\n", ch, ch); } ch = low; while (1) { printf("%d: %c\n", ch, ch); if (ch < high) { ch++; continue; } else { break; } asciiPrint3.c asciiPrint1.c Example: asciiPrint (cont)

10 for ( ch = low; ch <= high; ch++ ) { printf("%d: %c\n", ch, ch); } ch = low; for (;;) { printf("%d: %c\n", ch, ch); ch++; if (ch > high) { break; } asciiPrint4.c Example: asciiPrint (cont) asciiPrint1.c

11 while (1) { printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= MIN) && (high <= MAX) && (low < high)) { break; } else { printf("Bad bounds. Try again.\n"); } for (ch=low; ch <= high; ch++) { printf("%d: %c\n", ch, ch); } return 0; } Example: ascii1.c #include /* Print a section of the ASCII table */ #define MIN 0 #define MAX 127 int main() { int low, high; char ch;

12 while (1) { printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= MIN) && (high <= MAX) && (low < high)) { break; } else { printf("Bad bounds. Try again.\n"); } for (ch=low; ch <= high; ch++) { printf("%d: %c\n", ch, ch); } return 0; } #include /* Print a section of the ASCII table */ #define MIN 0 #define MAX 127 int main() { int low, high; char ch; Example: ascii1.c (cont)

13 while (1) { printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= MIN) && (high <= MAX) && (low < high)) { break; } else { printf("Bad bounds. Try again.\n"); } for (ch=low; ch <= high; ch++) { printf("%d: %c\n", ch, ch); } return 0; } #include /* Print a section of the ASCII table */ #define MIN 0 #define MAX 127 int main() { int low, high; char ch; Macro definition: #define identifier tokens All subsequent instances of identifier are replaced with its token Example: ascii1.c (cont)

14 Example 1: CountConsonantsAndVowels Count the number of consonants and the number of vowels in a file Non-alphabetic characters should not be counted

15 open file for input set consonantCount to 0 set vowelCount to 0 loop { input ch if (end of file) { exit loop } if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount } close file output consonantCount, vowelCount Algorithm

16 open file for input set consonantCount to 0 set vowelCount to 0 loop { input ch if (end of file) { exit loop } if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount } close file output consonantCount, vowelCount Algorithm (cont)

17 open file for input set consonantCount to 0 set vowelCount to 0 loop { input ch if (end of file) { exit loop } if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount } close file output consonantCount, vowelCount Algorithm (cont)

18 open file for input set consonantCount to 0 set vowelCount to 0 loop { input ch if (end of file) { exit loop } if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount } close file output consonantCount, vowelCount Algorithm (cont)

19 open file for input set consonantCount to 0 set vowelCount to 0 loop { input ch if (end of file) { exit loop } if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount } close file output consonantCount, vowelCount Algorithm (cont)

20 open file for input set consonantCount to 0 set vowelCount to 0 loop { input ch if (end of file) { exit loop } if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount } close file output consonantCount, vowelCount Algorithm (cont)

21 open file for input set consonantCount to 0 set vowelCount to 0 loop { input ch if (end of file) { exit loop } if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount } close file output consonantCount, vowelCount Read input from file? Algorithm (cont)

22 open file for input set consonantCount to 0 set vowelCount to 0 loop { input ch if (end of input) { exit loop } if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount } close file output consonantCount, vowelCount First, implement with input from stdin stream. Once working, modify to get input from file Algorithm (cont)

23 #include int main() { int consonantCount, vowelCount ; char ch ; consonantCount = 0 ; vowelCount = 0 ; printf("\nInput has %d consonants and %d vowels.\n", consonantCount, vowelCount) ; return 0; } Program

24 consonantCount = 0 ; vowelCount = 0 ; /* For each character in the file in turn, test if it is a consonant or a vowel, and adjust the total accordingly */ while ( scanf("%c", &ch) != EOF ) { } printf("\nInput has %d consonants and %d vowels.\n", consonantCount, vowelCount) ; Program

25 /* For each character in the file in turn, test if it is a consonant or vowel, and if so, adjust total accordingly */ while ( scanf("%c", &ch) != EOF ) { if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U') { /* Vowel */ vowelCount++ ; } else if ((ch >= 'a' && ch = 'A' && ch <= 'Z')) { /* Consonant, since vowels already dealt with */ consonantCount++ ; } } Program

26 #include /* Count the number of vowels, and the number of consonants in the input. */ int main() { int consonantCount, vowelCount ; char ch ; consonantCount = 0 ; vowelCount = 0 ; /* For each character in the file in turn, test if it is a consonant or vowel, and if so, adjust total accordingly. */ while ( scanf("%c", &ch) != EOF ) { if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U') { /* Vowel. */ vowelCount++ ; } else if ( (ch >= 'a' && ch = 'A' && ch <= 'Z') ) { /* Consonant, since vowels already dealt with. */ consonantCount++ ; } printf("\nInput has %d consonants and %d vowels.\n", consonantCount, vowelCount) ; return 0; } vowel1.c Modify to get input from file?

27 #include int main() { int consonantCount, vowelCount ; char ch ; consonantCount = 0 ; vowelCount = 0 ; printf("\nFile has %d consonants and %d vowels.\n", consonantCount, vowelCount) ; return 0; } while ( scanf("%c", &ch) != EOF ) { }...etc...etc...etc...

28 Input file stream (aka “file pointer”) #include int main() { FILE *inputFile ; int consonantCount, vowelCount ; char ch ; consonantCount = 0 ; vowelCount = 0 ; inputFile = fopen("yourFile.txt", "r") ; printf("\nFile has %d consonants and %d vowels.\n", consonantCount, vowelCount) ; fclose(inputFile) ; return 0; } while ( fscanf(inputFile, "%c", &ch) != EOF ) { }...etc...etc...etc...

29 #include int main() { FILE *inputFile ; int consonantCount, vowelCount ; char ch ; consonantCount = 0 ; vowelCount = 0 ; inputFile = fopen("yourFile.txt", "r") ; printf("\nFile has %d consonants and %d vowels.\n", consonantCount, vowelCount) ; fclose(inputFile) ; return 0; } while ( fscanf(inputFile, "%c", &ch) != EOF ) { }...etc...etc...etc... Declare file pointer Open file for input Read input from file Close file

30 #include /* Count the number of vowels, and the number of consonants in the file. */ int main() { FILE *inputFile ; int consonantCount, vowelCount ; char ch ; inputFile = fopen("yourFile.txt", "r") ; consonantCount = 0 ; vowelCount = 0 ; /* For each character in the file in turn, test if it is a consonant or vowel, and if so, adjust total accordingly. */ while ( fscanf(inputFile, "%c", &ch) != EOF ) { if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U') { /* Vowel. */ vowelCount++ ; } else if ( (ch >= 'a' && ch = 'A' && ch <= 'Z') ) { /* Consonant, since vowels already dealt with. */ consonantCount++ ; } printf("\nFile has %d consonants and %d vowels.\n", consonantCount, vowelCount) ; fclose(inputFile) ; return 0; } vowel2.c More on file input/output in Lecture 14

31 Example 2: Factorization Write a program which prints out the prime factorization of a number (treat 2 as the first prime) For example, on input 6, desired output is: 2 3 " " 24, " " : " " 14, " " : 2 7 " " 23, " " : 23 (23 is prime)

32 input n set factor to 2 Algorithm

33 input n set factor to 2 while(some factor yet to try) { } Algorithm (cont)

34 input n set factor to 2 while(some factor yet to try) { if (n is divisible by factor) { output factor set n to n / factor } } Algorithm (cont)

35 input n set factor to 2 while(some factor yet to try) { if (n is divisible by factor) { output factor set n to n / factor } else { increment factor } Algorithm (cont)

36 input n set factor to 2 while(some factor yet to try) { if (n is divisible by factor) { output factor set n to n / factor } else { increment factor } while(some factor yet to try) { if (n is divisible by factor) { output factor set n to n / factor } increment factor } Algorithm (cont) Why not?

37 #include /* Print out the prime factors of a number */ int main() { int n, factor ; return 0; } Program

38 #include /* Print out the prime factors of a number */ int main() { int n, factor ; printf("\nEnter integer: ") ; scanf("%d", &n) ; return 0; } Program (cont)

39 #include /* Print out the prime factors of a number */ int main() { int n, factor ; printf("\nEnter integer: ") ; scanf("%d", &n) ; printf("\nThe prime factors of %d are: ", n) ; /* Try each possible factor in turn */ printf("\n\n"); return 0; } Program (cont)

40 /* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { }

41 /* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; }

42 /* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor */ factor++ ; }

43 /* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor */ factor++ ; }

44 #include /* Print out the prime factors of a number */ int main() { int n, factor ; printf("\nEnter integer: ") ; scanf("%d", &n) ; printf("\nThe prime factors of %d are: ", n) ; /* Try each possible factor in turn */ factor = 2 while ( factor 1 ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor. */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor */ factor++ ; } printf("\n\n"); return 0; } factor1.c

45 /* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor */ factor++ ; } Change from while -loop to for - loop?

46 /* Try each possible factor in turn */ for ( factor = 2; factor 1 ; ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor */ factor++ ; }

47 #include /* Print out the prime factors of a number. */ int main() { int n, factor ; printf("\nEnter integer: ") ; scanf("%d", &n) ; printf("\nThe prime factors of %d are: ", n) ; /* Try each possible factor in turn. */ for ( factor = 2; factor 1 ; ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor. */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor. */ factor++ ; } printf("\n\n"); return 0; } factor2.c

48 Reading Deitel &Deitel Chapter 4, –Exercises 4.16, 4.36