DOC PREVIEW
UD CISC 181 - Assignment Operators

This preview shows page 1-2-3-4-25-26-27-52-53-54-55 out of 55 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CISC181 Introduction to Computer Science Dr. McCoy Lecture 4 September 10, 20092.11 Assignment Operators2.7 while Repetition Structure2.7 The while Repetition Structure2.8 Formulating Algorithms (Counter-Controlled Repetition)Slide 6fig02_07.cpp (1 of 2)fig02_07.cpp (2 of 2) fig02_07.cpp output (1 of 1)2.9 Formulating Algorithms (Sentinel-Controlled Repetition)Slide 10Slide 11Slide 12Slide 13fig02_09.cpp (1 of 3)fig02_09.cpp (2 of 3)fig02_09.cpp (3 of 3) fig02_09.cpp output (1 of 1)Example ProgramExercise 2.16Example output Ex. 2.16Slide 20Slide 212.13 Essentials of Counter-Controlled Repetitionfig02_16.cpp (1 of 1)fig02_16.cpp output (1 of 1)2.14 for Repetition Structurefig02_17.cpp (1 of 1)fig02_17.cpp output (1 of 1)Slide 28Slide 29fig02_20.cpp (1 of 1) fig02_20.cpp output (1 of 1)Practice writing for’sExercise 2.282.16 switch Multiple-Selection StructureSlide 34Slide 35fig02_22.cpp (1 of 4)fig02_22.cpp (2 of 4)fig02_22.cpp (3 of 4)fig02_22.cpp (4 of 4)fig02_22.cpp output (1 of 1)2.17 do/while Repetition StructureSlide 42fig02_24.cpp (1 of 1) fig02_24.cpp output (1 of 1)2.18 break and continue Statementsfig02_26.cpp (1 of 2)fig02_26.cpp (2 of 2) fig02_26.cpp output (1 of 1)Slide 47fig02_27.cpp (1 of 2)fig02_27.cpp (2 of 2) fig02_27.cpp output (1 of 1)2.21 Structured-Programming SummarySlide 51Slide 52Slide 53Slide 54Slide 551CISC181 Introduction to Computer ScienceDr. McCoyLecture 4September 10, 2009 2003 Prentice Hall, Inc. All rights reserved.22.11 Assignment Operators•Assignment expression abbreviations–Addition assignment operator c = c + 3; abbreviated to c += 3; •Statements of the formvariable = variable operator expression;can be rewritten asvariable operator= expression;•Other assignment operatorsd -= 4 (d = d - 4)e *= 5 (e = e * 5)f /= 3 (f = f / 3)g %= 9 (g = g % 9) 2003 Prentice Hall, Inc. All rights reserved.32.7 while Repetition Structure•Repetition structure–Action repeated while some condition remains true–Psuedocodewhile there are more items on my shopping list Purchase next item and cross it off my list –while loop repeated until condition becomes false•Exampleint product = 2;while ( product <= 1000 ) product = 2 * product; 2003 Prentice Hall, Inc. All rights reserved.42.7 The while Repetition Structure 2003 Prentice Hall, Inc. All rights reserved.52.8 Formulating Algorithms (Counter-Controlled Repetition)•Counter-controlled repetition–Loop repeated until counter reaches certain value•Definite repetition–Number of repetitions known •Example A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz. 2003 Prentice Hall, Inc. All rights reserved.62.8 Formulating Algorithms (Counter-Controlled Repetition)•Pseudocode for example:Set total to zeroSet grade counter to oneWhile grade counter is less than or equal to tenInput the next gradeAdd the grade into the totalAdd one to the grade counterSet the class average to the total divided by tenPrint the class average•Next: C++ code for this example 2003 Prentice Hall, Inc.All rights reserved.Outline7fig02_07.cpp(1 of 2)1 // Fig. 2.7: fig02_07.cpp2 // Class average program with counter-controlled repetition.3 #include <iostream>4 5 using std::cout;6 using std::cin;7 using std::endl;8 9 // function main begins program execution10 int main()11 {12 int total; // sum of grades input by user13 int gradeCounter; // number of grade to be entered next14 int grade; // grade value15 int average; // average of grades16 17 // initialization phase18 total = 0; // initialize total19 gradeCounter = 1; // initialize loop counter20 2003 Prentice Hall, Inc.All rights reserved.Outline8fig02_07.cpp(2 of 2)fig02_07.cppoutput (1 of 1)21 // processing phase22 while ( gradeCounter <= 10 ) { // loop 10 times23 cout << "Enter grade: "; // prompt for input24 cin >> grade; // read grade from user25 total = total + grade; // add grade to total26 gradeCounter = gradeCounter + 1; // increment counter27 }28 29 // termination phase30 average = total / 10; // integer division31 32 // display result33 cout << "Class average is " << average << endl; 34 35 return 0; // indicate program ended successfully36 37 } // end function mainEnter grade: 98Enter grade: 76Enter grade: 71Enter grade: 87Enter grade: 83Enter grade: 90Enter grade: 57Enter grade: 79Enter grade: 82Enter grade: 94Class average is 81The counter gets incremented each time the loop executes. Eventually, the counter causes the loop to end. 2003 Prentice Hall, Inc. All rights reserved.92.9 Formulating Algorithms (Sentinel-Controlled Repetition)•Suppose problem becomes: Develop a class-averaging program that will process an arbitrary number of grades each time the program is run–Unknown number of students–How will program know when to end?•Sentinel value–Indicates “end of data entry”–Loop ends when sentinel input–Sentinel chosen so it cannot be confused with regular input •-1 in this case 2003 Prentice Hall, Inc. All rights reserved.102.9 Formulating Algorithms (Sentinel-Controlled Repetition)•Top-down, stepwise refinement–Begin with pseudocode representation of topDetermine the class average for the quiz–Divide top into smaller tasks, list in orderInitialize variablesInput, sum and count the quiz gradesCalculate and print the class average 2003 Prentice Hall, Inc. All rights reserved.112.9 Formulating Algorithms (Sentinel-Controlled Repetition)•Many programs have three phases–Initialization•Initializes the program variables–Processing•Input data, adjusts program variables–Termination•Calculate and print the final results–Helps break up programs for top-down refinement 2003 Prentice Hall, Inc. All rights reserved.122.9 Formulating Algorithms (Sentinel-Controlled Repetition)•Refine the initialization phaseInitialize variables goes toInitialize total to zeroInitialize counter to zero•ProcessingInput, sum and count the quiz gradesgoes to Input the first grade (possibly the sentinel)While the user has not as yet entered the sentinel Add this grade into the running total Add one to the grade


View Full Document
Download Assignment Operators
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Assignment Operators and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Assignment Operators 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?