您的位置:首页 > 编程语言 > C语言/C++

c++primer exercises 7.0

2015-08-05 20:07 375 查看
Exercise 7.1 What is th difference betwen a parameter and an argument?

Ans: 形参与实参的区别

1. 初始化方式。形参在函数的形参表里定义,由函数调用的时候,实参来初始化。 实参可以是表达式,字面值常量。

2. 存储位置不同。 形参在函数调用的时候存储在栈区。实参依据具体的作用域不同,其存储位置有不同。

Exercise 7.2Indicate which of the following functions are in error and why Suggest how you might correct the problems

(a) int f(){

string s; //warning, 最好初始化。虽然程序会调用默认构造函数来初始化。

//...

return s; //error, 返回类型与函数定义的返回类型int不匹配。将int f()改成string f()

}

(b) f2(int i){/*...*/} //error, 函数必须制定返回类型。改成void f2(int i)

(c) int calc(int v1, int v1){ /*...*/}// OK

(d) double square(double x) return x*x; //error缺少函数体的大括弧。

Exercise 7.3 Write a program to make two int parameters and generate the result of rasing the first parameter to the power of the second. Write a program to call your function passing it two ints, verify the result.

Ans:

#include<iostream>

using namespace::std;

double power_calc(int low, int power)

{

double result = 1.0;

int abs_power = 0;

if(power == 0 )

{

return result;

}

else

{

if(power < 0 )

{

abs_power = 0 - power;

}

if(low == 0 )

{

cout<<"error"<<endl;

return 0;

}

for(int i = 1; i<= abs_power; ++i)

{

result *= low;

}

if(power < 0)

{

cout<<"power <0"<<endl;

result = 1.0/result;

}

return result;

}

}

int main()

{

double i = power_calc(2,-4);

cout<<"2 the power of -4 "<<"is "<<i<<endl;

}

Exercise 7.4 Write a program to return the absolute value of its parameter.

#include<iostream>

using namespace::std;

int abs_value(int para)

{

int result = para;

if(para < 0)

{

cout<<"para is lower than 0"<<endl;

result = 0 - para;

}

cout<<"result is "<<result<<endl;

return result;

}

int main()

{

int i = 0;

cout<<"please type one int value"<<endl;

cin>>i;

int result = abs_value(i);

cout<<"the absolute value of "<<i<<" is "<<result<<endl;

return 0;

Exercise 7.5 Write a function that takes an int and a pointer to an int and returns the larger of the int value of the value to which the pointer points. what types should you use for the pointer?

#include<iostream>

using namespace::std;

int get_large(int v1, const int *p)

{

if(v1 < *p)

{

return *p;

}

else

{

return v1;

}

}

int main()

{

int v1 = 0;

int v2 = 0;

cout<<"please type two int value:"<<endl;

cin>>v1>>v2;

int *p = &v2;

cout<<"the large value is "<<get_large(v1, p)<<endl;

}

Exercise 7.6: Write a function to swap the valus pointed to by two pointers to int. Test the function by calling it and printing the swapped values.

using namespace::std;

void swap_int(int *p1, int *p2)

{

int temp = 0;

temp = *p1;

*p1 = *p2;

*p2 = temp;

}

int main()

{

int v1(0),v2(5);

int *p1 = &v1;

int * p2 = &v2;

cout<<"before swap:"<<*p1<<" "<<*p2<<endl;

swap_int(p1, p2);

cout<<"after swap: "<<*p1<<" "<<*p2<<endl;

return 0;

}

Exercise 7.7 Explain thee difference in the following two paramter declarations.

void f(T);

void f(T &);

两个形参的声明里面没有定义形参的标识符,只有类型。当然,这也是可以的。第二个定义是T类型的一个引用。

Exercise 7.8 Give an example of when a parameter should be a reference type. Given an example of when a paramter should not be a reference.

Exercies 7.9 Change the declaration of occurs in the paramter list of find_val(defined on page 234) to be a nonreferenc argument type and return the program. how does the behavior of the program change?

Exercise 7.10: The following program, although legal, is less useful than it might be. Identify and correct the limitation on this program.

Exercise 7.11 When should reference parameters be const? What probleems might arise if we make a parameter a plain reference when it could be a const reference?

Exercise 7.12:When would you use a parameter that is a pointer? When would you use a parameter that is a reference? Explain the advantages and disadvantages of each?

Exercise 7.13: write a program to calculate the sum of the elements in an array. Write the function thre times, each one using a diferent approach to managing the array bounds

Exercise 7.14: Write a program to sum the elements in a vector<double>

Exercise 7.15: Write a main unction that takes two values as arguments and print their sum.

Exercise 7.16: Write a program that could accept the optionos presented in this section. Print the values of the arguments passed to main.

Exercise 7.17: When is it valid to return a reference? A const reference?

Exercise 7.18: What potential run-time problem does the following function have?

Exercise 7.19: Indicate whether the following program is legal. If so, explain what it does: if not, mak it legal and then explain it:

int &get(int *arry, int index)

{return arry[index];}

int main()

{

int ia[10];

for(int i = 0; i != 10; ++i)

{

get(ia,i) = 0;

}

Exercise 7.20: Rwrite factorial as in interativ function.

#include<iostream>

using namespace::std;

void factorial(int fac)

{

long result = 1;

for(int i = 1; i <= fac; ++i)

{

result *= i;

}

cout<<"result is "<<result<<endl;

}

int factor_recur(int fac)

{

int result = 1;

if(fac == 1)

{

cout<<"result is "<<result<<endl;

return result;

}

factor_recur(fac-1 )*fac;

cout<<"result is "<<result<<endl;

return result;

}

int main()

{

int fac = 0;

cout<<"please enter one value:"<<endl;

cin>>fac;

//factorial(fac);

factor_recur(fac);

}

Exercise 7.21: What would happen if th stopping condition in factorial were:

if(val != 0)

会出现什么问题

Exercise 7.22: Write the prototypes for each of the following functions

ans: 程序也可以正常结束。

Exercise 7.23: Given the following declarations, deetermin which calls are legal and which are illegal. For those that ar illegal, explain why?

double calc(double);

int count(const string &, char)

int sum(vector<int>::iterator, vector<int>::iterator, int);

vector<int> vec(10);

(a) cal(23.4,55.1);

(b) count("adcda",'a')

(c)calc(66);

(d) sum(vc.begin(),vec.end(),3.8)

Exercise 7.24: which, if any, of th following declarations are errors? why?

参考《c++ premier》第四版

(a) int ff(int a, int b= 0, int c=0)

(b char *init(int ht =24, int wd, char bckgrnd);

Exercise 7.25: Given the following function declarations and calls, which , if any, of the calls are illegal? why? which, if any, are legal but unlikely to match the programmer's intent? why?

char* init(int ht, int wd = 80, char bckgrnd='');

(a) init();

(b) init(24,10);

(c) init(14,'*')

Exercise 7.26: write a version of mak_plural with a dfault argument of 's'. Use that version to print singular and plural versions of th words "success" and "falure".

Exercise 7.27: Explain the differences betwen a prameter, a local variable and a static local variable. Give an example of a program in which each might be useful.

Exercise 7.28: Write a function that returns 0 when it is first called and then generates numbers in sequence each time it is called again.

Exercise 7.29: Which one of th fllowing declarations and definitiosn would you put in a eader? IN a program text file? Explain why?

(a) inline bool eq(const BigInt&, const BigInt&){}

(b) void putValues(int *arr, int size)

Exercise 7.30: Rewrite the is shorteer function from apge 235 as an inlin function.

Exercise 7.31: Write ourown version of the Sales_item class, adding two new public members to read and write Sales_item objects. These functions should operate similarly to the input and output operators used in Chapter1. Transactions should look like the
one defined in that chapter as well. Use this class to read and write a set of transactions.

Exercise 7.32: Write a header file to contain your version of th Sales_item class. Use ordinary C++conventions to name the header and any associated file needed to hould non-inline function defined outside the lcass. 

Exrcise 7.33. Add a member that adds two Sales_items Use the revised class to reimplement your solution to the average price problem presented in Chapter 1.

Exercise 7.34: Definie a set of overloaded functions named error that would match the following calls.

int index, upperBound;

char selectVal;

rror("Subscript out of bounds:", index, upperBound);

error("Division by zero");

error("Invalid selection",selectVal);

Exercise 7.35:Explain the effect of th second declaration in each one fo the following sets of declarations. INdicate which ,if any, are illegal.

(a) int calc(int, int);

int calc(const int, const int)

(b) int get()

double get();

(c) int * reset(int *);

double *reset(double *);

Exercise 7.37: Given th dclarations for f, dtermine whether the following calls are legal. For each call list the viable functions .if any, if the calll is illegal, indicate whether there is no match or wy the call is ambigour, if the call is legal. indicate
which function is the best match.

(a)f(2.56, 42);

(b) f(42);

(c)f(42,0);

(d) f(2.56,3.14);

Exercise 7.38: Given th following dclarations.

void mnip(int, int);

double dobj;

what is the rank(Section 7.8.4, p.272) of each conversion in the following calls?

(a) manip('a','z')

(b) mainp(55.4, dobj);

Exercise 7.39 Explain the ffect of the second declaration in each one of hte following sets of declarations. Indicate which , if any, are illegal.

(a)int calc(int, int

int calc)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: