< previous page page_318 next page >

Page 318
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
i = 1;
while (i <= 5)
{
    sum = 0;
    j = 1;
    while (j <= i)
    {
        sum = sum + j;
        j++;
    }
    cout << sum <<  ;
    i++;
}
Programming Warm-Up Exercises
1. Write a program segment that sets a Boolean variable dangerous to TRUE and stops reading in data if pressure (a float variable being read in) exceeds 510.0. Use dangerous as a flag to control the loop.
2. Write a program segment that counts the number of times the integer 28 occurs in a file of 100 integers.
3. Write a nested loop code segment that produces this output:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
1
1 2
1 2 3
1 2 3 4
4. Write a program segment that reads a file of student scores for a class (any size) and finds the class average.
5. a. Write a statement that prints the following headings in the format shown.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
         Sales
Week1    Week2    Week3
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
b. Write a statement that lines values up under each week's heading. The values are stored in the int variables week1, week2, and week3. The last digit of each number should fall under the 1, 2, or 3 of its column heading.
6. Write a program segment that reads in integers and then counts and prints out the number of positive integers and the number of negative integers. If a value is zero, it should not be counted. The process should continue until end-of-file occurs.
7. Write a program segment that adds up the even integers from 16 through 26, inclusive.
8. Rewrite program TempStat (page 296) using a different initialization scheme. One temperature should be read before the loop, and all values (except hour) should be initialized to the first temperature. Trace your program to make sure it works.

 
< previous page page_318 next page >