这是我应该用来创建一个用随机双打填充数组的函数的公式
lower bound + ( random integer / (maximum possible random number / (upper bound - lower bound)))
下限为0,上限为100,最大值为RAND_MAX
该函数是一个不带参数的double ___()
函数
对于我的生活,我无法弄清楚如何使这个公式作为一个函数或如何用其结果填充数组
它只是给我错误:
无法将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]<<" ";
}
}
抱歉,我的代码很邋and且效率低下。也很抱歉这个问题可能是一个明显的解决方案。我现在没有参加任何课程,我只是想自学C ++,所以我没有其他人可以问。
答案 0 :(得分:1)
我知道使用C / C ++开始很难:)
尝试使用以下
替换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;
}
一切都像魔术......
但请不要对C ++有不正确的看法。 &#34;现代&#34;代码应如下所示:
#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] << " ";
}
}
了解如何使用std::vector
代替C风格的数组......:)