Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
#include <iostream.h>
void main(void)
{
int bigVal = 10, smallVal = 1;
cout << "bigVal is " << bigVal
<< " and smallVal is " << smallVal << endl;
smallVal++;
bigVal--;
Program continues
Program Output
Program 5-2
//This program demonstrates the prefix and postfix modes of the
// increment and decrement operators.
#include <iostream.h>
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
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;
}
a = 2;
b = 5;
c = a * b++;
cout << a <<
x = 10;
if ( x++ > 10)
cout << x is greater than 10.\n;
<< b <<
<< c;
Results: 2 6 10
Program 5-3
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;
}
while (expression)
statement;
9
10
Terminating a Loop
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]
12
5.3 Counters
13
14
Program 5-4
// This program displays the numbers 1 through 10 and
// their squares.
#include <iostream.h>
void main(void)
{
int num = 1;
Program Output
number
number Squared
------------------------1
1
2
4
3
9
4
16
5
25
6
36
7
49
8
64
9
81
10
100
// Initialize counter
15
16
Program 5-5
// This program displays the numbers 1 through 10
and
// their squares.
#include <iostream.h>
Program Output
number
number Squared
------------------------1
1
2
4
3
9
4
16
5
25
6
36
7
49
8
64
9
81
10
100
void main(void)
{
int num = 0;
cout << "number
number Squared\n";
cout << "-------------------------\n";
while (num++ < 10)
cout << num << "\t\t" << (num * num) <<
endl;
}
17
18
Program 5-6
5.4 Letting the User Control the Loop
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);
19
Program continues
20
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: 75 80 82 [Enter]
The average is 79.
Student 2: 85 85 90 [Enter]
The average is 86.67.
Student 3: 60 75 88 [Enter]
The average is 74.33.
21
22
Program 5-7
5.5 Keeping a Running Total
void main(void)
{
int days, count = 0;
float total = 0.0;
cout << "For how many days do you have sales figures? ";
cin >> days;
23
Program continues
24
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;
25
26
Program 5-8
5.6 Sentinels
#include <iostream.h>
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";
27
Program continues
28
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
29
30
Program 5-9
statement;
while (expression);
31
32
33
34
Program 5-11
// This program displays the numbers 1 through 10 and
// their squares.
#include <iostream.h>
Program Output
Number
Number Squared
------------------------1
1
2
4
3
9
4
16
5
25
6
36
7
49
8
64
9
81
10
100
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;
}
35
36
int num = 1;
for ( ; num <= 10; num++)
cout << num << \t\t << (num * num) << endl;
37
38
Program 5-12
// This program takes daily sales figures for one week
// and calculates their total.
#include <iostream.h>
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
39
}
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);
cout << "The total sales are $" << total << endl;
}
40
10
Going backwards:
for(number = 10; number >= 0; number--)
cout << number << endl; //count from 10 to 0
42
44
11
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 <iostream.h>
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
45
46
48
12
Program 5-14
// This program raises the user's number to the powers
// of 0 through 10.
#include <iostream.h>
#include <math.h>
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
49
50
Program Output
Enter a number: 2 [Enter]
This program will raise 2 to the powers of 0 through 10.
2 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]
51
13