Starting Out with C++, 3 rd Edition 1 Chapter 5. Looping.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Loops and Files.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions Continued.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Control Structures Repetition or Iteration or Looping Part II.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Loops and Files. 5.1 The Increment and Decrement Operators.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
Starting Out with C++: From Control Structures through Objects
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
CS102 Introduction to Computer Programming Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter 5: Loops. Slide 5- 2 Outline Increment and Decrement while loop do-while loop for loop.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Starting Out with C++, 3 rd Edition 1 Chapter 5. Looping.
Starting Out with C++, 3 rd Edition. My first C++ program #include using namespace std; int main() { cout
Computer Programming -1-
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Chapter 4 Repetition Structures
REPETITION CONTROL STRUCTURE
Chapter 5. Looping.
Chapter 5: Loops and Files.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 2.2 Control Structures (Iteration)
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 4B More Repetition Richard Gesick
Chapter 8 Repetition Statements
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 5. Looping.
Chapter 4: Loops and Files
Chapter 2.2 Control Structures (Iteration)
Alternate Version of STARTING OUT WITH C++ 4th Edition
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chapter 4 Repetition Structures
Presentation transcript:

Starting Out with C++, 3 rd Edition 1 Chapter 5. Looping

Starting Out with C++, 3 rd Edition The Increment and Decrement Operators ++ and -- are operators that add and subtract one from their operands. num = num + 1; num += 1; num++;

Starting Out with C++, 3 rd Edition 3 Program 5-1 // This program demonstrates the increment and decrement // operators. #include void main(void) { int bigVal = 10, smallVal = 1; cout << "bigVal is " << bigVal << " and smallVal is " << smallVal << endl; smallVal++; bigVal--; Program continues…

Starting Out with C++, 3 rd Edition 4 Program continued from previous slide. cout << "bigVal is " << bigVal << " and smallVal is " << smallVal << endl; ++smallVal; --bigVal; cout << "bigVal is " << bigVal << " and smallVal is " << smallVal << endl; }

Starting Out with C++, 3 rd Edition 5 Program Output bigVal is 10 and smallVal is 1 bigVal is 9 and smallVal is 2 bigVal is 8 and smallVal is 3

Starting Out with C++, 3 rd Edition 6 Program 5-2 //This program demonstrates the prefix and postfix modes of the // increment and decrement operators. #include void main(void) { int bigVal = 10, smallVal = 1; cout << "bigVal starts as " << bigVal; cout << " and smallVal starts as " << smallVal << endl; cout << "bigVal--: " << bigVal-- << endl; cout << "smallVal++: " << smallVal++ << endl; cout << "Now bigVal is: " << bigVal << endl; cout << "Now smallVal is: " << smallVal << endl; cout << "--bigVal: " << --bigVal << endl; cout << "++smallVal: " << ++smallVal << endl; }

Starting Out with C++, 3 rd Edition 7 Program Output bigVal starts as 10 and smallVal starts as 1 bigVal--: 10 smallVal++: 1 Now bigVal is: 9 Now smallVal is: 2 --bigVal: 8 ++smallVal: 3

Starting Out with C++, 3 rd Edition 8 Using ++ and -- in Mathematical Expressions a = 2; b = 5; c = a * b++; cout << a << “ “ << b << “ “ << c; Results:

Starting Out with C++, 3 rd Edition 9 Using ++ and -- in Relational Expressions x = 10; if ( x++ > 10) cout << “x is greater than 10.\n”; Two operations are happening: the value in x is tested to determine if it is greater than 10 then x is incremented

Starting Out with C++, 3 rd Edition Introduction to Loops - The while Loop A loop is part of a program that repeats. A while loop is a “pre test” loop - the expression is tested before the loop is executed while (expression) statement;

Starting Out with C++, 3 rd Edition 11 Program 5-3 // This program demonstrates a simple while loop. #include void main(void) { int number = 0; cout << "This program will let you enter number after\n"; cout << "number. Enter 99 when you want to quit the "; cout << "program.\n"; while (number != 99) cin >> number; }

Starting Out with C++, 3 rd Edition 12 Program Output with Example Input This program will let you enter number after number. Enter 99 when you want to quit the program. 1 [Enter] 2 [Enter] 30 [Enter] 75 [Enter] 99 [Enter]

Starting Out with C++, 3 rd Edition 13 Terminating a Loop A loop that does not have a way of stopping is called an infinite loop int test = 0; while (test < 10) cout << “Hello\n”; A null statement is also an infinite loop, but it does nothing forever: while (test < 10);

Starting Out with C++, 3 rd Edition 14 Programming Style and the while Loop If there is only one statement repeated by the loop, it should appear on the line after the while statement and be indented one additional level If the loop repeats a block, the block should begin on the line after the while statement and each line inside the braces should be indented

Starting Out with C++, 3 rd Edition Counters A counter is a variable that is incremented or decremented each time a loop iterates.

Starting Out with C++, 3 rd Edition 16 Program 5-4 // This program displays the numbers 1 through 10 and // their squares. #include void main(void) { int num = 1; // Initialize counter cout << "number number Squared\n"; cout << " \n"; while (num <= 10) { cout << num << "\t\t" << (num * num) << endl; num++; // Increment counter } }

Starting Out with C++, 3 rd Edition 17 Program Output number number Squared

Starting Out with C++, 3 rd Edition 18 Program 5-5 // This program displays the numbers 1 through 10 and // their squares. #include void main(void) { int num = 0; cout << "number number Squared\n"; cout << " \n"; while (num++ < 10) cout << num << "\t\t" << (num * num) << endl; }

Starting Out with C++, 3 rd Edition 19 Program Output number number Squared

Starting Out with C++, 3 rd Edition Letting the User Control the Loop We can let the user indicate the number of times a loop should repeat.

Starting Out with C++, 3 rd Edition 21 Program 5-6 // This program averages a set of test scores for multiple // students. It lets the user decide how many. #include void main(void) { int numStudents, count = 0; cout << "This program will give you the average of three\n"; cout << "test scores per student.\n"; cout << "How many students do you have test scores for? "; cin >> numStudents; cout << "Enter the scores for each of the students.\n"; cout.precision(2); Program continues…

Starting Out with C++, 3 rd Edition 22 Program continued from previous slide. while (count++ < numStudents) { int score1, score2, score3; float average; cout << "\nStudent " << count << ": "; cin >> score1 >> score2 >> score3; average = (score1 + score2 + score3) / 3.0; cout << "The average is " << average << ".\n"; }

Starting Out with C++, 3 rd Edition 23 Program Output with Example Input This program will give you the average of three test scores per student. How many students do you have test scores for? 3 [Enter] Enter the scores for each of the students. Student 1: [Enter] The average is 79. Student 2: [Enter] The average is Student 3: [Enter] The average is

Starting Out with C++, 3 rd Edition Keeping a Running Total A running total is a sum of numbers that accumulates with each iteration of a loop. The variable used to keep the running total is called an accumulator.

Starting Out with C++, 3 rd Edition 25 Program 5-7 // This program takes daily sales figures over a period of // time and calculates their total. #include void main(void) { int days, count = 0; float total = 0.0; cout << "For how many days do you have sales figures? "; cin >> days; Program continues…

Starting Out with C++, 3 rd Edition 26 Program continues while (count++ < days) { float sales; cout << "Enter the sales for day " << count << ": "; cin >> sales; total += sales; } cout.precision(2); cout.setf(ios::fixed | ios::showpoint); cout << "The total sales are $" << total << endl; }

Starting Out with C++, 3 rd Edition 27 Program Output with Example Input For how many days do you have sales figures? 5 [Enter] Enter the sales for day 1: [Enter] Enter the sales for day 2: [Enter] Enter the sales for day 3: [Enter] Enter the sales for day 4: [Enter] Enter the sales for day 5: [Enter] The total sales are $

Starting Out with C++, 3 rd Edition Sentinels A sentinel is a special value that marks the end of a list of values.

Starting Out with C++, 3 rd Edition 29 Program 5-8 // This program calculates the total number of points a // soccer team has earned over a series of games. The user // enters a series of point values, then -1 when finished. #include void main(void) { int count = 0, points = 0, total = 0; cout << "Enter the number of points your team has earned\n"; cout << "so far in the season, then enter -1 when\n"; cout << "finished.\n"; Program continues…

Starting Out with C++, 3 rd Edition 30 Program continued from previous slide. while (points != -1) { count++; cout << "Enter the points for game " << count << ": "; cin >> points; if (points != -1) total += points; } cout << "The total points are " << total << endl; }

Starting Out with C++, 3 rd Edition 31 Program Output with Example Input Enter the number of points your team has earned so far in the season, then enter -1 when you are finished. Enter the points for game 1: 7 [Enter] Enter the points for game 2: 9 [Enter] Enter the points for game 3: 4 [Enter] Enter the points for game 4: 6 [Enter] Enter the points for game 5: 8 [Enter] Enter the points for game 6: -1 [Enter] The total points are 34

Starting Out with C++, 3 rd Edition The do-while Loop and for Loops In addition to the while loop, C++ also offers the do-while and for loops. A do-while loop is similar to a while loop, but in post-test format: do statement; while (expression);

Starting Out with C++, 3 rd Edition 33 Program 5-9 //This program averages 3 test scores. It repeats as many times as // the user wishes #include void main(void) { int score1, score2, score3; float average; char again; do { cout << "Enter 3 scores and I will average them: "; cin >> score1 >> score2 >> score3; average = (score1 + score2 + score3) / 3.0; cout << "The average is " << average << ".\n"; cout << "Do you want to average another set? (Y/N) "; cin >> again; } while (again == 'Y' || again == 'y'); }

Starting Out with C++, 3 rd Edition 34 Program Output with Example Input Enter 3 scores and I will average them: [Enter] The average is 80. Do you want to average another set? (Y/N) y [Enter] Enter 3 scores and I will average them: [Enter] The average is Do you want to average another set? (Y/N) n [Enter]

Starting Out with C++, 3 rd Edition 35 Program 5-10 // This program displays a menu and asks the user to make a // selection. A switch statement determines which item the // user has chosen. A do-while loop repeats the program until // the user selects item 4 from the menu. #include void main(void) { int choice, months; float charges; cout.setf(ios::fixed | ios::showpoint); cout.precision(2); Program continues…

Starting Out with C++, 3 rd Edition 36 Program continued from previous slide. do { cout << "\n\t\tHealth Club Membership Menu\n\n"; cout << "1. Standard Adult Membership\n"; cout << "2. Child Membership\n"; cout << "3. Senior Citizen Membership\n"; cout << "4. Quit the Program\n\n"; cout << "Enter your choice: "; cin >> choice; if (choice != 4) { cout << "For how many months? "; cin >> months; } Program continues…

Starting Out with C++, 3 rd Edition 37 Program continued from previous slide. switch (choice) { case 1: charges = months * 40.00; cout << "The total charges are $"; cout << charges << endl; break; case 2: charges = months * 20.00; cout << "The total charges are $"; cout << charges << endl; break; case 3: charges = months * 30.00; cout << "The total charges are $"; cout << charges << endl; break; Program continues…

Starting Out with C++, 3 rd Edition 38 Program continued from previous slide. case 4: cout << "Thanks for using this "; cout << "program.\n"; break; default: cout << "The valid choices are 1-4. "; cout << "Try again.\n"; } } while (choice != 4); } Program continues…

Starting Out with C++, 3 rd Edition 39 Program Output with Example Input Health Club Membership Menu 1. Standard Adult Membership 2. Child Membership 3. Senior Citizen Membership 4. Quit the Program Enter your choice: 1 [Enter] For how many months 12 [Enter] The total charges are $ Health Club Membership Menu 1. Standard Adult Membership 2. Child Membership 3. Senior Citizen Membership 4. Quit the Program Enter your choice: 4 [Enter] Thanks for using this program.

Starting Out with C++, 3 rd Edition 40 The for Loop Ideal for situations that require a counter because it has built-in expressions that initialize and update variables. for (initialization; test; update) statement;

Starting Out with C++, 3 rd Edition 41 Program 5-11 // This program displays the numbers 1 through 10 and // their squares. #include void main(void) { int num; cout << “Number Number Squared\n"; cout << " \n"; for (num = 1; num <= 10; num++) cout << num << "\t\t" << (num * num) << endl; }

Starting Out with C++, 3 rd Edition 42 Program Output Number Number Squared

Starting Out with C++, 3 rd Edition 43 Omitting the for Loop’s Expressions int num = 1; for ( ; num <= 10; num++) cout << num << “\t\t” << (num * num) << endl;

Starting Out with C++, 3 rd Edition 44 Using initialization and update lists You may need to perform more than one statement in the initialization part of a for loop. In that case, just separate the statements with commas.

Starting Out with C++, 3 rd Edition 45 Program 5-12 // This program takes daily sales figures for one week // and calculates their total. #include void main(void) { const int days = 7; int count; float total; for (count = 1, total = 0.0; count <= days; count++) { float sales; cout << "Enter the sales for day " << count << ": "; Program continues…

Starting Out with C++, 3 rd Edition 46 Program continued from previous slide. cin >> sales; total += sales; } cout.precision(2); cout.setf(ios::fixed | ios::showpoint); cout << "The total sales are $" << total << endl; }

Starting Out with C++, 3 rd Edition 47 Program Output with Example Input Enter the sales for day 1: [Enter] Enter the sales for day 2: [Enter] Enter the sales for day 3: [Enter] Enter the sales for day 4: [Enter] Enter the sales for day 5: [Enter] Enter the sales for day 6: [Enter] Enter the sales for day 7: [Enter] The total sales are $

Starting Out with C++, 3 rd Edition Other forms of the update expression Incrementing the counter by something besides 1: for(number = 2; number <= 100; number +=2) cout << number << endl; // print the even numbers 2 – 100 Going backwards: for(number = 10; number >= 0; number--) cout << number << endl; //count from 10 to 0 A stand-alone for loop //This one prints the integers from 1 to 10 for(number = 1; number <= 10; cout << number++) There are quite a few variations, try some of your own!

Starting Out with C++, 3 rd Edition Focus on Software Engineering: Deciding Which Loop to Use The while Loop A pre-test loop. Use when you do not want the loop to iterate if the condition is false from the beginning. Ideal if you want to use a sentinel. The do-while Loop A post-test loop. Use if you always want the loop to iterate at least once. The for Loop A pre-test loop. Automatically executes an update expression at the end of each iteration. Ideal for situations where a counter variable is needed. Used when the exact number of required iterations is known.

Starting Out with C++, 3 rd Edition Focus on Software Engineering: Nested Loops A loop that is inside another loop is called a nested loop.

Starting Out with C++, 3 rd Edition 51 Program 5-13 // This program averages test scores. It asks the user for the // number of students and the number of test scores per student. #include void main(void) { int numStudents, numTests, total; float average; cout << "This program averages test scores.\n"; cout << "For how many students do you have scores? "; cin >> numStudents; cout << "How many test scores does each student have? "; cin >> numTests; Program continues…

Starting Out with C++, 3 rd Edition 52 Program continued from previous slide. for (int count1 = 1; count1 <= numStudents; count1++) { total = 0; // Initialize accumulator for (int count2 = 1; count2 <= numTests; count2++) { int score; cout << "Enter score " << count2 << " for "; cout << "student " << count1 << ": "; cin >> score; total += score; // accumulate running total } average = total / numTests; cout << "The average score for student " << count1; cout << " is " << average << ".\n\n"; }

Starting Out with C++, 3 rd Edition 53 Program Output with Example Input This program averages test scores. For how many students do you have scores? 2 [Enter] How many test scores does each student have? 3 [Enter] Enter score 1 for student 1: 84 [Enter] Enter score 2 for student 1: 79 [Enter] Enter score 3 for student 1: 97 [Enter] The average for student 1 is 86. Enter score 1 for student 2: 92 [Enter] Enter score 2 for student 2: 88 [Enter] Enter score 3 for student 2: 94 [Enter] The average for student 2 is 91.

Starting Out with C++, 3 rd Edition Breaking Out of a Loop The break statement causes a loop to terminate early.

Starting Out with C++, 3 rd Edition 55 Program 5-14 // This program raises the user's number to the powers // of 0 through 10. #include void main(void) { int value; char choice; cout << "Enter a number: "; cin >> value; cout << "This program will raise " << value; cout << " to the powers of 0 through 10.\n"; Program continues…

Starting Out with C++, 3 rd Edition 56 Program continued from previous slide. for (int count = 0; count < 10; count++) { cout << value << " raised to the power of "; cout << count << " is " << pow(value, count); cout << "\nEnter Q to quit or any other key "; cout << "to continue. "; cin >> choice; if (choice == 'Q' || choice == 'q') break; }

Starting Out with C++, 3 rd Edition 57 Program Output Enter a number: 2 [Enter] This program will raise 2 to the powers of 0 through raised to the power of 0 is 1 Enter Q to quit or any other key to continue. C [Enter] 2 raised to the power of 1 is 2 Enter Q to quit or any other key to continue. C [Enter] 2 raised to the power of 2 is 4 Enter Q to quit or any other key to continue. Q [Enter]

Starting Out with C++, 3 rd Edition Using break in a nested loop The break statement below breaks out of the inner loop but NOT the outer loop for(int row = 0; row < 5; row++) { //begin outer loop for(star = 0; star < 20; star++) { //begin inner loop …………// some statements break; …………// some more statements } //end inner loop } //end outer loop

Starting Out with C++, 3 rd Edition The continue Statement The continue statement causes a loop to stop its current iteration and begin the next one.

Starting Out with C++, 3 rd Edition 60 Program 5-15 // This program calculates the charges for video rentals. // Every third video is free. #include void main(void) { int videoCount = 1, numVideos; float total = 0.0; char current; cout << "How many videos are being rented? "; cin >> numVideos; Program continues…

Starting Out with C++, 3 rd Edition 61 Program continued from previous slide. do { if ((videoCount % 3) == 0) { cout << "Video #" << videoCount << " is free!\n"; continue; } cout << "Is video #" << videoCount; cout << " a current release? (Y/N)"; cin >> current; if (current == 'Y' || current == 'y') total += 3.50; else total += 2.50; } while (videoCount++ < numVideos); Program continues…

Starting Out with C++, 3 rd Edition 62 Program continued from previous slide. cout.precision(2); cout.setf(ios::fixed | ios::showpoint); cout << "The total is $" << total; }

Starting Out with C++, 3 rd Edition 63 Program Output with Example Input How many Videos are being rented? 6 [Enter] Is video #1 a current release? y [Enter] Is video #2 a current release? n [Enter] Video #3 is free! Is video #4 a current release? n [Enter] Is video #5 a current release? y [Enter] Video #6 is free! The total is $12.00

Starting Out with C++, 3 rd Edition Using Loops for Input Validation Loops can be used to create input routines that repeat until acceptable data is entered.

Starting Out with C++, 3 rd Edition 65 Program 5-16 // This program calculates the number of soccer teams // that a youth league may create from the number of // available players. Input validation is demonstrated // with do-while loops. #include void main(void) { int players, teamPlayers, numTeams, leftOver; // Get the number of players per team. cout << “How many players you wish per team?\n”; cout << “(Enter a value in the range ): "; cin >> teamPlayers; Program continues…

Starting Out with C++, 3 rd Edition 66 Program continued from previous slide. while (teamPlayers 15) // Validate input {cout << "You should have at least 9 but no\n"; cout << "more than 15 per team.\n"; cout << “How many players do you wish per team? “; cin >> teamPlayers; } // Get the number of players available cout << “How many players are available? “; cin >> players; while (players < 0) // Validate input {cout << "Please enter a positive number.\n"; cin >> players; } // Perform calculations numTeams = players / teamPlayers; leftOver = players % teamPlayers; cout << "There will be " << numTeams << " teams with\n"; cout << leftOver << " players left over.\n"; }

Starting Out with C++, 3 rd Edition 67 Program Output with Example Input How many players you wish per team? (Enter a value in the range 9 – 15): 4[Enter] You should have at least 9 but no more than 15 per team. How many players you wish per team? 12[Enter] How many players are available? -142 [Enter] Please enter a positive number: 142[Enter] There will be 11 teams with 10 players left over.