Follow
Publications: 102 | Followers: 1

CS1313 for Loop Lesson 2 - University of Oklahoma

Publish on Category: Birds 0

forLoop Lesson 2CS1313 Fall 2019
1
forLoop Lesson 2 Outline
forLoop Lesson 2 OutlineforLoop ApplicationFactorialFactorial Program #1Factorial Program #2forLoop With Implicit IncrementforLoop With Explicit Increment #1forLoop With Explicit Increment #2forLoop With Explicit Increment #3forLoop with Negative IncrementforLoop with Decrement Example #1forLoop with Decrement Example #2forLoop with Decrement Example #3
forLoop with Named ConstantsforLoop w/Named Constants Example #1forLoop w/Named Constants Example #2forLoop with VariablesforLoop with Variables Example #1forLoop with Variables Example #2forLoop with ExpressionsforLoop with Expressions Example #1forLoop with Expressions Example #2
forLoop Lesson 2CS1313 Fall 2019
2
forLoop Application
Suppose that there’s a line of a dozen students waiting for tickets for the next OU-Texas football game.How many different orders can they have in the line?The head of the line could be any student.The 2nd position in line could be any student except the student at the head of the line.The 3rd position in line could be any student except the student at the head of the line or the student in the 2nd position.And so on.
forLoop Lesson 2CS1313 Fall 2019
3
Factorial
Generalizing, we have that the number of different orders of the 12 students is:12∙11∙10∙...∙2∙1We can also express this in the other direction:1∙2∙3∙...∙12In fact, for any number of studentsn, we have that the number of orders is:1∙2∙3∙...∙nThis arithmetic expression is calledn factorial, denotedn!There aren!permutations(orderings) of thenstudents.
forLoop Lesson 2CS1313 Fall 2019
4
Factorial Program #1
#include <stdio.h>intmain (){ /* main */constintprogram_success_code= 0;intnumber_of_students;intpermutations;intcount;printf("How many students are in line for tickets?\n");scanf("%d", &number_of_students);permutations = 1;for (count = 1; count <=number_of_students; count++) {permutations = permutations * count;} /* for count */printf("There are %d different orders in which\n",permutations);printf(" the %d students can stand in line.\n",number_of_students);returnprogram_success_code;} /* main */
forLoop Lesson 2CS1313 Fall 2019
5
Factorial Program #2
%gcc -o permute permute.c%permuteHow many students are in line for tickets?12There are 479001600 different orders in whichthe 12 students can stand in line.
forLoop Lesson 2CS1313 Fall 2019
6
forLoop With Implicit Increment
The most common increment in aforloop is1. For convenience, therefore, we typically use theincrement operator++in our loop change. For example:int product;int count;product = 1;for (count = 1; count <= 5; count++ ) {product *= count;} /* for count */
forLoop Lesson 2CS1313 Fall 2019
7
forLoop With Explicit Increment #1
We could state the loop increment explicitly in theforstatement, by using, for example, an addition assignment operator+=int product;int count;product = 1;for (count = 1; count <= 5; count += 1) {product *= count;} /* for count */The above program fragment behavesidenticallyto the one on the previous slide. Notice that both of the above loops have 5 iterations:countof 1, 2, 3, 4, 5.
forLoop Lesson 2CS1313 Fall 2019
8
forLoop With Explicit Increment #2
On the other hand, if the loop increment isn’t 1, then itMUSTbe explicitly stated, using, for example, an addition assignment operator+=int product;int count;product = 1;for (count = 1; count <= 5; count += 2) {product *= count;} /* for count */Notice that the above loop has only 3 iterations:countof 1, 3, 5.
forLoop Lesson 2CS1313 Fall 2019
9
forLoop With Explicit Increment #3
int product;int count;product = 1;for (count = 1; count <= 5; count += 2) {product *= count;} /* for count */The above program fragment behavesidenticallyto:int product = 1;int count;count = 1; /* count == 1, product == 1 */product *= count; /* count == 1, product == 1 */count += 2; /* count == 3, product == 1 */product *= count; /* count == 3, product == 3 */count += 2; /* count == 5, product == 3 */product *= count; /* count == 5, product == 15 */count += 2; /* count == 7, product == 15 */
forLoop Lesson 2CS1313 Fall 2019
10
forLoop with Negative Increment
Sometimes, we want to loop backwards, from a high initial value to a low final value. To do this, we use a negative loop increment; that is, we use the decrement operator--:count--
forLoop Lesson 2CS1313 Fall 2019
11
forLoop with Decrement Example #1
#include <stdio.h>#include <math.h>intmain (){ /* main */constintinput_digits= 4;constintbase = 10;constintprogram_success_code= 0;intbase_power,input_value;intbase_digit_value,output_digit;printf("Input an integer of no more ");printf("than %d digits:\n",input_digits);scanf("%d", &input_value);
forLoop Lesson 2CS1313 Fall 2019
12
forLoop with Decrement Example #2
for (base_power=input_digits- 1;base_power>= 0;base_power--) {base_digit_value=pow(base,base_power);if (input_value<base_digit_value) {printf("%2d^%1d: 0\n",base,base_power,output_digit);} /* if (input_value< ...) */else {output_digit=input_value/base_digit_value;printf("%2d^%1d: %1d\n",base,base_power,output_digit);input_value=input_value-output_digit*base_digit_value;} /* if (input_value>= ...)...else */} /* forbase_power*/returnprogram_success_code;} /* main */
forLoop Lesson 2CS1313 Fall 2019
13
forLoop with Decrement Example #3
%gcc -o decimaldigits decimaldigits.c -lm%decimaldigitsInput an integer of no more than 4 digits:398410^3: 310^2: 910^1: 810^0: 4%decimaldigitsInput an integer of no more than 4 digits:102410^3: 110^2: 010^1: 210^0: 4
forLoop Lesson 2CS1313 Fall 2019
14
forLoop with Named Constants
For the loop lower bound and upper bound, and for the stride if there is one, we can useintnamed constants.
forLoop Lesson 2CS1313 Fall 2019
15
forLoop w/Named Constants Example #1
#include <stdio.h>intmain (){ /* main */const intinitial_sum= 0;const intinitial_value= 1;constintfinal_value= 20;constintstride = 3;constintprogram_success_code= 0;intcount, sum;sum =initial_sum;for (count =initial_value;count <=final_value; count += stride) {sum = sum + count;printf("count = %d, sum = %d\n",count, sum);} /* for count */printf("After loop, count = %d, sum = %d.\n",count, sum);returnprogram_success_code;} /* main */
forLoop Lesson 2CS1313 Fall 2019
16
forLoop w/Named Constants Example #2
%gcc-oloopbndconstsloopbndconsts.c%loopbndconstscount = 1, sum = 1count = 4, sum = 5count = 7, sum = 12count = 10, sum = 22count = 13, sum = 35count = 16, sum = 51count = 19, sum = 70After loop, count = 22, sum = 70.In fact, weshoulduseintnamedconstants instead ofintliteralconstants: it’s much better programming practice, because it’s much easier to change the loop bounds and the stride.
forLoop Lesson 2CS1313 Fall 2019
17
forLoop with Variables
For the loop lower bound, loop upper bound and loop stride, we can useintvariables.
forLoop Lesson 2CS1313 Fall 2019
18
forLoop with Variables Example #1
#include <stdio.h>intmain (){ /* main */const intinitial_sum= 0;const intprogram_success_code= 0;intinitial_value,final_value, stride;intcount, sum;printf("What are the initial, final and ");printf("stride values?\n");scanf("%d %d %d",&initial_value, &final_value, &stride);sum =initial_sum;for (count =initial_value;count <=final_value; count += stride) {sum = sum + count;printf("count = %d, sum = %d\n", count, sum);} /* for count */printf("After loop, count = %d, sum = %d.\n",count, sum);returnprogram_success_code;} /* main */
forLoop Lesson 2CS1313 Fall 2019
19
forLoop with Variables Example #2
%gcc -o loopbndvars loopbndvars.c%loopbndvarsWhat are the initial, final and stride values?1 7 2count = 1, sum = 1count = 3, sum = 4count = 5, sum = 9count = 7, sum = 16After the loop, count = 9, sum = 16.
forLoop Lesson 2CS1313 Fall 2019
20
forLoop with Expressions
If we don’t happen to have a variable handy that represents one of the loop bounds or the stride, then we can use an expression.
forLoop Lesson 2CS1313 Fall 2019
21
forLoop with Expressions Example #1
#include <stdio.h>intmain (){ /* main */const intinitial_sum= 0;constintprogram_success_code= 0;intinitial_value,final_value, multiplier;intcount, sum;printf("What are the initial, final and ");printf("multiplier values?\n");scanf("%d %d %d",&initial_value, &final_value, &multiplier);sum =initial_sum;for (count =initial_value* multiplier;count <=final_value* multiplier;count += multiplier - 1) {sum = sum + count;printf("count = %d, sum = %d\n", count, sum);} /* for count */printf("After loop, count = %d, sum = %d.\n",count, sum);returnprogram_success_code;} /* main */
forLoop Lesson 2CS1313 Fall 2019
22
forLoop with Expressions Example #2
%gcc -o loopbndexprs loopbndexprs.c%loopbndexprsWhat are the initial, final and multiplier values?1 7 2count = 2, sum = 2count = 3, sum = 5count = 4, sum = 9count = 5, sum = 14count = 6, sum = 20count = 7, sum = 27count = 8, sum = 35count = 9, sum = 44count = 10, sum = 54count = 11, sum = 65count = 12, sum = 77count = 13, sum = 90count = 14, sum = 104After the loop, count = 15, sum = 104.

0

Embed

Share

Upload

Make amazing presentation for free
CS1313 for Loop Lesson 2 - University of Oklahoma