"C ++ primer plus" program review questions and exercises (04)

Chapter 5 cycles and relational expressions

Review Questions

1. What is the difference between the inlet conditions and outlet conditions cycle is a cycle? Various C ++ circulation belong to which one of them?

 

2. If the following code fragment is part of an effective program, it will print what?

int i;
for (i = 0; i < 5; i++)
    cout << i;
    cout << endl;

 

3. If the following code fragment is part of an effective program, it will print what?

int j;
for(j = 0; j < 11; j += 3)
    cout << j;
cout << endl << j << endl;

 

4. If the following code fragment is part of an effective program, it will print what?

int j = 5;
while ( ++j < 9)
    cout << j++ << endl;

 

5, if the following code fragment is part of an effective program, it will print what?

int j = 8;
do
    cout << " k = " << k << endl;
while (k++ < 5);

 

6, for the preparation of a printing 1,2,4,8,16,32,64 loop, each cycle will count variable is multiplied by two.

 

7. How to include multiple statements in the loop body?

 

8. The following statement is valid? If not, why? If so, it will complete what work?

int x = (1,024);

The following statement then?

int y; 
and = 1 , 024 ;

 

9. In terms of viewing input, cin >> ch with cin. Get (ch) and ch = cin. Get () What is the difference?

 

Programming Exercises

1. Preparation of a program requires the user to input two integers. The program calculates and outputs (including both integers) and all integers, the input is assumed here that the first lower integer between these two integers. For example, if the user input is 2 and 9, the procedure indicated all integers between 2 to 9 and 44.

 

2. The value of using array objects (instead of an array) and long double (rather than long long) to rewrite Listing 5.4, and calculate 100! Of.

 

3. Write a program that requires users to enter numbers. After each entry, the program will be reported so far, and all the accumulated input. When the user inputs 0, the routine ends.

 

4. Daphne 10% simple interest invested $ 100. That is, each year the profit is 10% of the investment, or $ 10 per year:

  Interest = 0.10X original deposit

And Cleo 5% compounded invested $ 100. In other words, the current deposit interest (including interest earned) 5%:

  = 005 × interest when the deposit

Cleo invest $ 100 profit in the first year is 5% - received $ 105, next year's profit was $ 105 5%, or $ 5.25, and so on. Write a program to calculate how many years, Cleo investment value to exceed the investment value of Daphne, and displays the value of the investment at this time two people.

 

5. Suppose you want to sell "C ++ For Fools" a book. Write a program, enter sales (number of books, rather than sales) in each month of the year, the program by recycling, using initialized to char * array (or string array object) month string of monthly prompt and the input data stored in an int array. Then, the program the total number of each element in the array is calculated and reported this year's sales.

 

6. Complete the programming exercise 5, but this time using a two-dimensional array to store inputs - three years in monthly sales. The program will report sales and total sales per year for three years.

 

7. The design of a structure called car, use it to store the following information about the vehicle: the manufacturer (or an array of characters stored in the character string), the year of production (an integer). Write a program that asks the user how many cars. Then, the process used to create a new dynamic array by a corresponding number of car structures. Next, the program prompts the user for each vehicle manufacturer (may consist of multiple words) and year information. Note that this requires special care, because it alternately read values ​​and character strings (see Chapter 4). Finally, the program will display the contents of each structure, the operation of the program are as follows:

how many cars do you wish wo catalog? 2

Car #1:

Please enter the make: Hudson Hornet

Please enter the year: 1952

Car #2:

Please enter the make: Kaiser

Please enter the year: 1951

Here is your collection:

1952 Hudson Hornet

1951 Kaiser

 

8. Write a program. It uses a char array and a read word per cycle, until the user inputs done. Subsequently, the program states that the user inputs the number of words (not including done included). The following is the operation of the program

Enter words (to stop, type the word done):

anteater birthday category dumpster

envy finagle geometry done for sure

You entered a tatal of  7 words.

You should include the header file cstring in the program, and use the function strcmp () to compare test

 

9. Write a program described in a practice before the meet, but use the string object instead of an array of characters. Include the header string in the program, using relational operators test for comparison.

 

10. Write a nested loop program used, requires the user to enter a value indicating how many lines to be displayed. Then, the program will display the corresponding number of asterisks line, wherein the first row comprises an asterisk, the second row comprising two asterisks, and so on, the number of characters included in each line is equal to the number of lines designated by the user, the asterisk case would not be enough, in front of the asterisk followed by a period, the operation of the program are as follows:

Enter number of rows : 5

. . . .*

. . .**

. . ***

. ****

*****

Guess you like

Origin www.cnblogs.com/mrray1105/p/12003867.html