C++ 数组的随机双精度
这是一个公式,我应该用它来创建一个函数,用随机双精度填充数组C++ 数组的随机双精度,c++,arrays,random,C++,Arrays,Random,这是一个公式,我应该用它来创建一个函数,用随机双精度填充数组 lower bound + ( random integer / (maximum possible random number / (upper bound - lower bound))) 下限为0,上限为100,最大值为RAND_max 函数是一个不带参数的双精度函数() 在我的一生中,我不知道如何使这个公式作为一个函数工作,或者如何用它的结果填充数组 它只是给了我一个错误: 无法将double()转换为double 我这里有
lower bound + ( random integer / (maximum possible random number / (upper bound - lower bound)))
下限为0,上限为100,最大值为RAND_max
函数是一个不带参数的双精度函数(
)
在我的一生中,我不知道如何使这个公式作为一个函数工作,或者如何用它的结果填充数组
它只是给了我一个错误:
无法将double()转换为double
我这里有一个“某种”工作版本的程序
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <iomanip>
using namespace std;
int buildArray(double[]);
void printArray (double[],int, int);
double randDouble();
const int LOWER=0;
const int UPPER=100;
const int ARRAY_SIZE=100;
int main(){
int numberElements = 0;
int line;
double MainArray [ARRAY_SIZE];
numberElements=buildArray(MainArray);
cout<< "how many values per line? ";
cin>>line;
cout<<"unsorted Values"<<endl<<endl;
printArray(MainArray, numberElements, line);
cout<<endl<<"sorted values"<<endl<<endl;
sort(MainArray, MainArray + numberElements);
cout<<endl<<endl;
printArray(MainArray, numberElements, line);
return 0;
//the function would normally go right here
}
int buildArray(double MainArray[]){
int i=0;
int arrayTotal;
srand(time(NULL));
while(arrayTotal <20){
arrayTotal=rand()%100;
}
while(i<arrayTotal){
//and I would insert it below here instead of this jerry-rigged thing
MainArray[i]=rand()%100/((double)RAND_MAX/10000);
i++;
}
return arrayTotal;
}
void printArray(double MainArray[],int numOfValues,int line){
int lineBreak = 0;
for(int i = 0; i < numOfValues; i++){
if (lineBreak==line){
cout<<endl;
lineBreak=0;
}
lineBreak++;
cout<<setprecision(5)<<MainArray[i]<<" ";
}
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int buildArray(双[]);
void printary(double[],int,int);
双随机双();
const int LOWER=0;
常数int上限=100;
常量int数组_SIZE=100;
int main(){
int numberrelations=0;
内线;
双主阵列[阵列大小];
NumberRelations=buildArray(主阵列);
曲线;
CUT< P>我知道C/C++的开始是很难的:< /P>
尝试用以下内容替换buildArray
int buildArray(double MainArray[]){
int i=0;
srand(time(NULL));
while(i<ARRAY_SIZE){
MainArray[i]= LOWER + double(rand()) * (UPPER - LOWER) / RAND_MAX;
i++;
}
return ARRAY_SIZE;
}
intbuildarray(双主数组[]){
int i=0;
srand(时间(空));
while(iOnly callsrand
一次。你能写一个函数来做1浮点并在此基础上构建吗?谢谢,我让它工作起来了。你的回答非常有用,而且比我预期的要彻底得多。我会记住你说过的关于使用向量的内容。
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <iomanip>
void buildArray(std::vector<double> & myVector);
void printArray(std::vector<double> & myVector, int line);
const int LOWER=0;
const int UPPER=100;
int main()
{
const size_t arraySize = 100;
std::vector<double> myVector;
myVector.resize(arraySize);
srand(time(nullptr)); // Call this only once
buildArray(myVector);
std::cout << "how many values per line? ";
int line = 0;
std::cin >> line;
std::cout << "unsorted Values" << std::endl << std::endl;
printArray(myVector, line);
std::cout << std::endl<<"sorted values" << std::endl << std::endl;
std::sort(myVector.begin(), myVector.end());
std::cout << std::endl << std::endl;
printArray(myVector, line);
return 0;
}
void buildArray(std::vector<double> & myVector)
{
for (size_t i = 0; i < myVector.size(); ++i) {
myVector[i] = LOWER + double(rand()) * (UPPER - LOWER) / RAND_MAX;
}
}
void printArray(std::vector<double> & myVector, int line)
{
size_t lineBreak = 0;
for (size_t i = 0; i < myVector.size(); i++){
if (lineBreak == line){
std::cout << std::endl;
lineBreak=0;
}
lineBreak++;
std::cout << std::setprecision(5) << myVector[i] << " ";
}
}