![]()
//ex02_08.cpp
// raise x to the y power
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int x, y, i, power;
i = 1;
power = 1;
cout << "Enter base as an
integer:
";
cin >> x;
cout << "Enter exponent as
an integer:
";
cin >> y;
while ( i <= y ) {
power *= x;
++i;
}
cout << power << endl;
return 0;
}
![]()
//ex02_15.cpp
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int y, x = 1, total = 0;
while ( x <= 10 ) {
y = x * x;
cout << y
<<
endl;
total += y;
++x;
}
cout << "Total is "
<< total
<< endl;
return 0;
}
![]()
//ex02_24.cpp
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int count = 1;
while ( count <= 10 ) {
cout <<
(count %
2 ? "****" : "++++++++")
<< endl;
++count;
}
return 0;
}
![]()
//ex02_25.cpp
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int row = 10, column;
while ( row >= 1 ) {
column = 1;
while ( column
<= 10
) {
cout
<< (row % 2 ? "<" : ">");
++column;
}
--row;
cout << endl;
}
return 0;
}
![]()
//ex02_42.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int x, y;
cout << "Enter two integers
in the
range 1-20: ";
cin >> x >> y;
for ( int i = 1; i <= y; i++ ) {
for ( int j =
1; j <=
x; j++ )
cout
<< '@';
cout <<
endl;
}
return 0;
}
![]()
// Fig. 2.7: fig02_07.cpp
// Class average program with counter-controlled
repetition
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int
total,
// sum of grades
gradeCounter, //
number of grades entered
grade,
// one grade
average;
// average of grades
// initialization phase
total =
0;
// clear total
gradeCounter =
1;
// prepare to loop
// processing phase
while ( gradeCounter <= 10 )
{
// loop 10 times
cout <<
"Enter grade:
"; // prompt for
input
cin >>
grade;
// input grade
total = total +
grade;
// add grade to total
gradeCounter =
gradeCounter
+ 1; // increment counter
}
// termination phase
average = total /
10;
// integer division
cout << "Class average is "
<<
average << endl;
return 0; // indicate
program
ended successfully
}
![]()
// Fig. 2.9: fig02_09.cpp
// Class average program with sentinel-controlled
repetition.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
#include <iomanip>
using std::setprecision;
using std::setiosflags;
int main()
{
int
total,
// sum of grades
gradeCounter, //
number of grades entered
grade;
// one grade
double average; //
number with
decimal point for average
// initialization phase
total = 0;
gradeCounter = 0;
// processing phase
cout << "Enter grade, -1 to
end: ";
cin >> grade;
while ( grade != -1 ) {
total = total +
grade;
gradeCounter =
gradeCounter
+ 1;
cout <<
"Enter grade,
-1 to end: ";
cin >> grade;
}
// termination phase
if ( gradeCounter != 0 ) {
average =
static_cast<
double >( total ) / gradeCounter;
cout <<
"Class average
is " << setprecision( 2 )
<< setiosflags( ios::fixed | ios::showpoint )
<< average << endl;
}
else
cout << "No
grades
were entered" << endl;
return 0; // indicate
program
ended successfully
}
![]()
// Fig. 2.11: fig02_11.cpp
// Analysis of examination results
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
// initialize variables in
declarations
int passes =
0,
// number of passes
failures =
0,
// number of failures
studentCounter =
1, // student counter
result;
// one exam result
// process 10 students;
counter-controlled
loop
while ( studentCounter <= 10 ) {
cout <<
"Enter result
(1=pass,2=fail): ";
cin >>
result;
if ( result ==
1 )
// if/else nested in while
passes
= passes + 1;
else
failures
= failures + 1;
studentCounter
= studentCounter
+ 1;
}
// termination phase
cout << "Passed " <<
passes
<< endl;
cout << "Failed " <<
failures
<< endl;
if ( passes > 8 )
cout <<
"Raise tuition
" << endl;
return 0; //
successful termination
}
![]()
// Fig. 2.14: fig02_14.cpp
// Preincrementing and postincrementing
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int c;
c = 5;
cout << c <<
endl;
// print 5
cout << c++ <<
endl;
// print 5 then postincrement
cout << c << endl
<< endl;
// print 6
c = 5;
cout << c <<
endl;
// print 5
cout << ++c <<
endl;
// preincrement then print 6
cout << c <<
endl;
// print 6
return
0;
// successful termination
}
![]()
// Fig. 2.16: fig02_16.cpp
// Counter-controlled repetition
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int counter =
1;
// initialization
while ( counter <= 10 )
{
// repetition condition
cout <<
counter
<< endl;
++counter;
// increment
}
return 0;
}
![]()
// Fig. 2.17: fig02_17.cpp
// Counter-controlled repetition with the for
structure
#include <iostream>
using std::cout;
using std::endl;
int main()
{
// Initialization, repetition
condition,
and incrementing
// are all included in the for
structure
header.
for ( int counter = 1; counter
<= 10;
counter++ )
cout <<
counter
<< endl;
return 0;
}
![]()
// Fig. 2.20: fig02_20.cpp
// Summation with for
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int sum = 0;
for ( int number = 2; number
<= 100; number
+= 2 )
sum += number;
cout << "Sum is " << sum << endl;
return 0;
}
![]()
// Fig. 2.21: fig02_21.cpp
// Calculating compound interest
#include <iostream>
using std::cout;
using std::endl;
using std::ios;
#include <iomanip>
using std::setw;
using std::setiosflags;
using std::setprecision;
#include <cmath>
int main()
{
double
amount,
// amount on deposit
principal = 1000.0, // starting principal
rate = .05; //
interest
rate
cout << "Year" <<
setw( 21 )
<< "Amount
on deposit" << endl;
// set the floating-point number
format
cout << setiosflags(
ios::fixed |
ios::showpoint )
<< setprecision(
2 );
for ( int year = 1; year <=
10; year++
) {
amount = principal
* pow(
1.0 + rate, year );
cout <<
setw( 4
) << year << setw( 21 ) << amount << endl;
}
return 0;
}
![]()
// Fig. 2.22: fig02_22.cpp
// Counting letter grades
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int
grade,
// one grade
aCount =
0,
// number of A's
bCount =
0,
// number of B's
cCount =
0,
// number of C's
dCount =
0,
// number of D's
fCount =
0;
// number of F's
cout << "Enter the letter
grades."
<< endl
<< "Enter
the EOF character to end input." << endl;
while ( ( grade = cin.get() ) != EOF ) {
switch ( grade ) { // switch nested in while
case
'A': // grade was uppercase A
case
'a': // or lowercase a
++aCount;
break; // necessary to exit switch
case
'B': // grade was uppercase B
case
'b': // or lowercase b
++bCount;
break;
case
'C': // grade was uppercase C
case
'c': // or lowercase c
++cCount;
break;
case
'D': // grade was uppercase D
case
'd': // or lowercase d
++dCount;
break;
case
'F': // grade was uppercase F
case
'f': // or lowercase f
++fCount;
break;
case
'\n': // ignore newlines,
case
'\t': // tabs,
case
' ': // and spaces in input
break;
default:
// catch all other characters
cout << "Incorrect letter grade entered."
<< " Enter a new grade." << endl;
break; // optional
}
}
cout << "\n\nTotals for
each letter
grade are:"
<< "\nA:
" << aCount
<< "\nB:
" << bCount
<< "\nC:
" << cCount
<< "\nD:
" << dCount
<< "\nF:
" << fCount << endl;
return 0;
}
![]()
// Fig. 2.24: fig02_24.cpp
// Using the do/while repetition structure
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int counter = 1;
do {
cout <<
counter
<< " ";
} while ( ++counter <= 10 );
cout << endl;
return 0;
}
![]()
// Fig. 2.26: fig02_26.cpp
// Using the break statement in a for structure
#include <iostream>
using std::cout;
using std::endl;
int main()
{
// x declared here so it can be used
after
the loop
int x;
for ( x = 1; x <= 10; x++ ) {
if ( x == 5 )
break;
// break loop only if x is 5
cout << x
<<
" ";
}
cout << "\nBroke out of
loop at x of
" << x << endl;
return 0;
}
![]()
// Fig. 2.27: fig02_07.cpp
// Using the continue statement in a for structure
#include <iostream>
using std::cout;
using std::endl;
int main()
{
for ( int x = 1; x <= 10; x++ ) {
if ( x == 5 )
continue;
// skip remaining code in loop
// only if x is 5
cout << x
<<
" ";
}
cout << "\nUsed continue to
skip printing
the value 5"
<<
endl;
return 0;
}