2 4.1 Introduction Before writing a program While writing a program Have a thorough understanding of problemCarefully plan your approach for solving itWhile writing a programKnow what “building blocks” are availableUse good programming principles
3 4.2 Algorithms Algorithms Control Statements The actions to execute The order in which these actions executeControl StatementsSpecify the order in which actions execute in a program
4 4.3 Pseudocode Pseudocode Artificial, informal language used to develop algorithmsUsed to think out program before codingEasy to convert into program (eg. C++)Executable statements in EnglishFor the programmer, not executed on computers
5 Fig. 4.1 | Pseudocode for the addition program of Fig. 2.5. Problem: Read two integers from the user and display their sum.Fig | Pseudocode for the addition program of Fig. 2.5.
6 4.4 Control StructuresOnly three types of structured constructs needed (one-in one-out)Sequencestatements executed sequentially by defaultConditionalif, if…else, switchRepetitionwhile, do…while, forNo goto statements => structured programmingDemonstrated by Böhm and Jacopini
7 4.4 Control Structures (Cont.) UML activity diagramModels the workflowAction state symbolsRectangles with curved sidesSmall circlesSolid circle is the initial stateSolid circle in a hollow circle is the final stateTransition arrowsRepresent the flow of activityComment notesConnected to diagram by dotted lines
12 4.7 while Repetition Statement (Cont.) UML merge symbolJoins two or more flows of activity into one flow of activityRepresented as a diamondUnlike the decision symbol a merge symbol hasMultiple incoming transition arrowsOnly one outgoing transition arrowsNo guard conditions on outgoing transition arrowsHas no counterpart in C++ code
13 Fig. 4.6 | while repetition statement UML activity diagram.
14 4.9 Formulating Algorithms: Repetition Problem statementDevelop a class average program that processes grades for an arbitrary number of students each time it is run.Top-down, stepwise refinementDevelopment technique for well-structured programsTop stepSingle statement conveying overall function of the programExampleDetermine the class average for the quizFirst refinementMultiple statements using only the sequence structureInitialize variablesInput, sum and count the quiz gradesCalculate and print the total of all student grades and the class average
15 Determine the class average for the quiz. Initialize variablesInput, sum and count the quiz gradesCalculate and print the total of all student grades and the class averageFig | Class average problem pseudocode algorithm with sentinel-controlled repetition.
17 Outlinefixed forces output to print in fixed point format (not scientific notation) and forces trailing zeros and decimal point to printfig04_13.cpp(1 of 4)steprecision stream manipulator (in header <iomanip>) sets numeric output precision
19 OutlineFunction determineClassAverage implements the class average algorithm described by the pseudocodefig04_13.cpp(3 of 4)Declare local int variables total, gradeCounter and grade and double variable averagewhile loop iterates as long as grade does not equal the sentinel value -1
20 Outlinefig04_13.cpp(4 of 4)Calculate average grade using static_cast< double > to perform explicit conversion
22 4.10 Formulating Algorithms: Nested Control Statement Problem statementA college offers a course that prepares students for the state licensing exam for real estate brokers. Last year, ten of the students who completed this course took the exam. The college wants to know how well its students did on the exam. You have been asked to write a program to summarize the results. You have been given a list of these 10 students. Next to each name is written a 1 if the student passed the exam or a 2 if the student failed.Your program should analyze the results of the exam as follows:1. 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.2. Count the number of test results of each type.3. Display a summary of the test results indicating the number of students who passed and the number who failed.4. If more than eight students passed the exam, print the message “Raise tuition.”
23 Outlinefig04_18.cpp(2 of 2)More than eight students passed the exam
24 4.10 Formulating Algorithms: Nested Control Statement (Cont.) Notice thatProgram processes 10 resultsFixed number, use counter-controlled loopEach test result is 1 or 2If not 1, assume 2Two counters can be usedOne counts number that passedAnother counts number that failedMust decide whether more than eight students passed
25 4.10 Formulating Algorithms: Nested Control Statement (Cont.) Top level outlineAnalyze exam results and decide whether tuition should be raisedFirst refinementInitialize variables Input the ten exam results and count passes and failures Print a summary of the exam results and decide whether tuition should be raisedSecond RefinementInitialize variables to Initialize passes to zero Initialize failures to zero Initialize student counter to one
26 4.10 Formulating Algorithms: Nested Control Statement (Cont.) Second Refinement (Cont.)Input the ten exam results and count passes and failures to While student counter is less than or equal to ten Prompt the user to enter the next exam result If the student passed Add one to passes Else Add one to failuresAdd one to student counter
27 4.10 Formulating Algorithms: Nested Control Statement (Cont.) Second Refinement (Cont.)Print a summary of the exam results and decide whether tuition should be raised to Print the number of passes Print the number of failures If more than eight students passed Print “Raise tuition”
28 Fig. 4.15 | Pseudocode for examination-results problem.
35 4.12 Increment and Decrement Operators Increment operator ++Increments variable by oneExamplec++Decrement operator --Decrement variable by onec--
36 4.12 Increment and Decrement Operators (Cont.) PreincrementWhen the operator is used before the variable (++c or --c)Variable is changed, then the expression it is in is evaluated using the new valuePostincrementWhen the operator is used after the variable (c++ or c--)Expression the variable is in executes using the old value, then the variable is changed