C++ Summing, Counting and Nested Loops Chapter 2B CSIS 10A

Download Report

Transcript C++ Summing, Counting and Nested Loops Chapter 2B CSIS 10A

C++ Summing, Counting
and Nested Loops
Chapter 2B
CSIS 10A
1
Agenda
Review
Counting events
Summing and Averaging numbers
Nesting Branches in Branches
Nesting Branches in Loops
Nesting Loops in Loops
2
Two kinds of loops
Counted Loop:
Repeat a certain number of times
Uses a counter to keep track of when to stop
Indefinite Loop:
Repeat until a key value (sentinel) is entered
Don’t know when user will decide to stop
3
Indefinite Loop Code—review
// CALCPAY.CPP A program to calculate paychecks
#include <iostream>
using namespace std;
int main( )
{ float hours, pay, rate;
cout << "enter hours and rate, -1 to stop: ";
cin >> hours >> rate;
// get first data set
while(hours>0)
// is data OK
{
pay = rate * hours;
// process last data set
cout << "pay is " << pay;
cin >> hours >> rate;
// get next data set
}
return 0;
}
4
How many times did loop cycle?
An indefinite loop sometimes needs a counter, not to stop the loop but
to keep track of how many times loop has been executed
k=0;
// initialize counter
cout << "enter hours and rate, -1 to stop: ";
cin >> hours >> rate;
while(hours>0) // loop control is still sentinel based
{
pay = rate * hours;
cout << "pay is " << pay;
cin >> hours >> rate;
k=k+1;
// add 1 to counter each time through
}
cout<<“You processed “<<k<<“ employees”<<endl;
5
How to Sum a list of numbers
// SUM.CPP A program to sum a list of positive values
#include <iostream>
using namespace std;
int main()
{ float number, sum;
sum=0.0;
cout << "enter a list of pos. values (ending with -1.0): ";
cin >> number;
while (number > 0.0)
{
sum = sum + number; // add value of number to sum
cin >> number;
}
cout << “The total sum is " << sum << endl;
return 0;
}
6
Combine Sum and Count to get Avg
// SUMAVE.CPP A program to sum and average a list
// of positive values
#include <iostream>
using namespace std;
int main( )
{
float number, sum = 0.0, count = 0.0, average;
cout << "enter a list of pos. values (ending with -1.0): ";
cin >> number;
while (number > 0.0)
{
sum = sum + number;
count = count + 1.0;
cin >> number;
}
cout << " sum is " << sum << endl;
average = sum / count;
cout << "The average is: " << average << endl;
return 0;
}
What will be the
result of this
input:
10
15
20
-1
7
Nesting if-else (one-way)
// CALCPAY3.CPP Program with 3 formula choices
#include <iostream>
using namespace std;
int main()
{
float hours, pay, rate, overtime;
cout << "enter hours and rate: ";
cin >> hours >> rate;
Draw a flowchart
of this code
if (hours <= 40.0)
{
pay = hours * rate;
}
else
{
overtime = hours - 40.0;
if (hours <= 60.0)
{
pay = (40.0 * rate) + (overtime * 1.5 * rate);
}
else
{
pay = (40.0 * rate) + (overtime * 2.0 * rate);
}
}
cout << "pay is " << pay;
return 0;
}
8
Nesting if-else (another way)
// CALCPAY3.CPP Program with 3 formula choices
#include <iostream>
using namespace std;
int main()
{ float hours, pay, rate;
cout << "enter hours and rate: ";
cin >> hours >> rate;
Draw a flowchart
of this code
if (hours <= 40.0)
pay = hours * rate;
else if (hours <= 60.0)
pay = 40.0 * rate + (hours-40) * 1.5 * rate;
else
pay = 40.0 * rate + (hours-40) * 2.0 * rate;
cout << "pay is " << pay;
return 0;
}
9
Nesting if-else in a while loop
Many possibilities
Display pass/fail for a list of scores
Only add numbers below 100 to sum
Calculate Pay/Overtime for a list of employees
Finding the largest in a list
while (
{
if (
…..;
else
…..;
…;
…;
}
)
)
10
Pay/Overtime For List of Employees
// CALCPAY.CPP A program to calculate paychecks
#include <iostream>
using namespace std;
int main( )
{ float hours, pay, rate;
cout << "enter hours and rate, -1 to stop: ";
cin >> hours >> rate;
// get first data set
while(hours>0)
// is data OK
{
if (hours<=40)
pay = rate * hours;
else
pay=rate*40 + rate*1.5* (hours-40);
cout << "pay is " << pay;
cin >> hours >> rate;
// get next data set
}
return 0;
}
11
Pay/Overtime For 10 Employees
// CALCPAY.CPP A program to calculate paychecks
#include <iostream>
using namespace std;
int main( )
{ float hours, pay, rate, k=0;
while(k<10)
// counter is OK
{
cout << "enter hours and rate, for employee “<<k<<endl;
cin >> hours >> rate;
// get first data set
if (hours<=40)
pay = rate * hours;
else
pay=rate*40 + rate*1.5* (hours-40);
cout << "pay is " << pay;
}
return 0;
}
12
Finding the largest…
float num, max=0; // make a variable to hold max
cin>>num;
Core idea
Compare num with max…
if (num>max)
// if num is bigger
max=num;
// max get’s num’s value
else
;
// leave max alone
13
Finding the largest…put in a loop
cout << "enter a list of numbers (quit with -1): \n";
cin >> num;
while (num > 0) // indefinite loop
{
if (num>max)
max=num;
else
;
cin >> num;
}
cout << "the largest number was " << max << endl;
14
Using a flag (done) to control loop
float done=1, data, k=0;
cout << "enter a list of data ((ending with -1): ";
cin >> data;
while (done > 0)
{
if (data<0)
done=0; // stop if data is negative
else if (k>=5)
done=0; // or if more than 5 data entered
else
{
cout<<“You entered “ <<data<<endl;
k = k + 1.0;
cin >> data;
}
}
Definite or Indefinite?
15
A Loop within a Loop…
// Generate a table of powers of the values 1 to 5
#include <iostream>
using namespace std;
int main()
{
float K,N;
N=1;
while (N < 3)
{
K=0;
while (K < 4)
{
cout <<K*N<<“ “;
K=K+1;
} // end of (K<4) loop
cout << endl;
N = N + 1;
} // end of (N<3) loop
return 0;
}
What is the
output?
4 cycles of inner loop
For every cycle of
Outer loop
16
An Execution Table (Trace)
N
N<3 K K<4
Output (K*N)
1
T
0123
0246
2
T
3
F
0
1
2
3
4
0
1
2
3
4
T
T
T
T
F
T
T
T
T
F
17
That’s a wrap !
What we learned today:
Summing and Counting Loops
Nested if-else
Nesting if-else in while
Loops within Loops
18
Go back home proud !
You’re a C++ Looping Guru !
19