"C++primer" V5 the 1th chapter began reading notes exercise answer

Source: Internet
Author: User

From today on the blog to write C++primer words. Mainly in the back of the exercise-oriented, will have the necessary knowledge points to supplement.

I am also a rookie, may have the wrong place, also hope to point out.

Pre-content may be compared to water.

1.1 Slightly

1.2 Slightly

1.3

CIN and cout are the objects of IStream and Ostream respectively.

#include <iostream>usingnamespace  std; int Main () {    cout<<"hello,world"<<Endl;     return 0 ;}

1.4

#include <iostream>usingnamespace  std; int Main () {    int A, b;    CIN>>a>>b;    cout<<a*b<<Endl;     return 0 ;}

1.5

#include <iostream>usingnamespace  std; int Main () {    int A, b;    CIN>>a>>b;    cout<<a<<endl<<b<<Endl;     return 0 ;}

1.6

This code is of course not correct. >>, << itself is a shift operator, and is overloaded before it is used by CIN and cout. “;” Indicates the end of the statement, so the following lines of code 7th and 8,<< missing the object that called it, the wording here does not conform to the use of the shift operator, so there will be an error .

1#include <iostream>2 using namespacestd;3 intMain ()4 {5     intv1=1, v2=2;6cout<<"The sum of"<<v1;7<<" and"<<v2;8<<" is"<<v1+v2<<Endl;9     return 0;Ten}

The correct notation is to remove the semicolon from lines 6th and 7.

#include <iostream>using namespacestd;intMain () {intv1=1, v2=2; cout<<"The sum of"<<v1<<" and"<<v2<<" is"<<v1+v2<<Endl; return 0;}

1.7 slightly

1.8

/*/Such annotations cannot be nested;

Will comment out a whole line from the time it appears

If it is within double quotes, both of these annotation functions are invalidated.

#include <iostream>using namespacestd;intMain () {cout<<"/*"<<endl;//Output/*cout<<"*/"<<endl;//Output * /cout<</* "*/"*/<<endl;//Compilation Errorcout<</* "*/"/* "/*" */<<endl;//Output/* with 1th    return 0;}

1.9

#include <iostream>usingnamespace  std; int Main () {    int i=, sum=0;      while (i<=)    {        sum+ =i        ; + +i;    }    cout<<sum<<Endl;     return 0 ;}

1.10

#include <iostream>usingnamespace  std; int Main () {    int i=;      while (i>=0)    {        cout<<i<<Endl;         --I    ;    } return 0 ;}

1.11

#include <iostream>usingnamespace  std; int Main () {    int  beg,end;    CIN>>beg>>end;      for (int i=beg;i<=end;++i)        cout<<i<<Endl;     return 0 ;}

1.12

Add the number between [-100,100] and the end result is 0.

1.13 slightly

1.14

For when the starting and ending points of the loop are relatively clear

While is useful when termination conditions are more complex

1.15 slightly

1.16

#include <iostream>usingnamespace  std; int Main () {    int val,sum=0;      while (Cin>>val)        sum+ =val;    cout<<sum<<Endl;     return 0 ;}

1.17

If all are equal: Number occurs n times

If not duplicated: Each number outputs AI occurs 1 times

1.18 slightly

1.19

The following code ignores the effect of the input order.

#include <iostream>using namespacestd;intMain () {intBeg,end; CIN>>beg>>end; if(beg>end) {        inttemp=Beg; Beg=end; End=temp; }     for(inti=beg;i<=end;++i) cout<<i<<Endl; return 0;}

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.