c++-pimer-plus-6th-chapter05
Chapter Review
1
An entry-condition loop evaluates a test expression before entering the body of the loop. If the condition is initiallyfalse, 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 initiallyfalse. Theforandwhileloops are entry-condition loops, and thedo whileloop is an exit-condition loop.
2
It would print the following:
01234
    
Not that cout << endl; is not part of the loop body (because there are no braces).
3
It would print the following:
0369
12
    
4
It would print the following:
6
8
    
5
It would print the following:
k = 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 two expressions — 1 and 024 — joined by a comma operator. The value of the right-hand expression. This is 024, which is octal for 20, so the declaration assigns the value20tox. The second statement is also valid. However, operator precedence causes it to be evaluated as follows:
(y = 1), 024;
That is, the left expression setsyto1, and the value of the entire expression, which isn't used, is024, or20.
9
Thecin >> chform skips over spaces, newlines, and tabs when it encounters them. The other two forms read those characters.
Programming Exercises
1
#include <iostream>
int main()
{
    using namespace std;
    int n1, n2;
    int sum = 0;
    cout << "Input two integer numbers (example: 2 9): ";
    cin >> n1;
    cin >> n2;
    for (int i = n1; i <= n2; ++i)
    {
        sum += i;
    }
    cout << "sum = " << sum << endl;
    return 0;
}
2
#include <iostream>
#include <array>
const int ArSize = 101;
int main()
{
    using namespace std;
    array<long double, ArSize> factorials;
    factorials[1] = factorials[0] = 1.0L;
    for (int i = 2; i < ArSize; ++i)
    {
        factorials[i] = i * factorials[i - 1];
    }
    for (int i = 0; i < ArSize; ++i)
    {
        cout << i << "! = " << factorials[i] << endl;
    }
    return 0;
}
3
#include <iostream>
int main()
{
    using namespace std;
    double x;
    double sum = 0.0;
    cin >> x;
    while (x != 0.0)
    {
        sum += x;
        cin >> x;
    }
    cout << "sum = " << sum << endl;
    return 0;
}
4
#include <iostream>
int main()
{
    using namespace std;
    double d, c;
    d = c = 100.0;
    int i;
    for (i = 0; d >= c; ++i)
    {
        d += 0.1 * 100.0;
        c *= 1.05;
    }
    cout << i << " year(s)\n";
    cout << "Daphne: " << d << endl;
    cout << "Cleo: " << c << endl;
    return 0;
}
5
#include <iostream>
const char * const Months[12] =
{
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
};
int main()
{
    using namespace std;
    int volumes[12];
    int sum = 0;
    for (int i = 0; i < 12; ++i)
    {
        cout << "Enter the sales volume of " << Months[i] << ": ";
        cin >> volumes[i];
    }
    for (int i = 0; i < 12; ++i)
        sum += volumes[i];
    cout << "Sum = " << sum << endl;
    return 0;
}
6
#include <iostream>
const char * const Months[12] =
{
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
};
int main()
{
    using namespace std;
    int volumes[3][12];
    int sum = 0;
    int total = 0;
    for (int i = 0; i < 3; ++i)
    {
        cout << "Enter the sales volumes of year: " << i + 1 << endl << endl;
        for (int j = 0; j < 12; ++j)
        {
            cout << "Enter the sales volumes of " << Months[j] << ": ";
            cin >> volumes[i][j];
        }
    }
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 12; ++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 are: " << total << endl;
    return 0;
}
7
#include <iostream>
struct car
{
    char make[40];
    int year;
};
int main()
{
    using namespace std;
    int num;
    car * cars;
    cout << "How many cars do you wish to catalog: ";
    cin >> num;
    cars = new car[num];
    for (int i = 0; i < num; i++)
    {
        cout << "Car #" << i + 1 << ":\n";
        cout << "Please enter the make: ";
        //cin >> cars[i].make;
        cin.getline(cars[i].make, 40);
        cin.get();
        cout << "Please enter the year made: ";
        cin >> cars[i].year;
    }
    cout << "Here is your collection:\n";
    for (int i = 0; i < num; ++i)
        cout << cars[i].year << " " << cars[i].make << endl;
    delete [] cars;
    return 0;
}
8
#include <iostream>
#include <cstring>
int main()
{
    using namespace std;
    char word[20];
    int count = 0;
    cout << "Enter word (to stop, type the word done):\n";
    cin >> word;
    while (strcmp(word, "done"))
    {
        ++count;
        cin >> word;
    }
    cout << "You entered a total of " << count << " words.\n";
    return 0;
}
9
#include <iostream>
#include <string>
int main()
{
    using namespace std;
    string word;
    int count = 0;
    cout << "Enter word (to stop, type the word done):\n";
    cin >> word;
    while (word != "done")
    {
        ++count;
        cin >> word;
    }
    cout << "You entered a total of " << count << " words.\n";
    return 0;
}
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++-pimer-plus-6th-chapter05的更多相关文章
- The 6th tip of DB Query Analyzer
		
 The 6th tip of DB Query Analyzer MA Gen feng (Guangdong Unitoll Services incorporated, Guangzhou ...
 - [转载]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 ...
 - chapter05
		
/** * Created by EX-CHENZECHAO001 on 2018-03-29. */class Chapter05 { } // 类// 类中的字段自动带有getter方法和sett ...
 - Chapter05 流程控制(Process Control)
		
目录 Chapter05 流程控制 5.1 顺序控制 5.2 分支控制 if-else 单分支基本语法: 双分支基础语法: 多分支基础语法 5.3 嵌套分支 5.4 switch分支结构 5.5 Fo ...
 - 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 ...
 - Codeforces Round #361 Jul.6th B题 ☺译
		
最近迈克忙着考前复习,他希望通过出门浮躁来冷静一下.迈克所在的城市包含N个可以浮躁的地方,分别编号为1..N.通常迈克在家也很浮躁,所以说他家属于可以浮躁的地方并且编号为1.迈克从家出发,去一些可以浮 ...
 - Codeforces Round #361 Jul.6th A题 ☺译
		
A.迈克和手机 当迈克在沙滩上游泳的时候,他意外的把他的手机扔进了水里.不过你甭担心因为他立马买了个便宜些的代替品,这个代替品是老款九键键盘,这个键盘只有十个等大的数字按键,按以下方式排列: 1 2 ...
 - October 6th 2016 Week 41st Thursday
		
The outer world you see is a reflection of your inner self. 你拥有什么样的内心,你就会看到什么样的世界. And we eventually ...
 - September 6th 2016 Week 37th Tuesday
		
I only wish to face the sea, with spring flowers blossoming. 我只愿面朝大海,春暖花开. That scenery is beautiful ...
 - July 6th, Week 28th Wednesday, 2016
		
Diligence is the mother of good fortune. 勤勉是好运之母. The mother of good fortune can be diligence, conti ...
 
随机推荐
- cf水题
			
题意:输入多组数据,有的数据代表硬币的长宽,有的数据代表钱包的长宽,问你当这组数据代表钱包的长宽时,能不能把它前面出现的所有硬币全部装下. 思路:只要钱包的长宽大于前面出现的所有硬币的长宽就可以装下, ...
 - ODAC(V9.5.15) 学习笔记(四)TCustomDADataSet(4)
			
6.Options TCustomDADataSet的选择项为TDADataSetOptions,其成员介绍如下表 : 名称 类型 说明 TDADataSetOptions AutoPrepare B ...
 - Python常用库之functools
			
functools 是python2.5被引人的,一些工具函数放在此包里. python2.7中 python3.6中 import functools print(dir(functools)) [ ...
 - P3232 [HNOI2013]游走
			
吐槽 傻了傻了,对着题解改了好长时间最后发现是自己忘了调用高斯消元了... 思路 期望题,分配编号,显然编号大的分给贡献次数小的,所以需要知道每个边被经过次数的期望 然后边被经过的次数的期望就是连接的 ...
 - Unsupervised Image-to-Image Translation Networks --- Reading Writing
			
Unsupervised Image-to-Image Translation Networks --- Reading Writing 2017.03.03 Motivations: most ex ...
 - (转) The Incredible PyTorch
			
转自:https://github.com/ritchieng/the-incredible-pytorch The Incredible PyTorch What is this? This is ...
 - ToString()格式和用法大全,C#实现保留两位小数的方法
			
C,货币,2.5.ToString("C"),¥2.50.D,十进制数,25.ToString("D5"),00025.E,科学型,25000.ToString ...
 - SQL语句总结2018-11-7
			
增加一条数据 insert into table (列字段1,列字段2)values(列1值,列2值) 删除一条数据 delete from table where 列名1=值1 修改一条数据 upd ...
 - 【转载】ASP.NET页面之间传值的方式之QueryString(个人整理)
			
转自: https://www.cnblogs.com/kudsu/p/7694637.html QueryString Querystring也叫查询字符串,这种页面间传递数据是利用网页地址URL. ...
 - MS-Windows中的Git命令行
			
Git command line for MS-Windows Inhalt 1 Download and install, or copy the git command line suite fo ...