"C + + Primer plus English version Sixth edition" Chapter 5

Source: Internet
Author: User
Tags integer numbers

Chapter Review

1
An entry-condition loop evaluates a test expression before entering the body of the loop. If the condition is initially false , the loop never executes its body. An exit-condition loop evaluates a test expression after processing the body of the loop. Thus, the loop body is executed once, even if the test expression is initially false . for while The and loops are entry-condition loops, and the do while loop was an exit-condition loop.
2
It would print the following:

01234    

Not, that is cout << endl; , the loop body (because there is no braces).

3
It would print the following:

0369    12    

4
It would print the following:

6    8    

5
It would print the following:

8;

6
It ' s simplest to use the *= operator:

for  (int  num = 1 ; Num <= 64 ; Num *= 2 ) cout << num << ;  

7
You enclose the statements within paired braces to form a single compound statement, or block.
8
Yes, the first statement is valid. The expression 1,024 consists of the expressions-1 and 024-joined by a comma operator. The value of the right-hand expression. This is 024, which are octal for a, so the declaration assigns the value 20 to x . The second statement is also valid. However, operator precedence causes it to be evaluated as follows:
(y = 1), 024;
That's, the left expression sets y 1 to, and the value of the entire expression, which isn't used, is 024 , or c7/>.
9
The cin >> ch form skips over spaces, newlines, and tabs when it encounters them. The other and the forms read those characters.

Programming Exercises1
#include <iostream>int main(){    usingnamespace std;        int n1, n2;    int0;        "Input two integer numbers (example: 2 9): ";    cin >> n1;    cin >> n2;        for (int i = n1; i <= n2; ++i)    {        sum += i;    }    "sum = " << sum << endl;        return0;}
2
#include <iostream>#include <array>Const intArsize =101;intMain () {using namespaceStd array<Long Double, arsize> factorials; factorials[1] = factorials[0] =1.0L; for(inti =2; i < arsize; ++i) {Factorials[i] = i * factorials[i-1]; } for(inti =0; i < arsize; ++i) {cout << i <<"! = "<< Factorials[i] << Endl; }return 0;}
3
#include <iostream>int main(){    usingnamespace std;        double x;    double0.0;        cin >> x;    while0.0)    {        sum += x;        cin >> x;    }    "sum = " << sum << endl;        return0;}
4
#include <iostream>int main(){    usingnamespace std;        double d, c;    100.0;       int i;    for0; d >= c; ++i)    {        0.1100.0;        1.05;    }        " year(s)\n";    "Daphne: " << d << endl;    "Cleo: " << c << endl;        return0;}
5
#include <iostream>Const Char*Constmonths[ A] ={"January","February","March","April","may","June","July","August","September","October","November","December"};intMain () {using namespaceStdintvolumes[ A];intsum =0; for(inti =0; I < A; ++i) {cout <<"Enter The sales volume of"<< Months[i] <<": ";    CIN >> Volumes[i]; } for(inti =0; I < A;        ++i) sum + = Volumes[i]; cout <<"Sum ="<< sum << Endl;return 0;}
6
#include <iostream>Const Char*Constmonths[ A] ={"January","February","March","April","may","June","July","August","September","October","November","December"};intMain () {using namespaceStdintvolumes[3][ A];intsum =0;intTotal =0; for(inti =0; I <3; ++i) {cout <<"Enter the sales volumes of year:"<< i +1<< Endl << Endl; for(intj =0; J < A; ++J) {cout <<"Enter The sales volumes of"<< Months[j] <<": ";        CIN >> Volumes[i][j]; }            } for(inti =0; I <3; ++i) { for(intj =0; J < A;        ++J) {sum + = volumes[i][j]; } cout <<"Sales volume of Year"<< i +1<<"is"<< sum << Endl;        Total + = sum; sum =0; } cout <<"Sales volumes of 3 years is:"<< Total << Endl;return 0;}
7
#include <iostream>structcar{Charmake[ +];intyear;};intMain () {using namespaceStdintNum        Car * cars; cout <<"How many cars does you wish to catalog:";    CIN >> Num; Cars =NewCar[num]; for(inti =0; i < num; i++) {cout <<"Car #"<< i +1<<":\ n"; cout <<"Please enter the Make:";//cin >> cars[i].make;Cin.getline (Cars[i].make, +);        Cin.get (); cout <<"Please enter the year made:";    Cin >> Cars[i].year; } cout <<"Here is your collection:\ n"; for(inti =0; i < num; ++i) cout << cars[i].year <<" "<< Cars[i].make << Endl;Delete[] cars;return 0;}
8
#include <iostream>#include <cstring>int main(){    usingnamespace std;        char word[20];    int0;        "Enter word (to stop, type the word done):\n";    cin >> word;    while"done"))    {        ++count;        cin >> word;    }    "You entered a total of "" words.\n";        return0;}
9
#include <iostream>#include <string>int main(){    usingnamespace std;        string word;    int0;        "Enter word (to stop, type the word done):\n";    cin >> word;    while"done")    {        ++count;        cin >> word;    }    "You entered a total of "" words.\n";        return0;}
10
 #include <iostream>  int  Main () {using  namespace  std;    cout <<  "Enter number of rows:" ;    int  N;    CIN >> N; for  (int  i = 0 ; i < n; ++i)    //row  {for  (int  j = 0 ; j < n; ++j) //column  if  (J < (n-(i + 1 )))            cout << ;                else  cout << ;    cout << Endl; } return  0 ;}  

C + + Primer plus the sixth edition of the English version Chapter 5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.