您好我正在尝试用C ++编写程序,生成并打印0到999之间的20个随机数,并在不使用内置函数的情况下执行以下操作,查找并打印:最小值,最大值,平均值,中位数,标准差,方差。在第15个元素上进行二分查找。请帮我解释一下代码。
到目前为止,我已经完成了这么多 #包括 #包括 #include
df1 <- data.frame(TIME_ACTUAL_DEPART = c("1899-12-30 10:43:41",
"1899-12-30 20:01:27", "1899-12-30 06:41:07",
"1899-12-30 19:29:19", "1899-12-30 13:24:12",
"1899-12-30 23:12:14"),
stringsAsFactors=FALSE)
答案 0 :(得分:1)
你有办法,你的代码不会做任何你想要的事情。但是,您提到您是初学者,因此我修复了您的代码并设置了如何开始的基本结构。我留下了关于我改变了什么以及你需要做什么的评论。话虽如此,我不知道你的意思&#34;对第15个元素进行二元搜索&#34;
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int ra()
{
// You wanted a number between 0 and 999 inclusive so do not add 1
// Instead do a modulus of 1000
int r = rand() % 1000;
return r;
}
int main ()
{
// Do this to get different random numbers each time you run your program
srand(time(NULL));
// You have to call ra as a function. Do this by writing: ra()
// Here I am storing 20 random numbers in an array
int nums[20];
for (unsigned int i = 0; i < 20; ++i)
{
nums[i] = ra();
cout << "Index: " << i << ", random number: " << nums[i] << endl;
}
// Iterate to find the minimum number
int minimum = nums[0];
for (unsigned int i = 1; i < 20; ++i)
if (nums[i] < minimum)
minimum = nums[i];
cout << "Minimum value: " << minimum << endl;
// TODO: Find the maximum in basically the same way
// TODO: Find the average by summing all numbers then dividing by 20
// TODO: Find the median by sorting nums and taking the average of the two center elements
// TODO: etc.
return 0;
}
答案 1 :(得分:0)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int r;
int ra;
int i=0;
int ra(){
r = (rand() % 999) + 1;
return r;
}
int main ()
{
int random_;
srand((int)time(0));
while (i++ < 20)
{
random_ = r;
cout<< random_<<endl;
}
return 0;
}