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 double
long 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 choices
long 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);
}