【发布时间】:2013-10-14 19:51:03
【问题描述】:
假设您有一个完全定义且正确的函数,其原型如下
int randomBetween( int lowerBound, int upperBound );
返回一个介于上下限之间的随机数。
编写一个 main 函数,使用 randomBetween 生成 1 到 10 之间的 1000 个值,包括 1 和 10。 随着值的生成,计算该值等于 5 的次数。 显示该值等于五的次数。 您的程序应该只输出一个数字(即,不要将 cout 语句放入循环中)。
这是我目前所拥有的:
// Function Prototypes
int randomBetween( int lowerBound, int upperBound );
int main( )
{
int lowerbound, upperbound;
cout << "Enter the value for the lower bound: " ;
cin >> lowerbound;
cout << "Enter the value for the upper bound ( lower < upper ) : " ;
cin >> upperbound;
cout << "The random value between " << lowerbound << " and "<< upperbound
<< " is " << randomBetween << endl;
return EXIT_SUCCESS;
}
int randomBetween( int lowerBound, int upperBound )
{
int upperbound, lowerbound;
int randomBetween = rand() % (upperbound-lowerbound) + upperbound;
return randomBetween;
}
当我编译程序并输入下限和上限的值时,我得到的答案是:00E9131B
【问题讨论】: