首页 > 代码库 > c++ primer 6.2.4节练习答案

c++ primer 6.2.4节练习答案

练习6.21

 1 int max(const int x, const int *y)
 2 {
 3     if (x > *y)
 4         return x;
 5     else
 6         return *y;
 7 }
 8 
 9 int main()
10 {
11     int a1, a2;
12     while (cin >> a1 >> a2)
13     {
14         cout << "the max is " << max(a1, &a2) << endl;
15     }
16     system("pause");
17     return 0;
18 }

const int *

练习6.22

 1 void change(int **x, int **y)
 2 {
 3     int *temp;
 4     temp = *y;
 5     *y = *x;
 6     *x = temp;
 7 }
 8 
 9 int main()
10 {
11     int a1 = 10;
12     int a2 = 20;
13     int *i = &a1;
14     int *j = &a2;
15     change(&i, &j);
16     cout << *i << " " << *j << endl;
17     system("pause");
18     return 0;
19 }

练习6.23

void print(int (&j)[2], int i)//int (&j)[2]
{
    for (i = 0; i != 2; ++i)
        cout <<*(j+i) << endl;
}

void print2(int *p, int i)
{
    for (i = 0; i != 2; ++i)
        cout << *(p+i) << endl; 
} 
int main()
{
    int i = 0;
    int j[2] = {0,1};
    //int a[10] = {1,2,3,4,5,6,7,8,9,10};
    cout << "Calls Function: " << endl;
    print(j,i); 
    cout << "Calls Function: " << endl;
    print2(j,i);

    return 0;
} 

练习6.24

正确的

 1 void print(const int ia[10]) 
 2 {
 3     for (size_t i = 0; i != 10; ++i)
 4         cout << ia[i] << endl;
 5 }
 6 
 7 int main()
 8 {
 9     
10     int a[10] = {1,2,3,4,5,6,7,8,9,10};
11     print(a);
12 
13     system("pause");
14     return 0;
15 }

 

c++ primer 6.2.4节练习答案