210 likes | 230 Views
Learn about layers of control and nested loops in C programming. From multiplying numbers to analyzing test results, explore algorithms and pseudocode. Discover the power of increment and assignment operators in code.
E N D
Nested while Loops • Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start at i=1, j=1 and go from there) • Formulate pseudocode algorithm • i = 1 j = 1: 1 * j = j = 2: 1 * j = j = 3: 1 * j = j = 4: 1 * j = • i = 2 j = 1: 2 * j = j = 2: 2 * j = j = 3: 2 * j = j = 4: 2 * j = j = 1; While j is less than 5 print out the result of 1 * j; j = j + 1; j = 1; While j is less than 5 print out the result of 2 * j; j = j + 1;
Nested while Loops • Formulate pseudocode algorithm • i = 3 j = 1: 3 * j = j = 2: 3 * j = j = 3: 3 * j = j = 4: 3 * j = • i = 4 j = 1: 4 * j = j = 2: 4 * j = j = 3: 4 * j = j = 4: 4 * j = j = 1; While j is less than 5 print out the result of 3 * j; j = j + 1; j = 1; While j is less than 5 print out the result of 4 * j; j = j + 1;
Formulate Pseudocode Algorithm • i = 1 • i = 2 • i = 3 • i = 4 j = 1; While j is less than 5 print out the result of 1 * j; j = j + 1; i j = 1; While j is less than 5 print out the result of 2 * j; j = j + 1; i = 1; While i is less than 5 i = i + 1; i j = 1; While j is less than 5 print out the result of 1 * j; j = j + 1; i j = 1; While j is less than 5 print out the result of 3 * j; j = j + 1; i j = 1; While j is less than 5 print out the result of 4 * j; j = j + 1; i
Pseudocode i = 1; While i is less than 5 i = i + 1; j = 1; While j is less than 5 print out the result of i * j; j = j + 1;
Nested Control Structures • Problem A college has a list of test results (1 = pass, 2 = fail) for 10 students. Write a program that analyzes the results as follows: • Input each test result (i.e., a 1 or a 2). Display the prompting message “Enter result” each time the program requests another test result. • Count the number of test results of each type • Display a summary of the test results indicating the number of students who passed and the number who failed • If more than 8 students passed the test, print the message “Raise tuition.”
Before Formulate the Algorithm • The program must process 10 test results • Counter-controlled loop will be used • Two counter can be used • One for number of passes, one for number of fails • Each test result is a number -- either a 1 or a 2 • If the number is not a 1, we assume that it is a 2 • Decide if more than 8 students passed the test.
Formulate the Pseudocode Algorithm • Top level outline Analyze exam results and decide if tuition should be raised • First refinement Initialize variables Input the ten quiz grades and count passes and failures Print a summary of the exam results and decide if tuition should be raised • Refine Initialize variables to Initialize passes to zero Initialize failures to zero Initialize student counter to one
Formulate the Pseudocode Algorithm • Refine Input the ten quiz grades and count passes and failuresto While student counter is less than or equal to ten Input the next exam result If the student passed Add one to passes else Add one to failures Add one to student counter • Refine Print a summary of the exam results and decide if tuition should be raisedto Print the number of passes Print the number of failures If more than eight students passed Print “Raise tuition”
Initializing variables when they are defined can help reduce a program’s execution time. while loop continues until 10 students have been processed if and else statements are nested inside while loop C code for Test Results Analysis
Assignment Operators • Assignment operators abbreviate assignment expressions c = c + 3 can be abbreviated as c += 3; using the addition assignment operator “+=” • Statements of the form variable=variableoperatorexpression; can be rewritten as variableoperator=expression; • Examples of other assignment operators: d -= 4 (d = d - 4) e *= 5 (e = e * 5) f /= 3 (f = f / 3) g %= 9 (g = g % 9)
Increment and Decrement Operators • Increment operator (++) Can be used instead of c += 1 • Decrement operator (--) Can be used instead of c -= 1 • Pre-increment/Pre-decrement • Operator is used before the variable (++c or --c) • Variable is changed before the expression it is in is evaluated • Post-increment/Post-decrement • Operator is used after the variable (c++ or c--) • Expression executes before the variable is changed
Increment and Decrement Operators • If c equals 5, then • printf( "%d", ++c ); Prints 6 • printf( "%d", c++ ); Prints 5 • In either case, c now has the value of 6 • When variable not in an expression • Preincrementing and postincrementing have the same effect ++c; printf( “%d”, c ); Has the same effect as c++; printf( “%d”, c );
c is printed, then incremented c is incremented, then printed Increment and Decrement Operators
In-Class Programming Exercise Create a program that prints out a triangle with a user specified width. Example output: Enter the specified width:5 X XX XXX XXXX XXXXX XXXX XXX XX X