Chapter Review

1

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 * pd = &ted;
cout << *pd << "\n";

12

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

13

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

unsigned int size;
cout << "Enter a positive integer: ";
cin >> size;
int * dyn = new int [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. The cout object interprets the address of a char as an invitation to print a string, but the type cast (int *) converts the address to type pointer-to-int, which is then printed as an address. In short, the statement prints the address of the string, assuming the int type is wide enough to hold an address.

15

struct fish
{
    char kind[20];
    int weight;
    float length;
};

fish * pole = new fish;
cout << "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 will skip over the newline following the numeric input, avoiding that problem. On the other hand, it will read just a single word, not an entire line.

17

#include <string>
#include <vector>
#include <array>
const int Str_num {10}; // or = 10
...
std::vector<std::string> vstr(Str_num);
std::array<std::string, Str_num> astr;

Programming Exercises

1

#include <iostream>

int main()
{
    using namespace std;

    cout << "What is your first name? ";
    char first_name[20];
    cin.getline(first_name, 20);

    cout << "What is your last name? ";
    char last_name[20];
    cin.getline(last_name, 20);

    cout << "What letter grade do you deserve? ";
    char grade;
    cin.get(grade);

    cout << "What is your age? ";
    int age;
    cin >> age;

    cout << "Name: " << last_name << ", " << first_name << endl
         << "Grade: " << grade << "\n"
         << "Age: " << age << endl;

    return 0;
}

2

#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string name;
    string dessert;

    cout << "Enter your name: \n";
    getline(cin, name);
    cout << "Enter your favorite dessert: \n";
    getline(cin, dessert);
    cout << "I have some delicious " << dessert;
    cout << " for you, " << name << ".\n";

    return 0;
}

3

#include <iostream>
#include <cstring>

int main()
{
    using namespace std;

    char first_name[20], last_name[20];
    char combined[40];

    cout << "Enter your first name: ";
    cin >> first_name;
    cout << "Enter your favorite last name: ";
    cin >> last_name;

    strcpy(combined, last_name);
    strcat(combined, ", ");
    strcat(combined, first_name);
    cout << "Here's the information in a single string: " << combined << endl;

    return 0;
}

4

#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string first_name, last_name, combined;

    cout << "Enter your first name: ";
    cin >> first_name;
    cout << "Enter your favorite last name: ";
    cin >> last_name;

    combined = last_name + ", " + first_name;
    cout << "Here's the information in a single string: " << combined << endl;

    return 0;
}

5

#include <iostream>
#include <string>

struct CandyBar
{
    std::string brand;
    float weight;
    int calories;
};

int main()
{
    using namespace std;

    CandyBar snack = {"Mocha Munch", 2.3, 350};

    cout << "Brand: " << snack.brand << "\nWeight: " << snack.weight
         << "\nCalories = " << snack.calories << endl;

    return 0;
}

6

#include <iostream>
#include <string>

struct CandyBar
{
    std::string brand;
    float weight;
    int cal;
};

int main()
{
    using namespace std;

    CandyBar snack[3] = {{"Mocha Munch", 2.3, 350}, {"Wei Long", 0.5, 222}, {"crisps", 1.0, 500}};

    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 namespace std;

struct Pizza
{
    string company;
    float diameter;
    float weight;
};

int main()
{
    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 namespace std;

struct Pizza
{
    string company;
    float diameter;
    float weight;
};

int main()
{
    Pizza * piz = new Pizza;

    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>

struct CandyBar
{
    std::string brand;
    float weight;
    int cal;
};

int main()
{
    using namespace std;

    CandyBar * snack = new CandyBar[3];
    snack[0] = {"Mocha Munch", 2.3, 350};
    snack[1] = {"Wei Long", 0.5, 222};
    snack[2] = {"crisps", 1.0, 500};

    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()
{
    using namespace std;

    cout << "Enter your three 40-yard runnings' scores: ";
    array<float, 3> scores;
    cin >> scores[0] >> scores[1] >> scores[2];
    cout << "Average: " << (scores[0] + scores[1] + scores[2]) / 3.0 << endl;

    return 0;
}

c++-pimer-plus-6th-chapter04的更多相关文章

  1. The 6th tip of DB Query Analyzer

      The 6th tip of DB Query Analyzer MA Gen feng (Guangdong Unitoll Services incorporated, Guangzhou ...

  2. [转载]ECMA-262 6th Edition / Draft August 24, 2014 Draft ECMAScript Language Specification

    http://people.mozilla.org/~jorendorff/es6-draft.html#sec-23.4 Draft Report Errors and Issues at: htt ...

  3. Chapter04 运算符(Operator)

    Chapter04 运算符 目录 Chapter04 运算符 4.1 算数运算符 4.2 关系运算符 4.3 逻辑运算符 4.4 赋值运算符 4.5 三元运算符 4.6 运算符的优先级 4.7 标识符 ...

  4. ​Si2151/41 6th Generation Silicon TV Tuner ICs

    ​ The Si2151/41 are the industry's most advanced silicon TV tuner ICs supporting all worldwide terre ...

  5. Codeforces Round #361 Jul.6th B题 ☺译

    最近迈克忙着考前复习,他希望通过出门浮躁来冷静一下.迈克所在的城市包含N个可以浮躁的地方,分别编号为1..N.通常迈克在家也很浮躁,所以说他家属于可以浮躁的地方并且编号为1.迈克从家出发,去一些可以浮 ...

  6. Codeforces Round #361 Jul.6th A题 ☺译

    A.迈克和手机 当迈克在沙滩上游泳的时候,他意外的把他的手机扔进了水里.不过你甭担心因为他立马买了个便宜些的代替品,这个代替品是老款九键键盘,这个键盘只有十个等大的数字按键,按以下方式排列: 1 2 ...

  7. October 6th 2016 Week 41st Thursday

    The outer world you see is a reflection of your inner self. 你拥有什么样的内心,你就会看到什么样的世界. And we eventually ...

  8. September 6th 2016 Week 37th Tuesday

    I only wish to face the sea, with spring flowers blossoming. 我只愿面朝大海,春暖花开. That scenery is beautiful ...

  9. July 6th, Week 28th Wednesday, 2016

    Diligence is the mother of good fortune. 勤勉是好运之母. The mother of good fortune can be diligence, conti ...

  10. August 6th, 2016, Week 32nd, Saturday

    It is not the mountain we conquer, but ourselvess. 我们征服的不是高山,而是我们自己. Difficulties and obstacles, jus ...

随机推荐

  1. topcoder srm 693 div1 -3

    1.给出一个$n$个顶点的无向带权图.其中顶点$i,i+1$之间存在边,$i,i+2$之间存在边.而且仅有这些边.现在删掉其中的一些边,剩下的边满足图仍然是2联通的情况下使得权值和最小? 思路:其实就 ...

  2. ODAC(V9.5.15) 学习笔记(三)TOraSession(2)

    2. 事务相关 名称 类型 说明 AutoCommit Boolean 是否自动提交事务 注意:只有当TOraSession和TOraQuery的AutoCommit都为True时才对每个数据库操作自 ...

  3. eMMC分区详解【转】

    本文转载自:https://blog.csdn.net/wxh0000mm/article/details/77864002 转自:http://blog.csdn.net/junzhang1122/ ...

  4. 转载:Systemd 命令

    目录 一.由来 二.Systemd 概述 三.系统管理 3.1 systemctl 3.2 systemd-analyze 3.3 hostnamectl 3.4 localectl 3.5 time ...

  5. 古堡算式|2012年蓝桥杯B组题解析第二题-fishers

    (4')古堡算式 福尔摩斯到某古堡探险,看到门上写着一个奇怪的算式: ABCDE * ? = EDCBA 他对华生说:"ABCDE应该代表不同的数字,问号也代表某个数字!" 华生: ...

  6. python 之 文件I/0

    打开和关闭文件 open()函数 必须要open()内置函数打开一个文件,创建一个file对象,相关的方法才可以调用它进行读写. 语法 file object=open(file_name [,acc ...

  7. Asp.net 之 window 操作命令

    命令:cmd  打开执行窗口 命令:inetmgr.打开iis管理器 命令:dcomcnfg 打开组件服务 命令:regedit   打开注册表

  8. mysql中if()函数使用

    博主原创,转载请注明出处: 在mysql中if()函数的用法类似于java中的三目表达式,其用处也比较多,具体语法如下: IF(expr1,expr2,expr3),如果expr1的值为true,则返 ...

  9. CentOS7时间和日期的同步 (chrony和)

    CentOS 6版本,使用 hwclock CentOS 7版本,使用timedatectl 1.基本概念 1.1 GMT,UTC,CST,DST时间 世界标准时间 整个地球分为二十四时区,每个时区都 ...

  10. 理解ffmpeg中的pts,dts,time_base

    首先介绍下概念: PTS:Presentation Time Stamp.PTS主要用于度量解码后的视频帧什么时候被显示出来 DTS:Decode Time Stamp.DTS主要是标识读入内存中的b ...