DOC PREVIEW
UD CISC 181 - Assignment Operators

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 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 10 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 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CISC181 Introduction to Computer ScienceDr McCoy1Dr. McCoyLecture 4September 10, 200922.11 Assignment Operators• Assignment expression abbreviations– Addition assignment operatorc = c + 3; abbreviated to c += 3;• Statements of the formibl ibl t i 2003 Prentice Hall, Inc. All rights reserved.variable = 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) 32.7 while Repetition Structure• Repetition structure– Action repeated while some condition remains true– Psuedocodewhile there are more items on my shopping listPurchase next item and cross it off my listwhileloop repeated until condition becomes false 2003 Prentice Hall, Inc. All rights reserved.–whileloop repeated until condition becomes false•Exampleint product = 2;while ( product <= 1000 )product = 2 * product;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 2003 Prentice Hall, Inc. All rights reserved.ExampleA 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. 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 total 2003 Prentice Hall, Inc. All rights reserved.Add one to the grade counterSet the class average to the total divided by tenPrint the class average• Next: C++ code for this example2Outline7fig02_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 grades 2003 Prentice Hall, Inc.All rights reserved.16 17 // initialization phase18 total = 0; // initialize total19 gradeCounter = 1; // initialize loop counter20 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 return0; // indicate program ended successfullyThe counter gets incremented each time the loop executes. Etllth t 2003 Prentice Hall, Inc.All rights reserved.36 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 81Eventually, the counter causes the loop to end.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?Sill 2003 Prentice Hall, Inc. All rights reserved.•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 case102.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 grades 2003 Prentice Hall, Inc. All rights reserved.Input, sum and count the quiz gradesCalculate and print the class average 112.9 Formulating Algorithms (Sentinel-Controlled Repetition)• Many programs have three phases– Initialization• Initializes the program variables– Processing• Input data, adjusts program variablesTiti 2003 Prentice Hall, Inc. All rights reserved.–Termination• Calculate and print the final results– Helps break up programs for top-down refinement122.9 Formulating Algorithms (Sentinel-Controlled Repetition)• Refine the initialization phaseInitialize variablesgoes toInitialize total to zeroInitialize counter to zero•Processing 2003 Prentice Hall, Inc. All rights reserved.•ProcessingInput, sum and count the quiz gradesgoes to Input the first grade (possibly the sentinel)While the user has not as yet entered the sentinelAdd this grade into the running totalAdd one to the grade counterInput the next grade (possibly the sentinel)3132.9 Formulating Algorithms (Sentinel-Controlled Repetition)• TerminationCalculate and print the class averagegoes toIf the counter is not equal to zeroSet the average to the total divided by the counterPrint the average 2003 Prentice Hall, Inc. All rights reserved.Print the averageElsePrint “No grades were entered” • Next: C++ programOutline14fig02_09.cpp(1 of 3)1 // Fig. 2.9: fig02_09.cpp2 // Class average program with sentinel-controlled repetition.3 #include <iostream>4 5 using std::cout;6 using std::cin;7 using std::endl;8 using std::fixed;9 10 #include <iomanip> // parameterized stream manipulators11 12 using std::setprecision; // sets numeric output precision 13 14 // function main begins program execution15 intmain() 2003 Prentice Hall, Inc.All rights reserved.16 {17 int total; // sum of grades18 int gradeCounter; // number of grades entered19 int grade; // grade value20 21 double average; // number with decimal point for average22 23 // initialization phase24 total = 0; // initialize total25 gradeCounter = 0; // initialize loop


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?