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

Source: Internet
Author: User

Chapter Review1

A.char actors[30];

B.short betsie[100];

C.float chunk[13];

D.long double dipsea[64];

2

A.array<char, 30> actors;

B.array<short, 100> betsie;

C.array<float, 13> chunk;

D.array<long double, 64> dipsea;

3

int oddly[5] = {1, 3, 5, 7, 9};

4

int even = oddly[0] + oddly[4];

5

cout << ideas[1] << "\n"; // or << endl;

6

char lunch[13] = "cheeseburger"; // number of characters + 1
Or
char lunch[] = "cheeseburger"; // let the compiler count elements

7

string lunch = "Waldorf Salad";
Or, if you don't have a using directive,
std::string lunch = "Waldorf Salad";

8
struct fish{    char kind[20];    int weight;    float length;};
9
fish petes = {    "trout",    12,    26.66}
10

enum Response {No, Yes, Maybe};

11
double"\n";
12
float// or = &treacle[0]cout << pf[0" " << pf[9"\n";// or use *pf and *(pf + 9)
13

This assumes the and iostream vector header files has been included and that there are a using directive:

unsignedint"Enter a positive integer: ";cin >> size;intnewint [size];vector<int> dv(size);
14

Yes, it is valid. The expression "Home of the Jolly bytes" is a string constant; Hence it evaluates as the address of the beginning of the string. coutthe object interprets the address of a char invitation to print a string, but the type cast (int *) converts th e address to type pointer-to- int , which are then printed as an address. In short, the statement prints the address of the "the" string, assuming the int type is wide enough "to" an address.

15
struct fish{    char kind[20];    int weight;    floatnew"Enter kind of fish: ";cin >> pole -> kind;
16

Using cin >> address causes a program-to-skip over whitespace until it finds non-whitespace. It then reads characters until it encounters whitespace again. Thus, it'll skip over the newline following the numeric input, avoiding that problem. On the other hand, it would read just a single word, not a entire line.

17
#include <string>#include <vector>#include <array>constint Str_num {10// or = 10...std::vector<std::string> vstr(Str_num);std::array<std::string, Str_num> astr;
Programming Exercises1
#include <iostream>intMain () {using namespaceStd cout <<"What's your first name?";Charfirst_name[ -]; Cin.getline (first_name, -); cout <<"What's your last name?";Charlast_name[ -]; Cin.getline (last_name, -); cout <<"What letter grade does you deserve?";CharGrade        Cin.get (grade); cout <<"What's Your age?";intAge        CIN >> age; cout <<"Name:"<< last_name <<", "<< first_name << Endl <<"Grade:"<< Grade <<"\ n"<<"Age:"<< age << Endl;return 0;}
2
#include <iostream>#include <string>int main(){    usingnamespace std;        string name;    string dessert;        "Enter your name: \n";    getline(cin, name);    "Enter your favorite dessert: \n";    getline(cin, dessert);    "I have some delicious " << dessert;    " for you, "".\n";        return0;}
3
#include <iostream>#include <cstring>int main(){    usingnamespace std;        char first_name[20], last_name[20];    char combined[40];        "Enter your first name: ";    cin >> first_name;    "Enter your favorite last name: ";    cin >> last_name;        strcpy(combined, last_name);    ", ");    strcat(combined, first_name);    "Here's the information in a single string: " << combined << endl;        return0;}
4
#include <iostream>#include <string>int main(){    usingnamespace std;        string first_name, last_name, combined;        "Enter your first name: ";    cin >> first_name;    "Enter your favorite last name: ";    cin >> last_name;        ", " + first_name;    "Here's the information in a single string: " << combined << endl;        return0;}
5
#include <iostream>#include <string>struct CandyBar{    std::string brand;    float weight;    int calories;};int main(){    usingnamespace std;        CandyBar snack = {"Mocha Munch"2.3350};        "Brand: ""\nWeight: " << snack.weight         "\nCalories = " << snack.calories << endl;        return0;}
6
#include <iostream>#include <string>structcandybar{std::string brand;floatWeightintCal;};intMain () {using namespaceStd CandyBar snack[3] = {{"Mocha Munch",2.3, -}, {"Wei Long",0.5,222}, {"Crisps",1.0, -}}; cout <<"Brand:"<< snack[0].brand << Endl <<"Weight:"<< snack[0].weight << Endl <<"Calories:"<< snack[0].cal << Endl << Endl; cout <<"Brand:"<< snack[1].brand << Endl <<"Weight:"<< snack[1].weight << Endl <<"Calories:"<< snack[1].cal << Endl << Endl; cout <<"Brand:"<< snack[2].brand << Endl <<"Weight:"<< snack[2].weight << Endl <<"Calories:"<< snack[2].cal << Endl << Endl;return 0;}
7
#include <iostream>#include <string>using namespaceStdstructpizza{String Company;floatDiameterfloatWeight;};intMain () {Pizza Piz; cout <<"Enter The pizza's company:";    Getline (CIN, Piz.company); cout <<"Enter The pizza ' s diameter (in CM):";    Cin >> Piz.diameter; cout <<"Enter The pizza ' s weight (in Kg):";        Cin >> Piz.weight; cout <<"Company:"<< Piz.company << Endl; cout <<"Diameter:"<< Piz.diameter <<"CM"<< Endl; cout <<"Weight:"<< Piz.weight <<"Kg"<< Endl;return 0;}
8
#include <iostream>#include <string>using namespaceStdstructpizza{String Company;floatDiameterfloatWeight;};intMain () {Pizza * Piz =NewPizza; cout <<"Enter The pizza's company:";    Getline (CIN, Piz-Company); cout <<"Enter The pizza ' s diameter (in CM):";    CIN >> Piz-diameter; cout <<"Enter The pizza ' s weight (in Kg):";        CIN >> Piz-weight; cout <<"Company:"<< Piz company << Endl; cout <<"Diameter:"<< Piz, diameter <<"CM"<< Endl; cout <<"Weight:"<< Piz, Weight <<"Kg"<< Endl;return 0;}
9
#include <iostream>#include <string>structcandybar{std::string brand;floatWeightintCal;};intMain () {using namespaceStd CandyBar * Snack =Newcandybar[3]; snack[0] = {"Mocha Munch",2.3, -}; snack[1] = {"Wei Long",0.5,222}; snack[2] = {"Crisps",1.0, -}; cout <<"Brand:"<< snack[0].brand << Endl <<"Weight:"<< snack[0].weight << Endl <<"Calories:"<< snack[0].cal << Endl << Endl; cout <<"Brand:"<< snack[1].brand << Endl <<"Weight:"<< snack[1].weight << Endl <<"Calories:"<< snack[1].cal << Endl << Endl; cout <<"Brand:"<< snack[2].brand << Endl <<"Weight:"<< snack[2].weight << Endl <<"Calories:"<< snack[2].cal << Endl << Endl;Delete[] snack;return 0;}
10
#include <iostream>#include <array>int main(){    usingnamespace std;        "Enter your three 40-yard runnings' scores: ";    array<float3> scores;    cin >> scores[0] >> scores[1] >> scores[2];    "Average: " << (scores[0] + scores[1] + scores[23.0 << endl;        return0;}

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

Related Article

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.