C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第七章(chapter 7) 1-5
2014-02-17 19:05
344 查看
7.1//calculate the harmonic average//program end until you enter zero#include <iostream>using namespace std;double h_average(double,double);
int main(){ double a,b,average; cout<<"Please enter two numbers:\n"; while(cin>>a>>b&&a!=0&&b!=0)//a*b!=0 { average=h_average(a,b); cout<<"Harmonic average:"<<average<<endl; cout<<"Please enter two numbers:\n"; continue; } cout<<"Bye!!"; return 0;//dd}
double h_average(double a,double b){ double average; average=2*a*b/(a+b); return average;}
7.2//enter at most ten groups of your golf scores//display,calculate the average,show the average.#include <iostream>using namespace std;int read(double*,int);double calculate(double*,int);void show(double*,int,double);const int Size=10;
int main(){ double scores[Size]; int count; cout<<"Enter at most ten groups of your golf scores.(q to quit)\n"; count=read(scores,Size); double average; average=calculate(scores,count); show(scores,count,average); return 0; //dd
}
int read(double scores[],int n){ int i=0; cout<<"# "<<i+1<<" : "; for(i;i<n;i++) { if(cin>>scores[i]) { cout<<"# "<<i+2<<" : "; } else break; }
return i;}
double calculate(double scores[],int count){ double average=0.0,sum=0.0; for(int i=0;i<count;i++) sum+=scores[i]; average=sum/count; return average; }
void show(double scores[],int count,double average){ cout<<"Here are the scores: "; for(int i=0;i<count;i++) cout<<scores[i]<<' '; cout<<endl; cout<<"Average: "<<average; }7.3//using a program to test//two functions ,one that passes structure by value//the other passes structure by address#include <iostream>using namespace std;struct box{ char maker[40]; float height; float width; float length; float volume;};
void show1(box);void show2(box*);void setbox(box*);
int main(){ box car = { "Mercedes benz", 200, 100, 2, }; setbox(&car); show1(car); cout<<endl; show2(&car); return 0;}
void show1(box car){ cout<<"Maker: "<<car.maker<<endl; cout<<"height: "<<car.height<<endl; cout<<"width: "<<car.width<<endl; cout<<"length: "<<car.length<<endl; cout<<"volume: "<<car.volume<<endl;}
void show2(box* car){ cout<<"Maker: "<<car->maker<<endl; cout<<"height: "<<car->height<<endl; cout<<"width: "<<car->width<<endl; cout<<"length: "<<car->length<<endl; cout<<"volume: "<<car->volume<<endl;}
void setbox(box*pb){ pb->volume=pb->height*pb->width*pb->length;}
7.4// lotto.cpp -- probability of winning#include <iostream>// Note: some implementations require double instead of long doublelong double probability(unsigned numbers, unsigned picks);int main(){ using namespace std; double total, choices,total2,choices2,p2; cout << "Enter the total number of choices on the game card and\n" "the number of picks allowed:\n"; while ((cin >> total >> choices) && choices <= total) { cout<<"Enter the total number of the second field number\n" "and you can choose at most one or not.\n"; while((cin>>total2>>choices2)&&choices2<=1) { if(choices2==1){ p2=probability(total2,choices2); break; } else { p2=1; break; } } cout << "You have one chance in "; cout << probability(total, choices)*p2; cout << " of winning.\n"; cout << "Next two numbers (q to quit): "; } cout << "bye\n"; // cin.get(); // cin.get(); return 0;//dd}
// the following function calculates the probability of picking picks// numbers correctly from numbers choiceslong double probability(unsigned numbers, unsigned picks){ long double result = 1.0; // here come some local variables long double n; unsigned p;
for (n = numbers, p = picks; p > 0; n--, p--) result = result * n / p ; return result;}7.5//using recursive function to caculate factorial#include <iostream>using namespace std;long long factorial(int);
int main(){ int num; cout<<"Please enter a integer number(q to quit): "; while(cin>>num&&num>=0) { cout<<num<<"! = "<<factorial(num)<<endl; cout<<"Please enter a integer number: "; continue; } cout<<"Bye!!"; return 0; //dd }
long long factorial(int num){ if(num==0) return 1; else if(num==1) return 1; return num*factorial(num-1);}
int main(){ double a,b,average; cout<<"Please enter two numbers:\n"; while(cin>>a>>b&&a!=0&&b!=0)//a*b!=0 { average=h_average(a,b); cout<<"Harmonic average:"<<average<<endl; cout<<"Please enter two numbers:\n"; continue; } cout<<"Bye!!"; return 0;//dd}
double h_average(double a,double b){ double average; average=2*a*b/(a+b); return average;}
7.2//enter at most ten groups of your golf scores//display,calculate the average,show the average.#include <iostream>using namespace std;int read(double*,int);double calculate(double*,int);void show(double*,int,double);const int Size=10;
int main(){ double scores[Size]; int count; cout<<"Enter at most ten groups of your golf scores.(q to quit)\n"; count=read(scores,Size); double average; average=calculate(scores,count); show(scores,count,average); return 0; //dd
}
int read(double scores[],int n){ int i=0; cout<<"# "<<i+1<<" : "; for(i;i<n;i++) { if(cin>>scores[i]) { cout<<"# "<<i+2<<" : "; } else break; }
return i;}
double calculate(double scores[],int count){ double average=0.0,sum=0.0; for(int i=0;i<count;i++) sum+=scores[i]; average=sum/count; return average; }
void show(double scores[],int count,double average){ cout<<"Here are the scores: "; for(int i=0;i<count;i++) cout<<scores[i]<<' '; cout<<endl; cout<<"Average: "<<average; }7.3//using a program to test//two functions ,one that passes structure by value//the other passes structure by address#include <iostream>using namespace std;struct box{ char maker[40]; float height; float width; float length; float volume;};
void show1(box);void show2(box*);void setbox(box*);
int main(){ box car = { "Mercedes benz", 200, 100, 2, }; setbox(&car); show1(car); cout<<endl; show2(&car); return 0;}
void show1(box car){ cout<<"Maker: "<<car.maker<<endl; cout<<"height: "<<car.height<<endl; cout<<"width: "<<car.width<<endl; cout<<"length: "<<car.length<<endl; cout<<"volume: "<<car.volume<<endl;}
void show2(box* car){ cout<<"Maker: "<<car->maker<<endl; cout<<"height: "<<car->height<<endl; cout<<"width: "<<car->width<<endl; cout<<"length: "<<car->length<<endl; cout<<"volume: "<<car->volume<<endl;}
void setbox(box*pb){ pb->volume=pb->height*pb->width*pb->length;}
7.4// lotto.cpp -- probability of winning#include <iostream>// Note: some implementations require double instead of long doublelong double probability(unsigned numbers, unsigned picks);int main(){ using namespace std; double total, choices,total2,choices2,p2; cout << "Enter the total number of choices on the game card and\n" "the number of picks allowed:\n"; while ((cin >> total >> choices) && choices <= total) { cout<<"Enter the total number of the second field number\n" "and you can choose at most one or not.\n"; while((cin>>total2>>choices2)&&choices2<=1) { if(choices2==1){ p2=probability(total2,choices2); break; } else { p2=1; break; } } cout << "You have one chance in "; cout << probability(total, choices)*p2; cout << " of winning.\n"; cout << "Next two numbers (q to quit): "; } cout << "bye\n"; // cin.get(); // cin.get(); return 0;//dd}
// the following function calculates the probability of picking picks// numbers correctly from numbers choiceslong double probability(unsigned numbers, unsigned picks){ long double result = 1.0; // here come some local variables long double n; unsigned p;
for (n = numbers, p = picks; p > 0; n--, p--) result = result * n / p ; return result;}7.5//using recursive function to caculate factorial#include <iostream>using namespace std;long long factorial(int);
int main(){ int num; cout<<"Please enter a integer number(q to quit): "; while(cin>>num&&num>=0) { cout<<num<<"! = "<<factorial(num)<<endl; cout<<"Please enter a integer number: "; continue; } cout<<"Bye!!"; return 0; //dd }
long long factorial(int num){ if(num==0) return 1; else if(num==1) return 1; return num*factorial(num-1);}
相关文章推荐
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第七章(chapter 7) 6-10
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第八章(chapter 8) 1-4
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第九章(chapter 9) 1-4
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第五章(chapter 5) 1-5
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第五章(chapter 5) 6-10
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第六章(chapter 6) 1-4
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第六章(chapter 6) 5-9
- C++ Primer Plus第六版 第七章 编程练习答案
- C++ PRIMER PLUS (第六版) 中文版 第七章编程练习答案
- C++ Primer Plus 第六版 学习笔记 第七章 编程练习答案
- C++ Primer Plus第五版 第七章 编程练习答案
- c++ primer plus(第6版)中文版 第七章编程练习答案
- c++ primer plus(第6版)中文版 第四章编程练习答案
- C++primer plus第六版课后编程练习答案7.6
- 《C++ Primer Plus(第六版)》(45)(第十八章 探讨C++新标准 编程练习和答案)
- 【学习C++】C++ Primer Plus (第六版)第七章编程练习6-10
- C++ primer plus第六版课后编程练习答案:6.4
- C++primer plus第六版课后编程练习答案12.3
- C++ Primer Plus课后编程练习——第七章 函数-C++的编程模块(部分)
- C++ Primer Plus第六版 第九章 编程练习答案