Chapter 4 expression 21-30 in the 5th after class exercise of C++ Primer

Exercise 4.21 write a program that uses conditional operators to find out which elements of the vector have odd values, and then double them.

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int temp{};
    vector<int> vec;
    while (cin >> temp)
    {
        vec.push_back(temp);
    }
    for (auto i : vec)
    {
        (i % 2) ? i *= 2 : i;
        cout << i <<endl;
    }
    return 0;
}

Exercise 4.22 the example program in this section divides the scores into high pass, pass and fail. Expand the program to further set the scores between 60 and 75 points to low pass. The program is required to have two versions: one using only conditional operators; the other using one or more if statements. Which version of the program is easier to understand? Why?

#include<iostream>
using namespace std;
int main()
{
    int temp{};
    while (cin >> temp)
    {
        cout << ((temp > 90) ? "hign pass"
            : (temp > 75) ? "pass" 
            : (temp > 60) ? "low pass" : "fail") << endl;
    }
    return 0;
}
#include<iostream>
using namespace std;
int main()
{
    int temp{};
    while (cin >> temp)
    {
        if (temp > 90)
            cout << "high pass";
        else if (temp > 75)
            cout << "pass";
        else if (temp > 60)
            cout << "low pass";
        else
            cout << "fail";
        cout << endl;
    }
    return 0;
}

Exercise 4.23 because of the priority of the operator, the following expression cannot be compiled. What are its problems according to the table in section 4.12? How should it be modified?

string s = "word";
string pl = s + s[s.size() - 1] == 's' ? "" : "s" ;

The addition operator takes precedence over the conditional operator. Therefore, it should be changed to:

string pl = s + (s[s.size() - 1] == 's' ? "" : "s") ;

Exercise 4.24 the example program in this section divides the scores into three types: high pass, pass, and fail, which is based on the condition that the operators satisfy the right combination law. If the conditional operator satisfies the left binding law, what will be the evaluation process?

finalgrade = (grade > 90) ? "high pass" : (grade < 60) ? "fail" : "pass";
finalgrade = ((grade > 90) ? "high pass" : (grade < 60)) ? "fail" : "pass";//Left associative

It is possible to use "high pass" as the judging condition of the next conditional expression, which may cause errors.

Exercise 4.25 if int occupies 32 bits and char occupies 8 bits on a machine, the Latin-1 character set is used, and the binary form of the character 'q' is 011110001, what is the value of the expression ~ 'q' < 6?

First, upgrade to int to get 00000000 00000000 01110001,

The final result is 00000000 00000000 00100011 10000000

Exercise 4.26 in the example of test scores in this section, what happens if you use unsigned int as the type of quiz1?

There should be no exception.

Since unsigned int only takes two bytes on some machines, if unsigned int is used as the type of quiz1, data loss may occur on some machines.

Exercise 4.27 what is the result of the following expression?

unsigned long ul1 = 3, ul2 = 7;
(a) ul1 & ul2 
(b) ul1 | ul2 
(c) ul1 && ul2
(d) ul1 || ul2 

a 3

b 7

c true

d true

Exercise 4.28 write a program to output the space occupied by each built-in type

#include<vector>
#include<iostream>
using namespace std;
int main()
{
	cout << "bool_size: " << sizeof(bool) << endl;//1
	cout << "char_size: " << sizeof(char) << endl;//1
	cout << "short_size: " << sizeof(short) << endl;//2
	cout << "int_size: " << sizeof(int) << endl;//4
	cout << "long_size: " << sizeof(long) << endl;//4
	cout << "long long_size: " << sizeof(long long) << endl;//8
	cout << "float_size: " << sizeof(float) << endl;//4
	cout << "double_size: " << sizeof(double) << endl;//8
	cout << "long double_size: " << sizeof(long double) << endl;//8
	return 0;
}

Exercise 4.29 infer the output of the following code and explain why. Is the result of running this program the same as you think? If not, why?

int x[10]; int *p = x;
cout << sizeof(x)/sizeof(*x) << endl;//10
cout << sizeof(p)/sizeof(*p) << endl;//1

As I imagined, the first sizeof gets the total size of the array 10 * 4, the second sizeof gets the size of the array element 4, and the result of dividing the two is 10; the third sizeof gets the size of the pointer p 4, and the fourth sizeof gets the size of the pointer p points to the element 4.

Exercise 4.30 according to the table in section 4.1.2, place parentheses in the appropriate places of the following expressions so that the expressions after parentheses have the same meaning as the previous expressions.

sizeof x+y			sizeof p->men[i]
sizeof a<b			sizeof f()
(sizeof x)+y			sizeof (p->men[i])
(sizeof a)<b			sizeof (f())
273 original articles published, 21 praised, 40000 visitors+
Private letter follow

Added by scheda on Wed, 15 Jan 2020 07:03:06 +0200